全wa,显示too long line on
查看原帖
全wa,显示too long line on
690320
MoGuYun_12楼主2024/8/23 02:52
#include <bits/stdc++.h>

using namespace std;

const int N = 30005;
int T;
int fa[N],siz[N],d[N];

int getf(int x){
	if(fa[x]==x) return fa[x];
	int root=getf(fa[x]);
	d[x]+=d[fa[x]];
	return fa[x]=root;
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>T;
    for(int i=1;i<=N;i++) fa[i]=i,siz[i]=1;
    while(T--){
    	char c; int x,y;
	cin>>c>>x>>y;
    	if(c=='M'){
    		int fx=getf(x),fy=getf(y);
    		fa[fx]=fy,d[fx]=siz[fy];
    		siz[fy]+=siz[fx];
		}else{
			int fx=getf(x),fy=getf(y);
			if(fx==fy) cout<<abs(d[x]-d[y])-1<<endl;
			else cout<<-1<<endl;
		}
	}
	return 0;
}
2024/8/23 02:52
加载中...