-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1579.cpp
More file actions
executable file
·87 lines (77 loc) · 1.54 KB
/
LC1579.cpp
File metadata and controls
executable file
·87 lines (77 loc) · 1.54 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Problem Statement: https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/
Time: O(n² • log n), amortized O(n²)
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class UnionFind {
private:
int m_components;
vector<int> link;
public:
UnionFind(int n) : m_components(n), link(n) {
iota(link.begin(), link.end(), 0);
}
int components() {
return m_components;
}
int find(int x) {
int y = x;
// find the root of component
while (x != link[x])
x = link[x];
// path compression
while (y != x)
y = exchange(link[y], x);
return x;
}
bool same(int a, int b) {
return find(a) == find(b);
}
void unify(int a, int b) {
a = find(a);
b = find(b);
// belong to the same component
if (a == b)
return;
link[b] = a;
m_components--;
}
};
class Solution {
public:
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
int cnt = 0;
UnionFind dsu_a(n + 1), dsu_b(n + 1);
for (vector<int>& e: edges) {
int t, u, v;
tie(t, u, v) = {e[0], e[1], e[2]};
if (t != 3)
continue;
if (dsu_a.same(u, v))
cnt++;
else {
dsu_a.unify(u, v);
dsu_b.unify(u, v);
}
}
for (vector<int>& e: edges) {
int t, u, v;
tie(t, u, v) = {e[0], e[1], e[2]};
if (t == 1) {
if (dsu_a.same(u, v))
cnt++;
else
dsu_a.unify(u, v);
} else if (t == 2) {
if (dsu_b.same(u, v))
cnt++;
else
dsu_b.unify(u, v);
}
}
if (dsu_a.components() > 2 || dsu_b.components() > 2)
return -1;
return cnt;
}
};