大佬求助 全RE 啊啊啊啊啊·
查看原帖
大佬求助 全RE 啊啊啊啊啊·
921657
_Submerge_楼主2023/12/27 17:00
#include<bits/stdc++.h>
using namespace std;
char c;
int n,x,y,f[30010],len[30010],dis[30010];
int find(int x)
{
	if(f[x]==x)
	{
		return x;
	}
	else
	{
		int t=f[x];
		f[x]=find(f[x]);
		dis[x]=dis[x]+dis[t];
		return f[x];
	}
}
int merge(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		f[fx]=fy;
		dis[fx]=dis[fx]+len[fy];
		len[fy]=len[fy]+len[fx];
		len[fx]=0;
	}
}
int main()
{
	for(int i=1;i<=30000;i++)
	{
		f[i]=i;
		len[i]=1;
	}
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>c;
		if(c=='M')
		{
			cin>>x>>y;
			merge(x,y);
		}
		else if(c=='C')
		{
			cin>>x;
			int a=find(x);
			cout<<dis[x]<<endl;
		}
	}
	return 0;
}

2023/12/27 17:00
加载中...