题目
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=31000;
int f[MAXN],min1;
int front[MAXN],behind[MAXN];
int find(int x)
{
if(f[x]==x) return x;
int fx=find(f[x]);
front[x]+=front[f[x]];
return f[x]=fx;
}
int main()
{
int t;
cin>>t;
for(int i=1;i<=30000;i++)
{
f[i]=i;
behind[i]=1;
}
for(int i=1;i<=t;i++)
{
int x,y;
char c1[10];
scanf("%s%d%d",c1,&x,&y);
if(c1[0]=='M')
{
int fx=find(x);
int fy=find(y);
front[fx]=behind[fy];
behind[fy]+=behind[fx];
f[fx]=fy;
}
else
{
int ans=-1;
int fx=find(x);
int fy=find(y);
if(fx==fy) ans=abs(front[x]-front[y])-1;
printf("%d\n",ans);
}
}
return 0;
}