- 银河英雄传说
- 2013-10-07 14:25:03 @
为何全部re啊啊啊!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
long f[30000],s[30000],sum[30000];
long getfather(long v){
if (f[v]==v) return v;
long p=getfather(f[v]);
s[v]+=s[f[v]];
f[v]=p;
return f[v];
}
int main(){
int num,a,b; char ch;
scanf("%d",&num);
for(int i=0;i<30000;i++) {s[i]=0; sum[i]=1; f[i]=i;}
for(int i=0;i<num;i++){
scanf("%c %d %d\n",&ch,&a,&b);
int f1=getfather(a),f2=getfather(b);
if(ch=='M'){
s[f1]+=sum[f2];
sum[f2]+=sum[f1];
f[f1]=f2;
}else if(ch=='C'){
if(f1!=f2) printf("-1\n");
else printf("%d\n",abs(s[a]-s[b])-1);
}
}
return 0;
}