Cod sursa(job #313363)
Utilizator | Data | 8 mai 2009 21:46:42 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 85 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.66 kb |
#include <cstdio>
#include <vector>
using namespace std;
vector <int> G[100002];
long viz[100000],cnt,x,y,m,n;
void DFS(int q)
{ int i;
viz[q]=1;
for(i=0; i<G[q].size(); ++i)
if(!viz[G[q][i]])
DFS(G[q][i]);
}
int main()
{ int i;
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%ld %ld",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%ld %ld",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
for(i=1; i<=n; ++i)
if(!viz[i])
{cnt++;
DFS(i);
}
printf("%ld",cnt);
return 0;
}