大佬求助!!!
查看原帖
大佬求助!!!
856459
yangjunhan1楼主2023/6/24 14:47
#include<bits/stdc++.h>
using namespace std;
int t,b[1000000],s[1000000];
int fsb(int x){
	if(b[x]==x)	return x;
	return fsb(b[x]);
}
int fss(int x){
	if(s[x]==x)	return x;
	return fss(s[x]);
}
int gs(int x,int y){
	int xx=x,yy=y,s=-1;
	while(b[yy]!=yy){
		if(yy==x)	return s;
		s++;
		yy=b[yy];
	}
	s=-1;
	while(b[xx]!=xx){
		if(xx==y)	return s;
		s++;
		xx=b[xx];
	}
}
int main(){
	cin>>t;
	for(int i=1;i<=30000;i++)	b[i]=i,s[i]=i;
	while(t--){
		char p;
		int x,y;
		cin>>p>>x>>y;
		if(p=='M'){
			int xx=fsb(x),yy=fss(y);
			b[xx]=yy,s[yy]=xx;
		}
		else{
			if(fsb(x)==fsb(y) && fss(x)==fss(y))	cout<<gs(x,y)<<endl;
			else	cout<<-1<<endl;
		}
	}
	return 0;
}
2023/6/24 14:47
加载中...