1 条题解
-
3
#include <bits/stdc++.h> using namespace std; #define LL long long namespace Online { constexpr int N = 3e4 + 5; int father[N]; int size1[N], d[N]; inline int find(int x) { if (x == father[x]) return x; int root = find(father[x]); d[x] += d[father[x]]; father[x] = root; return father[x]; } inline void merge(int a, int b) { a = find(a), b = find(b); d[a] = size1[b]; size1[b] += size1[a]; father[a] = b; } inline void NaCl(void) { int t; cin >> t; for (int i = 1; i <= (N - 5); ++i) father[i] = i, size1[i] = 1, d[i] = 0; for (; t--;) { char c; int x, y; cin >> c >> x >> y; if (c == 'M') { merge(x, y); } else { int a = find(x); int b = find(y); if (a == b) { LL res = abs(d[x] - d[y]) - 1; cout << max(res, 0LL) << '\n'; } else cout << -1 << '\n'; } } } } auto main(void) -> signed { cin.tie(nullptr)->sync_with_stdio(false); Online::NaCl(); }
- 1
信息
- ID
- 5317
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 6
- 标签
- 递交数
- 160
- 已通过
- 47
- 上传者