标签:des style http color java 使用 os io
/*
中文题意:
中文翻译:
题目大意:
解题思路:
难点详解:
关键点:
解题人:lingnichong
解题时间:2014/08/05 11:34
解题感受:并查集的使用
*/
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 998Huge input, scanf is recommended.HintHint
#include<stdio.h>
int father[1010],tot;
int find(int x)
{
int r=x;
return r==father[r]? r:r=find(father[r]);
}
void join(int a,int b)
{
int fa = find(a),fb=find(b);
if(fa!=fb)
{
father[fa]=fb;
tot--;
}
}
int main()
{
int n,m;
int i,x,y;
while(scanf("%d",&n),n)
{
scanf("%d",&m);
tot=n-1;
father[n+1];
for(i=1;i<=n;i++)
father[i]=i;
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
join(x,y);
}
printf("%d\n",tot);
}
return 0;
}
标签:des style http color java 使用 os io
原文地址:http://blog.csdn.net/qq_16767427/article/details/38383067