RE求条
查看原帖
RE求条
683212
Henly_Z楼主2024/9/2 20:14
#include<bits/stdc++.h>
using namespace std;
const int N = 1000005;
int f[N] , sum[N]  , num[N];
int n;
void init(){
	for(int i = 1 ; i <= 300000 ; i++){
		f[i] = i;
	}
}
int find(int x , int yuan){
	if(f[x] == x) return x;
	num[yuan]++;
	return find(f[x] , yuan);
	
}
int merge(int x , int y){
	int cx = find(x,x) , cy = find(y,y);
	if(cx != cy) f[cy] = cx;
}
int main(){
	cin >> n;
	init();
	for(int i = 1 ; i <= n ; i++){
		char a;
		int b , c;
		cin >> a >> b >> c;
		if(a=='M'){
			merge(b,c);
		}
		else{
			int fx = find(b,b);
			int fy = find(c,c);
//			cout << fx << " " << fy << endl; 
			if(fx != fy) cout << -1 << endl;
			else cout << abs(num[b] - num[c]) << endl;
		}
	}
}
2024/9/2 20:14
加载中...