-
Notifications
You must be signed in to change notification settings - Fork 3
/
1319.cpp
46 lines (46 loc) · 1.11 KB
/
1319.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
class DisjointSet {
public:
vector<int> parent;
vector<int> rank;
int groups;
DisjointSet(int n) {
parent.resize(n, 0);
for (int i = 0; i < n; ++i) parent[i] = i;
rank.resize(n, 0);
groups = n;
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void join(int x, int y) {
int pX = find(x);
int pY = find(y);
if (pX == pY) return;
groups--;
if (rank[pX] > rank[pY]) {
parent[pY] = pX;
}
else if (rank[pX] < rank[pY]) {
parent[pX] = pY;
}
else {
parent[pY] = pX;
rank[pX]++;
}
}
};
class Solution {
public:
int makeConnected(int n, vector<vector<int>>& connections) {
int m = connections.size();
if (m < n - 1) return -1;
DisjointSet* disjointSet = new DisjointSet(n);
for (auto& connection : connections) {
disjointSet->join(connection[0], connection[1]);
}
return disjointSet->groups - 1;
}
};