|
- #include <algorithm>
- #include <iostream>
- #include <cmath>
- #include <cstring>
- #include <map>
- #include <string>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <cstdio>
- #include <cstdlib>
- using namespace std;
- int x,y,t,fa[31010],sum[31010],num[31010],all[31010];
- char a[100];
- int getf(int u)
- {
- if(u==fa[u]) return u;
- int k=fa[u];
- fa[u]=getf(fa[u]);
- sum[u]+=sum[k];
- num[u]=num[fa[u]];
- return fa[u];
- }
- int main()
- {
- scanf("%d",&t);
- for(int i=1;i<=30010;i++)
- fa[i]=i,num[i]=1;
- while(t--)
- {
- scanf("%s%d%d",a,&x,&y);
- if(a[0]=='M')
- {
- int t1=getf(x),t2=getf(y);
- fa[t1]=t2;
- sum[t1]+=num[t2];
- num[t1]+=num[t2];
- num[t2]=num[t1];
- }
- else
- {
- if(getf(x)!=getf(y))
- printf("-1\n");
- else
- printf("%d\n",abs(sum[x]-sum[y])-1);
- }
- }
- return 0;
- }
复制代码 |
|