http://www.mokit.top/problem/program/3310
c++代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fx first
#define fy second
#define LL long long
#define ph push_back
#define INF 0x3f3f3f3f
#define PII pair<int,int>
const int N = 3e4 + 5;
int p[N], sz[N], d[N];
int find(int x) {
if (p[x] != x) {
int root = find(p[x]);
d[x] += d[p[x]];
p[x] = root;
}
return p[x];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
for (int i = 1; i < N; i ++) sz[i] = 1, p[i] = i;
int t; cin >> t;
while (t --) {
char op; cin >> op;
int a, b;
if (op == 'M') {
cin >> a >> b;
int pa = find(a), pb = find(b);
d[pb] = sz[pa];
sz[pa] += sz[pb];
p[pb] = pa;
}
else {
cin >> a;
int pa = find(a);
cout << max(0, sz[pa] - d[a] - 1) << endl;
}
}
return 0;
}
进不去啊,能截图发下题面吗
在上课,晚上回去发