Pagini recente » Cod sursa (job #3208591) | Cod sursa (job #1140664) | Cod sursa (job #2909976) | Cod sursa (job #1190114) | Cod sursa (job #698909)
Cod sursa(job #698909)
#include<cstdio>
#include<vector>
using namespace std;
int n,m,i,j,x,y,nr,viz[100001];
vector <int> G[100001];
int dfs(int nod)
{
int i;
if(viz[nod])
return 0;
viz[nod]=nr;
for(i=0;i<G[nod].size();i++)
dfs(G[nod][i]);
return 0;
}
int main()
{
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);
G[x].push_back(y);
G[y].push_back(x);
}
for(i=1;i<=n;i++)
if(viz[i]==0)
{
nr++;
dfs(i);
}
printf("%d",nr);
return 0;
}