/*
2150ms,水过,暂时不知道怎么改
*/
include<cstdio>
using namespace std;
int f[100001],cnt[100001];
int mfind(int x) //find
{
if(f[x]!=x)f[x]=mfind(f[x]);
return f[x];
}
void make(int a,int b) //并操作
{
int f1=mfind(a);
int f2=mfind(b);
if(f1!=f2)
{
f[f2]=f1;
cnt[f1]+=cnt[f2];//将解的个数并于根节点
}
}
int main()
{
int n,m,i,k=1;
while(cin>>n>>m)
{
if(k!=1)printf("\n");
printf("Case %d:\n",k++);
for(i=1;i<=n;i++){
f[i]=i;
cnt[i]=1;
}
//初始化
while(m--)
{
char op;
cin>>op;
int a,b;
if(op=='M')
{
cin>>a>>b;
make(a,b);
}
else{
scanf("%d",&a);
int t=mfind(a);
printf("%d\n",cnt[t]);
}
}
}
return 0;
}
ZOJ 2833 Friendship(并查集) 简单题
最新推荐文章于 2024-04-14 19:03:38 发布