Pagini recente » Cod sursa (job #218636) | Cod sursa (job #3121317) | Cod sursa (job #2250980) | Cod sursa (job #552429) | Cod sursa (job #543590)
Cod sursa(job #543590)
#include <stdio.h>
int n, m, viz[100005], nr;
typedef struct nod
{
int x;
nod *b;
} *pNod;
pNod v[100005];
void citire()
{
freopen("dfs.in","r",stdin);
freopen("dfs.out","w",stdout);
scanf("%d %d",&n,&m);
int x, y;
pNod a;
for (int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
a=new nod;
a->x=x;
a->b=v[y];
v[y]=a;
a=new nod;
a->x=y;
a->b=v[x];
v[x]=a;
}
}
void DFS(int nod)
{
pNod a;
viz[nod]=1;
for (a=v[nod];a!=NULL;a=a->b)
if (!viz[a->x])DFS(a->x);
}
int main()
{
citire();
for (int i =1;i<=n;i++)
if (!viz[i])
{
nr++;
DFS(i);
}
printf("%d\n",nr
);
return 0;
}