Pagini recente » Cod sursa (job #1130664) | Cod sursa (job #638440) | Profil now_or_never | Cod sursa (job #2246031) | Cod sursa (job #672674)
Cod sursa(job #672674)
#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;
}