[Luogu 1196] NOI2002 银河英雄传说
话说十六年前的 NOI 真简单。。。
我一开始还把题看错了…
啊好像要上课了,一会再写qwq
#include <cstdio>
#include <cstdlib>
const int MAXN=30010;
int n;
class UFS
{
private:
int f[MAXN],sum[MAXN],num[MAXN];
int Find(int x)
{
if(x==f[x])
return x;
int t=Find(f[x]);
sum[x]+=sum[f[x]];
return f[x]=t;
}
public:
UFS(void)
{
for(int i=1;i<=30000;++i)
{
f[i]=i;
sum[i]=0;
num[i]=1;
}
}
void Union(int x,int y)
{
int a=Find(x),b=Find(y);
f[a]=b;
sum[a]+=num[b];
num[b]+=num[a];
num[a]=0;
}
void Query(int x,int y)
{
if(Find(x)^Find(y))
puts("-1");
else
printf("%d\n",abs(sum[x]-sum[y])-1);
}
}S;
int main(int argc,char** argv)
{
scanf("%d",&n);
for(int x,y;n--;)
{
char opt;
scanf("\n%c %d %d",&opt,&x,&y);
if(opt=='M')
S.Union(x,y);
else
S.Query(x,y);
}
return 0;
}
谢谢阅读。