forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-good-paths.cpp
61 lines (56 loc) · 1.72 KB
/
number-of-good-paths.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
// Time: O(nlogn)
// Space: O(n)
// tree, sort, union find
class Solution {
public:
int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {
sort(begin(edges), end(edges), [&](const auto& x, const auto& y) {
return max(vals[x[0]], vals[x[1]]) < max(vals[y[0]], vals[y[1]]);
});
int result = size(vals);
UnionFind uf(vals);
for (const auto& e : edges) {
result += uf.union_set(e[0], e[1], max(vals[e[0]], vals[e[1]]));
}
return result;
}
private:
class UnionFind {
public:
UnionFind(const vector<int>& nums)
: set_(size(nums))
, rank_(size(nums))
, cnt_(size(nums)) { // added
iota(set_.begin(), set_.end(), 0);
for (int i = 0; i < size(nums); ++i) {
++cnt_[i][nums[i]]; // added
}
}
int find_set(int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
}
int union_set(int x, int y, int v) { // modified
x = find_set(x), y = find_set(y);
if (x == y) {
return 0; // modified
}
if (rank_[x] > rank_[y]) {
swap(x, y);
}
set_[x] = y; // Union by rank.
if (rank_[x] == rank_[y]) {
++rank_[y];
}
const int cx = cnt_[x][v], cy = cnt_[y][v]; // added
cnt_[y] = {{v, cx + cy}}; // added
return cx * cy; // modified
}
private:
vector<int> set_;
vector<int> rank_;
vector<unordered_map<int, int>> cnt_; // added
};
};