-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0238.h
More file actions
68 lines (59 loc) · 1.43 KB
/
Problem0238.h
File metadata and controls
68 lines (59 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Created by Fengwei Zhang on 9/20/21.
//
#ifndef ACWINGSOLUTION_PROBLEM0238_H
#define ACWINGSOLUTION_PROBLEM0238_H
#include <iostream>
#include <cmath>
using namespace std;
class Problem0238 {
private:
static const int N = 30010;
int clsSize[N], dist[N], parent[N];
int find_root(int x) {
if (x != parent[x]) {
int root = find_root(parent[x]);
dist[x] += dist[parent[x]];
parent[x] = root;
}
return parent[x];
}
void merge_sets(int a, int b) {
auto pa = find_root(a);
auto pb = find_root(b);
if (pa == pb) {
return;
}
dist[pa] = clsSize[pb];
clsSize[pb] += clsSize[pa];
parent[pa] = pb;
}
int querySets(int a, int b) {
auto pa = find_root(a);
auto pb = find_root(b);
if (pa == pb) {
return max(abs(dist[a] - dist[b]) - 1, 0);
}
return -1;
}
int main() {
for (int i = 0; i < N; ++i) {
parent[i] = i;
clsSize[i] = 1;
}
int n;
scanf("%d", &n);
char op[2];
int a, b;
for (int i = 0; i < n; ++i) {
scanf("%s%d%d", op, &a, &b);
if (op[0] == 'M') {
merge_sets(a, b);
} else {
printf("%d\n", querySets(a, b));
}
}
return 0;
}
};
#endif //ACWINGSOLUTION_PROBLEM0238_H