首页 > 其他分享 >P1196&&P5092

P1196&&P5092

时间:2022-09-30 10:03:25浏览次数:50  
标签:return 30001 int fa xx && P5092 find P1196

#include<bits/stdc++.h>
using namespace std;
//边带权并查集
class solve{
	public:
		int T;
		int fa[30001];
		int D[30001];
		int d[30001];
		int find(int x)
		{
			if(fa[x]==x)
			{
				return x;
			}
			else
			{
				int xx=find(fa[x]);
				d[x]+=d[fa[x]];
				fa[x]=xx;
				return fa[x];
			}
		}
		void work()
		{
			for(int i=1; i<=30000; i++)
			fa[i]=i,D[i]=1,d[i]=0;
			cin>>T;
			for(int i=1; i<=T; i++)
			{
				char op;
				cin>>op;
				int x,y;
				cin>>x;
				if(op=='M')
				{
					cin>>y;
					x=find(x);
					y=find(y);
					d[x]+=D[y];
					D[y]+=D[x];
					fa[x]=y;
				}
				else
				{
					find(x);
					cout<<d[x]<<endl;
					/*  P1196 [NOI2002] 银河英雄传说
					if(find(x)!=find(y))cout<<"-1"<<endl;
					else
					{
						cout<<abs(d[x]-d[y])-1<<endl;
					}
					*/
				}
			}
		}
}x;
int main()
{
	x.work();
}

标签:return,30001,int,fa,xx,&&,P5092,find,P1196
From: https://www.cnblogs.com/dadidididi/p/16743901.html

相关文章