标签:
Total Submission(s): 31033 Accepted Submission(s): 16313
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
1 0 2 998HintHint
水题,就是把有关系的联系在一起,不解释
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 10005
int father[N];
int cha(int x)
{
if(x!=father[x])
father[x]=cha(father[x]);
return father[x];
}
int main()
{
int i,n,m;
while(scanf("%d",&n),n)
{
scanf("%d",&m);
for(i=1;i<=n;i++)
father[i]=i;
int x,y;
while(m--)
{
scanf("%d%d",&x,&y);
int xx=cha(x);
int yy=cha(y);
father[xx]=yy;
}
int ans=-1;
for(i=1;i<=n;i++)
if(father[i]==i)
ans++;
printf("%d\n",ans);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/4239890.html