wa:find函数写错了,在此贴出..
#include<stdio.h>
int father[50005];
int find(int x)
{
if (father[x]!=x) father[x]=find(father[x]);
else
return father[x];
}
int main()
{
long long cas=0;
while (1)
{
long long n,m,i,x,y,c=0;
scanf("%lld%lld",&n,&m);
if (n==0&&m==0) return 0;
for (i=1;i<=n;i++) father[i]=i;
for (i=1;i<=m;i++)
{
scanf("%lld%lld",&x,&y);
if (find(x)!=find(y)) father[find(y)]=find(x);
}
for (i=1;i<=n;i++)
{
find(i);
if (father[i]==i) c++;
}
printf("Case %lld: %lld\n",++cas,c);
}
}
简单的并查集,输出总数即可,石头剪刀步类型的还是一脸蒙蔽
#include<stdio.h>
int father[50005];
int find(int x)
{
return father[x]==x?x:find(father[x]);
}
int main()
{
long long cas=0;
while (1)
{
long long n,m,i,x,y,c=0;
scanf("%lld%lld",&n,&m);
if (n==0&&m==0) return 0;
for (i=1;i<=n;i++) father[i]=i;
for (i=1;i<=m;i++)
{
scanf("%lld%lld",&x,&y);
if (find(x)!=find(y)) father[find(y)]=find(x);
}
for (i=1;i<=n;i++)
{
find(i);
if (father[i]==i) c++;
}
printf("Case %lld: %lld\n",++cas,c);
}
}