带权并查集,AC on #9&#10,其他全WA,求助!
查看原帖
带权并查集,AC on #9&#10,其他全WA,求助!
443675
紊莫turtle楼主2022/6/10 13:37
#include <bits/stdc++.h>
using namespace std;
int f[30005],d[30005],l[30005];
int find(int x)
{
	if(f[x]==x) return x;
	f[x]=find(f[x]);
	d[x]+=d[f[x]];
	return f[x];
}
int main()
{
//	freopen("P5092_1.in","r",stdin);
//	freopen("P5092_1.ans","w",stdout);
	for(int i=1;i<=30002;i++) f[i]=i,l[i]=1;
	int p;cin>>p;
	while(p--)
	{
		char op;cin>>op;
		if(op=='M')
		{
			int x,y;cin>>x>>y;
			int fx=find(x),fy=find(y);
			f[fx]=fy;
			d[fx]+=l[fy];
			l[fy]+=l[fx];
		}
		else 
		{
			int x;cin>>x; 
			cout<<d[x]<<'\n';
		}
	}
	return 0;
}

代码见上,思路和tj①差不多。但是18pts

2022/6/10 13:37
加载中...