Cod sursa(job #631817)
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> a[100001];
bool ok[100001];
void DF(int nod)
{
ok[nod]=true;
for (vector<int>::iterator it=a[nod].begin();it!=a[nod].end();it++)
if (ok[*it]==false) DF(*it);
}
int main()
{
int nr,i,x,y,N,M;
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%d%d",&N,&M);
for (i=1;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 (ok[i]==false)
{
DF(i);
nr++;
}
printf("%d\n",nr);
}