Pagini recente » Cod sursa (job #1786143) | Cod sursa (job #2146900) | Cod sursa (job #1239260) | Cod sursa (job #1350004) | Cod sursa (job #1507520)
#include <cstdio>
#include <vector>
using namespace std;
const int NMAX=100000;
vector <int> G[NMAX+1];
int viz[NMAX+1];
int CC;
void dfs(int u){
viz[u]=CC;
for(int i=0;i<(int)G[u].size();i++){
int v=G[u][i];
if(!viz[v]){
dfs(v);
}
}
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
int n,m,u,v,i;
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
for(i=1;i<=n;i++){
if(!viz[i]){
CC++;
dfs(i);
}
}
printf("%d\n",CC);
return 0;
}