#include<bits/stdc++.h>
using namespace std;
const int N=3e4+5;
int T,s[N],c[N],siz[N];
int find_set(int x){
if(x!=s[x]){
int t=find_set(s[x]);
c[x]+=c[s[x]];
s[x]=t;
}
return s[x];
}
void merge_set(int x,int y){
x=find_set(x);y=find_set(y);
if(x!=y){
s[x]=y;
c[x]=c[y]+siz[y];
siz[y]+=siz[x];
}
}
int main(){
cin>>T;
for(int i=1;i<=N;i++){
s[i]=i;
c[i]=0;
siz[i]=1;
}
char op;
int x,y;
while(T--){
cin>>op>>x>>y;
if(op=='M'){
merge_set(x,y);
continue;
}else{
if(find_set(x)!=find_set(y)){
printf("-1\n");
}else{
printf("%d\n",abs(c[x]-c[y])-1);
}
}
}
return 0;
}
c数组是当前节点到代表元的距离。 下载了洛谷第一个点的数据在本地测了是对的,但评测一个点都没过,实在不知道哪里错了,麻烦大佬帮忙看一下,谢谢!