-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveMaxNumberofEdgestoKeepGraphFullyTraversable.cpp
More file actions
54 lines (47 loc) · 1.19 KB
/
removeMaxNumberofEdgestoKeepGraphFullyTraversable.cpp
File metadata and controls
54 lines (47 loc) · 1.19 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
// Source: https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/
// Author: Miao Zhang
// Date: 2021-05-18
class UnionFind {
public:
UnionFind(int n): p_(n), edges_(0) {
iota(begin(p_), end(p_), 0);
}
int find(int x) {
if (p_[x] != x) {
return p_[x] = find(p_[x]);
}
return p_[x];
}
int merge(int x, int y) {
int px = find(x);
int py = find(y);
if (px == py) return 1;
p_[px] = py;
edges_++;
return 0;
}
int edges() const {
return edges_;
}
private:
vector<int> p_;
int edges_;
};
class Solution {
public:
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
int res = 0;
UnionFind a(n + 1), b(n + 1);
for (const auto& e: edges) {
if (e[0] != 3) continue;
res += a.merge(e[1], e[2]);
b.merge(e[1], e[2]);
}
for (const auto& e: edges) {
if (e[0] == 3) continue;
UnionFind& uf = e[0] == 1 ? a : b;
res += uf.merge(e[1], e[2]);
}
return (a.edges() == n - 1 && b.edges() == n - 1) ? res : -1;
}
};