Cod sursa(job #283417)
| Utilizator | Data | 19 martie 2009 09:37:27 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 5 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.52 kb |
#include<stdio.h>
int s[50], a[50][50], n,m, c=0;
void graf(int nod){
s[nod] = 1;
for (int i = 1; i <= n; i++)
if (!s[i] && a[nod][i])
graf(i);
}
int main(){
int x, y;
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%d %d",&n,&m);
for (int i = 1; i <= m; i++){
scanf("%d %d",&x,&y);
a[x][y] = a[y][x] = 1;
}
for (int i = 1; i <= n; i++)
if (!s[i]) {
c++;
graf(i);
}
printf("%d\n",c);
return 0;
}
