Pagini recente » Cod sursa (job #1038578) | Cod sursa (job #39601) | Cod sursa (job #3238782) | Cod sursa (job #434168) | Cod sursa (job #631178)
Cod sursa(job #631178)
#include <cstdlib>
#include <iostream>
#include<vector>
#include<stdio.h>
using namespace std;
int N,M,k;
vector <int> a[100001];
int vizitat[100001];
void dfs(int s)
{ vizitat[s]=1;
for( int i=0;i<a[s].size();i++)
if (!vizitat[a[s][i]])
dfs(a[s][i]);
}
int main()
{
int i,x,y,nr=0;
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%d %d", &N,&M);
for(i=0;i<M;i++)
{ scanf("%d %d",&x,&y);
a[x].push_back(y);
a[y].push_back(x);
}
for (i=1;i<=N;i++)
if (!vizitat[i])
{ nr++;
dfs(i);
}
printf("%d",nr);
return 0;
}