-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerging-communities.cpp
More file actions
55 lines (50 loc) · 1.17 KB
/
merging-communities.cpp
File metadata and controls
55 lines (50 loc) · 1.17 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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (HackerRank) merging-communities
// Title: Merging Communities
// Link: https://www.hackerrank.com/challenges/merging-communities/problem
// Idea: Use a union-find data structure to represent the communities.
// Difficulty: medium
// Tags: union-find
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> size, rank, p;
UnionFind(int n) : size(n, 1), rank(n, 0), p(n) {
iota(p.begin(), p.end(), 0);
}
int find(int i) { return i == p[i] ? i : (p[i] = find(p[i])); }
void join(int i, int j) {
int x = find(i), y = find(j);
if (x != y) {
if (rank[x] > rank[y]) {
p[y] = x;
size[x] += size[y];
} else {
if (rank[x] == rank[y]) {
++rank[y];
}
p[x] = y;
size[y] += size[x];
}
}
}
};
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n, q, i, j;
string cmd;
cin >> n >> q;
UnionFind uf(n);
while (q--) {
cin >> cmd;
if (cmd == "Q") {
cin >> i;
cout << uf.size[uf.find(i - 1)] << "\n";
} else {
cin >> i >> j;
uf.join(i - 1, j - 1);
}
}
return 0;
}