/
30: 1579. Remove Max Number of Edges to Keep Graph Fully Traversable.py
56 lines (47 loc) · 1.57 KB
/
30: 1579. Remove Max Number of Edges to Keep Graph Fully Traversable.py
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
# problem:
# https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/
# solution:
class UnionFind:
def __init__(self, n):
self.root = list(range(n))
self.rank = [1] * n
self.groups = n - 1
def find(self, x):
while x != self.root[x]:
self.root[x] = self.root[self.root[x]]
x = self.root[x]
return self.root[x]
def union(self, x, y):
px, py = self.find(x), self.find(y)
if px == py:
return 0
if self.rank[px] <= self.rank[py]:
self.root[px] = py
self.rank[py] += self.rank[px]
elif self.rank[px] > self.rank[py]:
self.root[py] = px
self.rank[px] += self.rank[py]
self.groups -= 1
return 1
class Solution:
def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:
uf = UnionFind(n+1)
alice, bob, common = 1, 2, 3
output = 0
graph = defaultdict(list)
for t, a, b in edges:
graph[t].append((a, b))
# Common Graph
for u, v in graph[common]:
if uf.union(u, v) == 0:
output += 1
uf_alice, uf_bob = deepcopy(uf), deepcopy(uf)
# Alice
for u, v in graph[alice]:
if uf_alice.union(u, v) == 0:
output += 1
# Bob
for u, v in graph[bob]:
if uf_bob.union(u, v) == 0:
output += 1
return -1 if uf_bob.groups > 1 or uf_alice.groups > 1 else output