2 条题解

  • 1
    @ 2025-10-18 18:17:47
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    const int MAXN = 3e4+5;
    int fa[MAXN], d[MAXN], s[MAXN];
    int find(int x){
    	if(x == fa[x])
    		return x;
    	int t = find(fa[x]);
    	d[x] += d[fa[x]];
    	return fa[x] = t;
    }
    signed main(){
    	int t;cin>>t;
    	for(int i = 1; i <= 30000; ++ i)
    		fa[i] = i, s[i] = 1;
    	while(t --){
    		char op;
    		cin >> op;
    		int u, v;
    		cin >> u >> v;
    		int tu = find(u), tv = find(v);
    		if(op == 'M'){
    			d[tu] += s[tv];
    			fa[tu] = tv;
    			s[tv] += s[tu];
    			s[tu] = 0;
    		}
    		if(op == 'C'){
    			if(tu != tv)
    				cout << "-1\n";
    			else
    				cout << abs(d[u] - d[v]) - 1 << "\n";
    		}
    	}
    }
    

    信息

    ID
    5254
    时间
    1000ms
    内存
    128MiB
    难度
    6
    标签
    递交数
    57
    已通过
    23
    上传者