菜鸟刚学并查集保灵求助QAQ
查看原帖
菜鸟刚学并查集保灵求助QAQ
298549
SIXIANG32楼主2020/5/9 13:09
#include<iostream>
#include<cmath>
using namespace std;
int fa[100000],siz[100000],dis[100000];
int Find(int x)
{
	if(fa[x]==x)return x;
    dis[x]+=dis[fa[x]];
	return fa[x]=Find(fa[x]);
} 
void Union(int u,int v)
{
	int a=Find(u),b=Find(v),t;
	if(siz[a]>siz[b])t=a,a=b,b=t;
    dis[a]+=siz[b],fa[a]=b,siz[a]+=siz[b];
}
int main()
{
	int n,m,a,b,a2,a3;
    char a1;
	cin>>n;
	for(int p=1;p<=30000;p++)fa[p]=p,siz[p]=1;
	for(int p=1;p<=n;p++)
	{
		cin>>a1>>a2>>a3;
		if(a1=='M')
			Union(a2,a3);
		if(a1=='C')
			if(Find(a2)!=Find(a3))cout<<-1<<endl;
			else cout<<abs(dis[a2]-dis[a3])-1<<endl;
	}
	return 0;
}

0分求助QAQ,全WA了

2020/5/9 13:09
加载中...