比赛 THUPC 2025 pre 评测结果 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
题目名称 骑行计划 最终得分 0
用户昵称 梦那边的美好CE 运行时间 0.142 s
代码语言 C++ 内存使用 4.74 MiB
提交时间 2026-01-29 19:06:09
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;

int t;
int fa[33333],siz[33333],pos[33333];

int fd(int x){
    if(x==fa[x]) return x;
    int f=fa[x];
    fa[x]=fd(f);
    pos[x]+=pos[f];
    return fa[x];
}

signed main(){
	freopen("galaxy.in","r",stdin);freopen("galaxy.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);

    for(int i=1;i<=30000;i++){
        fa[i]=i;siz[i]=1;pos[i]=0;
    }

    cin>>t;
    while(t--){
        char op;
        int x,y;
        cin>>op>>x>>y;
        int rx=fd(x),ry=fd(y);

        if(op=='M'){
            if(rx!=ry){
                fa[rx]=ry;
                pos[rx]=siz[ry];
                siz[ry]+=siz[rx];
            }
        }else{
            if(rx!=ry){
                cout<<"-1\n";
            }else{
                cout<<abs(pos[x]-pos[y])-1<<"\n";
            }
        }
    }
    return 0;
}