Pagini recente » Cod sursa (job #1047270) | Cod sursa (job #2708142) | Cod sursa (job #2710904) | Cod sursa (job #1516381) | Cod sursa (job #326228)
Cod sursa(job #326228)
#include<stdio.h>
long n,m;
long k;
int v[100010];
struct nod
{
nod *urm;
long info;
};
nod *top[100010];
void InsertBegin(long x,long y)
{
nod *aux;
aux=new nod;
aux->urm=top[x];
aux->info=y;
top[x]=aux;
aux=new nod;
aux->urm=top[y];
aux->info=x;
top[y]=aux;
}
void read()
{
scanf("%ld%ld",&n,&m);
long i,x,y;
for (i=1;i<=m;i++)
{
scanf("%ld%ld",&x,&y);
InsertBegin(x,y);
}
}
void dfs(long x)
{
if (top[x]==NULL)
{
return;
}
nod *p;
for (p=top[x];p!=NULL;p=p->urm)
{
if (!v[p->info])
{
v[p->info]=1;
dfs(p->info);
}
}
}
void rez()
{
long i;
for (i=1;i<=n;i++)
{
if (!v[i])
{
k++;
dfs(i);
}
}
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
read();
rez();
printf("%ld",k);
return 0;
}