> 希望更丰富的展现?使用Markdown
by Hexarhy @ 2018-11-24 11:04:23
@[2442929200jyh](/space/show?uid=101349) 麻烦您用[markdown](https://www.luogu.org/wiki/show?name=%E5%B8%AE%E5%8A%A9%EF%BC%9Amarkdown)
by SkyLiYu @ 2018-11-24 11:12:35
```cpp
#include<bits/stdc++.h>
using namespace std;
const int maxn=30005;
int fa[maxn],front[maxn],num[maxn];
int find(int x)
{
if(fa[x]==x)return x;
int fy=find(fa[x]);
front[x]+=front[fa[x]];
return fa[x]=fy;
}
void unionn(int x,int y)
{
int fx=find(x),fy=find(y);
front[fx]+=num[fy];
num[fy]+=num[fx];
num[fx]=0;
fa[fx]=fy;
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=30000;i++)
{
fa[i]=i;
num[i]=1;
}
char c;int x,y,fx,fy;
getchar();
while(t--)
{
scanf("%c",&c);
if(c=='M')
{
scanf("%d%d",&x,&y);
unionn(x,y);
}
else{
scanf("%d%d",&x,&y);
fx=find(x),fy=find(y);
if(fx==fy)
printf("%d\n",abs(front[x]-front[y])-1);
else
printf("-1\n");
}
getchar();
}
return 0;
}
```
by 2442929200jyh @ 2018-11-24 11:25:56
谢谢你
by 2442929200jyh @ 2018-11-24 11:27:02
没有人帮我吗?
by 2442929200jyh @ 2018-11-27 16:00:15