-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2492.cpp
More file actions
36 lines (31 loc) · 853 Bytes
/
Copy path2492.cpp
File metadata and controls
36 lines (31 loc) · 853 Bytes
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
class UnionFind {
int n;
vector<int> root, rank, res;
public:
UnionFind(int n) : n(n), root(n), rank(n, 1), res(n, INT_MAX) { iota(root.begin(), root.end(), 0); }
int find(int x) {
while (x != root[x])
x = root[x] = root[root[x]];
return x;
}
void join(int x, int y, int val) {
x = find(x), y = find(y);
if (x != y) {
if (rank[x] > rank[y]) swap(x, y);
res[y] = min(res[x], res[y]);
root[x] = y;
rank[y] += rank[x];
}
res[y] = min(val, res[y]);
}
int mini(int x) { return res[find(x)]; }
};
class Solution {
public:
int minScore(int n, vector<vector<int>> &roads) {
UnionFind uf(n + 1);
for (auto &r : roads)
uf.join(r[0], r[1], r[2]);
return uf.mini(n);
}
};