-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2421.cpp
More file actions
58 lines (51 loc) · 1.51 KB
/
Copy path2421.cpp
File metadata and controls
58 lines (51 loc) · 1.51 KB
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
class UnionFind {
int n, cnt = n;
vector<int> root, size;
public:
UnionFind(int n) : n(n), root(n), size(n, 1) { 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) {
x = find(x), y = find(y);
if (x != y) {
if (size[x] > size[y]) swap(x, y);
root[x] = y;
size[y] += size[x];
cnt--;
}
}
};
class Solution {
public:
int numberOfGoodPaths(vector<int> &vals, vector<vector<int>> &edges) {
int n = vals.size();
map<int, vector<int>> valuesToNodes;
vector<vector<int>> adj(n);
UnionFind uf(n);
for (auto &edge : edges) {
adj[edge[0]].push_back(edge[1]);
adj[edge[1]].push_back(edge[0]);
}
for (int i = 0; i < n; i++)
valuesToNodes[vals[i]].push_back(i);
int goodPaths = 0;
for (auto &[value, nodes] : valuesToNodes) {
for (int node : nodes) {
for (int neighbor : adj[node]) {
if (vals[node] >= vals[neighbor]) {
uf.join(node, neighbor);
}
}
}
unordered_map<int, int> group;
for (int u : nodes)
group[uf.find(u)]++;
for (auto &[_, size] : group)
goodPaths += (size * (size + 1) / 2);
}
return goodPaths;
}
};