-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13418.py
59 lines (40 loc) · 1.27 KB
/
13418.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
import sys
def input():
return sys.stdin.readline().rstrip()
def main():
n, m = map(int, input().split())
ascend_list = []
for road in range(m + 1):
a, b, c = map(int, input().split())
ascend_list.append((c, a, b))
ascend_list.sort()
descend_list = ascend_list[:]
descend_list.reverse()
roots = [node for node in range(n + 1)]
min_fatigue = mst_kruskal(descend_list, roots) ** 2
roots = [node for node in range(n + 1)]
max_fatigue = mst_kruskal(ascend_list, roots) ** 2
res = max_fatigue - min_fatigue
print(res)
def mst_kruskal(sorted_list: list, roots: list) -> int:
total_cost = 0
for weight, node_a, node_b in sorted_list:
if find_root(node_a, roots) == find_root(node_b, roots):
continue
if weight == 0:
total_cost += 1
union(node_a, node_b, roots)
return total_cost
def find_root(x: int, roots: list) -> int:
if roots[x] == x:
return x
roots[x] = find_root(roots[x], roots)
return roots[x]
def union(a: int, b: int, roots: list):
root_a = find_root(a, roots)
root_b = find_root(b, roots)
if root_a > root_b:
root_a, root_b = root_b, root_a
roots[root_b] = root_a
if __name__ == '__main__':
main()