记录编号 462816 评测结果 AAAAAAAAAA
题目名称 [NOI 2002]银河英雄传说 最终得分 100
用户昵称 GravatarFisher. 是否通过 通过
代码语言 C++ 运行时间 0.498 s
提交时间 2017-10-23 11:04:33 内存使用 0.66 MiB
显示代码纯文本
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
inline int read(){
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}
const int maxn=30010;
int n;
struct dd{
	int fa,top,sz;
}f[maxn];
inline void init(){
	for(int i=1;i<=30000;i++){
		f[i].fa=i;
		f[i].sz=1;
		f[i].top=1;
	}
}
inline int find(int x){
	if(f[x].fa==x)return x;
	int y=f[x].fa;
	f[x].fa=find(f[x].fa);
	f[x].top=f[y].top+f[x].top-1;
	return f[x].fa;
}
char cz[10];
inline void work(){
	for(int i=1;i<=n;i++){
		scanf("%s",cz);
		int x=read(),y=read();
		if(cz[0]=='M'){
			int u=find(x),v=find(y);
			//cout<<cz<<" "<<x<<" "<<y<<" "<<u<<" "<<v<<endl;
			f[u].fa=v;
			f[u].top=f[v].sz+1;
			f[v].sz+=f[u].sz;
		}
		else{
			int u=find(x),v=find(y);
			//cout<<cz<<" "<<x<<" "<<y<<" "<<u<<" "<<v<<endl;
			if(u!=v)puts("-1");
			else{
				printf("%d\n",abs(f[x].top-f[y].top)-1);
			}
		}
	}
}
int main(){
	freopen("galaxy.in","r",stdin);
	freopen("galaxy.out","w",stdout);
	n=read();
	init();
	work();
	return 0;
}