Mai intai trebuie sa te autentifici.
Cod sursa(job #672674)
Utilizator | Data | 2 februarie 2012 21:36:33 | |
---|---|---|---|
Problema | Parcurgere DFS - componente conexe | Scor | 50 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.49 kb |
#include <stdio.h>
int n, m,cnt;
bool a[10000][10000],viz[10000];
void citire()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%d %d",&n,&m);
int i, x, y;
for (i = 1; i <= m; i++)
{
scanf("%d %d",&x,&y);
a[x][y] = a[y][x] = 1;
}
}
void DFS(int nod)
{
int i;
viz[nod] = 1;
for (i = 1; i <= n; i++) if (!viz[i] && a[nod][i]) DFS(i);
}
int main()
{
citire();
int i;
for (i = 1; i <= n; i++) if (!viz[i]) { cnt++; DFS(i);}
printf("%d\n",cnt);
return 0;
}