Pagini recente » Cod sursa (job #2477895) | Cod sursa (job #2415030) | Cod sursa (job #1760141) | Cod sursa (job #706711) | Cod sursa (job #672672)
Cod sursa(job #672672)
#include <stdio.h>
int n, m, cnt;
bool a[1005][1005],viz[1005];
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;
}