-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#1489 findCriticalAndPseudoCriticalEdges.py
83 lines (60 loc) · 2.07 KB
/
#1489 findCriticalAndPseudoCriticalEdges.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
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
class UF:
def __init__(self, size):
self.p = {i: i for i in range(size)}
self.count = 0
def find(self, x):
if x != self.p[x]:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, x, y):
xp, yp = self.find(x), self.find(y)
if xp == yp:
return False
self.p[xp] = yp
self.count += 1
return True
class Solution:
def findCriticalAndPseudoCriticalEdges(self, n: int, edges: List[List[int]]) -> List[List[int]]:
# Sort edges by increasing cost and keeping the original index
sorted_edges = sorted(zip(edges, range(len(edges))), key=lambda x: x[0][2])
def get_mst_without_edge(edge: int) -> int:
s = UF(n)
ind = 0
res = 0
# While not a full spanning tree
while ind < len(edges) and s.count != n - 1:
# Add an edge
(u, v, c), i = sorted_edges[ind]
if edge != i:
if s.union(u, v):
res += c
ind += 1
return res if s.count == n - 1 else inf
def get_mst_with_edge(edge: int) -> int:
s = UF(n)
ind = 0
u, v, c = edges[edge]
s.union(u, v)
res = c
# While not a full spanning tree
while s.count < n - 1:
# Add an edge
(u, v, c), i = sorted_edges[ind]
if edge != i:
if s.union(u, v):
res += c
ind += 1
return res
# Get minimum possible cost
min_cost = get_mst_without_edge(-1)
critical = []
p_critical = []
for i in range(len(edges)):
cost = get_mst_without_edge(i)
if cost > min_cost:
critical.append(i)
else:
cost_with = get_mst_with_edge(i)
if cost_with == min_cost:
p_critical.append(i)
return [critical, p_critical]