Pagini recente » Cod sursa (job #1322023) | Cod sursa (job #1273861) | Cod sursa (job #309476) | Cod sursa (job #395605) | Cod sursa (job #2043300)
#include <stdio.h>
int n, m, viz[100005], cnt;
struct nod
{
int x;
nod *urm;
} ;
nod *p[100005];
void adaug(int z,nod *&p)
{
nod *t;
t = new nod;
t -> x = z;
t -> urm = p;
p=t;
}
void citire()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%d %d",&n,&m);
int i, x, y;
for (i = 1; i <= m; i++)
{
scanf("%d %d",&x,&y);
adaug(y,p[x]);
adaug(x,p[y]);
}
}
void DFS(int x)
{
nod *q;
viz[x] = 1;
for (q = p[x]; q != NULL; q = q -> urm)
if (!viz[q -> x]) DFS(q -> x);
}
int main()
{
citire();
int i;
for (i = 1; i <= n; i++) if (!viz[i]) { cnt++; DFS(i);}
printf("%d\n",cnt);
return 0;
}