forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
81 lines (67 loc) · 1.69 KB
/
solution.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
//declare a disjoint union class
class dsu {
public:
//p[x] stores parent of x
//size[x] stores the current size of connected componet of which x is a part;
vector<int> p, size;
//define a constructor
dsu(int _n) {
p.resize(_n);
//initialise sizes of all components as one
size.assign(_n, 1);
//initially, p[x] = x for all x
iota(p.begin(), p.end(), 0);
}
//function to get parent of some node x (use path compression);
inline int get(int x) {
return (x == p[x] ? x : (p[x] = get(p[x])));
}
//function to unite two sets
void unite(int x, int y) {
//unite parents of x and y
x = get(x);
y = get(y);
if(x!=y) {
//Assign bigger component to be parent of smaller component
if(size[x] >= size[y]) {
p[y] = x;
size[x] += size[y];
} else {
p[x] = y;
size[y] += size[x];
}
}
}
//function to check if x and y are part of same component
inline bool belong(int x, int y) {
return get(x) == get(y);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
//take input of nuber of people and number of queries
int n, q;
cin >> n >> q;
//declare a disjoint set data structure
dsu d(n+1);
//take input of queries
while(q--) {
char op;
cin >> op;
if(op == 'Q') {
//if query of is of this type, print size of community of which x is a part
int x;
cin >> x;
cout << d.size[d.get(x)] << '\n';
} else {
//else unite x and y
int x, y;
cin >> x >> y;
d.unite(x, y);
}
}
return 0;
}