-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLC1319.cpp
executable file
·60 lines (54 loc) · 1.13 KB
/
LC1319.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
/*
Problem Statement: https://leetcode.com/problems/number-of-operations-to-make-network-connected/
Time: O(n + m • log n), amortized O(n + m)
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class UnionFind {
private:
int m_components;
vector<int> link, size;
public:
UnionFind(int n) : m_components(n), link(n), size(n, 1) {
iota(link.begin(), link.end(), 0);
}
int find(int x) {
int y = x;
// find the root of component
while (x != link[x])
x = link[x];
// path compression
while (y != x)
y = std::exchange(link[y], x);
return x;
}
bool same(int a, int b) {
return find(a) == find(b);
}
void unify(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (size[a] < size[b])
swap(a, b);
link[b] = a;
size[a] += size[b];
m_components--;
}
int components() {
return m_components;
}
};
class Solution {
public:
int makeConnected(int n, vector<vector<int>>& connections) {
int m = connections.size();
if (m < n - 1)
return -1;
UnionFind dsu(n);
for (vector<int>& conn: connections)
dsu.unify(conn[0], conn[1]);
return dsu.components() - 1;
}
};