-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1579.RemoveMaxNumberOfEdgesToKeepGraphFullyTraversable.cpp
More file actions
61 lines (48 loc) · 1.3 KB
/
1579.RemoveMaxNumberOfEdgesToKeepGraphFullyTraversable.cpp
File metadata and controls
61 lines (48 loc) · 1.3 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
59
60
61
class Solution {
public:
struct UnionFind { // simple version for UnionFind class
vector<int> m_root;
int m_components;
UnionFind(int n) : m_root(n + 1), m_components(n) {
iota(m_root.begin(), m_root.end(), 0);
}
const int find(int x) {
if (x == m_root[x]) return x;
return m_root[x] = find(m_root[x]);
}
const bool unificate(int x, int y) {
// Check x & y.
x = find(x), y = find(y);
if (x == y) return 0;
// Switchies.
m_root[y] = x;
m_components--;
return 1;
}
const bool isConnected() const {
return m_components == 1;
}
};
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Process type 3 edges first
UnionFind alice(n), bob(n);
int edgesNeeded = 0;
for (vector<int>& e : edges)
if (e[0] == 3)
edgesNeeded += alice.unificate(e[1], e[2]) | bob.unificate(e[1], e[2]);
// Process type 1 and type 2 edges.
for (vector<int>& e : edges) {
if (e[0] == 1) edgesNeeded += alice.unificate(e[1], e[2]);
else if (e[0] == 2) edgesNeeded += bob.unificate(e[1], e[2]);
}
// Return edges that can be removed.
if (alice.isConnected() && bob.isConnected())
return edges.size() - edgesNeeded;
// Graph isn't valid.
return -1;
}
};