没什么可说的,我自己写的都一次通过了.
自己写的代码还要注意细节.
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAX 50000
#define MAXN 1300000000
int father[MAX],son[MAX];
long long i;
void Uset(int n)
{
for(i=1;i<=n;i++)
{
father[i]=i;
son[i]=1;
}
}
int find(int x)
{
return (x == father[x] ? x : find(father[x]) );
}
bool Union(long long x,long long y)//有问题啊
{
long long root1=find(x);
long long root2=find(y);
if(root1 == root2)
return false;
else if(son[root1] >= son[root2])
{
father[root2]=root1;
son[root1]+=son[root2];
}
else
{
father[root1]=root2;
son[root2]+=son[root1];
}
return true;
}
int main()
{
// freopen("1232.txt","r",stdin);
long long n,m,a,b,count;
int kcase=0;//?
while(1)
{
scanf("%lld%lld",&n,&m);
Uset(n);
if(n==0 && m==0)
break;
count=0;
while(m--)
{
scanf("%lld%lld",&a,&b);
// Union(a,b);
if(Union(a,b))
++count;
}
kcase++;
printf("Case %d: %lld\n",kcase,n-count);
// printf("son[1]=%d\n",son[1]);
}
return 0;
}