Cod sursa(job #2195585)
| Utilizator | Data | 16 aprilie 2018 19:38:49 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int n,m,a[100005],c,x,y,nr;
vector <int> g[100005];
void dfs(int x)
{
a[x]=1;
for(auto &v:g[x])
if(!a[v])
dfs(v);
}
int main()
{
ifstream fin("dfs.in");
fin>>n>>m;
for(int i=0;i<m;i++)
{
fin>>x>>y;
if(x!=y)
{
g[x-1].push_back(y-1);
g[y-1].push_back(x-1);
}
}
for(int i=0;i<n;i++)
{
if(!a[i])
{
dfs(i);
nr++;
}
}
ofstream fout("dfs.out");
fout<<nr;
return 0;
}
