-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.cpp
69 lines (60 loc) · 1.29 KB
/
main.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
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int countComponents(int n, vector<pair<int, int>>& edges) {
UnionFind uf(n);
for (const auto& e : edges) {
uf.union_set(e.first, e.second);
}
return uf.length();
}
private:
class UnionFind {
private:
vector<int> parent;
int count;
public:
UnionFind(const int n) {
parent = vector<int>(n, 0);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
count = n;
}
int find_set(const int x) {
if (parent[x] != x) {
parent[x] = find_set(parent[x]); // Path compression.
}
return parent[x];
}
void union_set(const int x, const int y) {
int x_root = find_set(x), y_root = find_set(y);
if (x_root != y_root) {
parent[min(x_root, y_root)] = max(x_root, y_root);
--count;
}
}
int length() const {
return count;
}
};
};
int main() {
Solution sol;
vector<pair<int,int>> edges = {{0, 1}, {1, 2}, {3, 4}};
cout << sol.countComponents(5, edges) << endl;
return 0;
}