Pagini recente » Cod sursa (job #993644) | Cod sursa (job #2418990) | Cod sursa (job #2457221) | Cod sursa (job #3040837) | Cod sursa (job #952521)
Cod sursa(job #952521)
#include<cstdio>
#include<vector>
using namespace std;
vector <int> G[100005];
int m,n,nr,viz[100005],rez,x,y;
int vizitat()
{
int i;
for (i=1;i<=n;++i)
if (!viz[i]) return i;
return 0;
}
void dfs(int s, int val)
{
int i,z;
viz[s]=val;
for (i=0;i<G[s].size();++i)
{
z=G[s][i];
if (!viz[z]) dfs(z,val);
}
}
int main()
{
int i;
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);
}
rez=vizitat();
while (rez)
{
++nr;
dfs(rez,nr);
rez=vizitat();
}
printf("%d\n",nr);
return 0;
}