Cod sursa(job #313358)
| Utilizator | Data | 8 mai 2009 21:42:11 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 55 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <cstdio>
#include <vector>
using namespace std;
vector <int> G[100002];
int viz[10000],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("%d %d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d %d",&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("%d",cnt);
return 0;
}
