Pagini recente » Rating Stoian George (atpkekw) | Cod sursa (job #2670446) | Cod sursa (job #2466438) | Istoria paginii runda/infinity/clasament | Cod sursa (job #396706)
Cod sursa(job #396706)
#include <iostream>
#include <stdio.h>
using namespace std;
int a[10001][10001], n, m, viz[1000001], nr;
void citire()
{
freopen("dfs.in","r",stdin);
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 x)
{
viz[x] = 1;
for (int i = 1; i <= n; i++)
if (!viz[i] && a[x][i])
DFS(i);
}
int main()
{
citire();
freopen("dfs.out","w",stdout);
for (int i = 1; i <= n; i++)
if (!viz[i])
{
nr++;
DFS(i);
}
printf("%d\n",nr);
return 0;
}