#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int a[N],js[N];
int dist[N];
int find(int x){
if(x!=a[x]){
int fa=find(a[x]);
dist[x]+=dist[a[x]];
a[x]=fa;
}
return a[x];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int t;
cin>>t;
for(int i=0;i<=N-9;i++){
js[i]=1;
dist[i]=0;
a[i]=i;
}
while(t--){
string s;
int x,y;
cin>>s>>x>>y;
int fx=find(x),fy=find(y);
if(s[0]=='M'){
dist[fx]+=js[fy];
a[fx]=fy;
js[fy]+=js[fx];
js[fx]=0;
}else{
if(fx==fy) cout<<abs(dist[x]-dist[y])-1<<endl;
else cout<<-1<<endl;
}
}
}
洛谷上过了,但是acwing上挂了。 有几篇题解也跟着一起挂了。