Pagini recente » Cod sursa (job #388283) | Cod sursa (job #2939036) | Cod sursa (job #1453916) | Cod sursa (job #1368165) | Cod sursa (job #798205)
Cod sursa(job #798205)
#include <stdio.h>
using namespace std;
int n,m,viz[100100],nr;
struct nod
{
int x;
nod *urm;
}*l[100100];
void adaugare(int x, nod*&p)
{
nod *q=new nod;
q->x=x;
q->urm=p;
p=q;
}
void citire()
{
int x,y;
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d %d",&x,&y);
adaugare(x,l[y]);
adaugare(y,l[x]);
}
}
void adancime(int v)
{
viz[v]=1;
for(nod *i=l[v];i;i=i->urm)
if(!viz[i->x])
adancime(i->x);
}
void conexe()
{
for(int i=1;i<=n;i++)
if(!viz[i])
{
nr++;
adancime(i);
}
}
int main()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
citire();
conexe();
printf("%d",nr);
return 0;
}