Pagini recente » Cod sursa (job #272850) | Cod sursa (job #1566431) | Cod sursa (job #680055) | Cod sursa (job #1200921) | Cod sursa (job #1720188)
# include<cstdio>
using namespace std;
# define nmax 100000
int N,M;
typedef struct nod
{
int x;
nod *a;
}*pNod;
pNod A[nmax];bool viz[nmax];
void add(pNod &adr,int val)
{
pNod p;
p=new nod;
p->x=val;
p->a=adr;
adr=p;
}
void citire()
{
FILE *f=freopen("dfs.in","r",stdin),*g=freopen("dfs.out","w",stdout);
scanf("%d%d",&N,&M);
int x,y;
for(int i=1;i<=M;++i)
{
scanf("%d%d",&x,&y);
add(A[x],y);
add(A[y],x);
}
}
void dfs(int start)
{
viz[start]=1;
pNod p;
for(p=A[start];p!=NULL;p=p->a)if(!viz[p->x])dfs(p->x);
}
int main()
{
citire();
int cnt=0,i;
for(i=1;i<=N;++i)
if(!viz[i])
{
cnt++;
dfs(i);
}
printf("%d",cnt);
}