We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7f0243b commit fb5618cCopy full SHA for fb5618c
👑 G4/6497.py
@@ -0,0 +1,33 @@
1
+import sys
2
+input = sys.stdin.readline
3
+sys.setrecursionlimit(10**6)
4
+
5
+while True:
6
+ m, n = map(int, input().split())
7
+ if m == 0 and n == 0: break
8
+ edges = []
9
+ mst = []
10
+ for _ in range(n):
11
+ x, y, z = map(int, input().split())
12
+ edges.append([z, x, y])
13
+ edges.sort(key=lambda x:x[0])
14
15
+ parent = [i for i in range(m)]
16
17
+ def find(x):
18
+ if x == parent[x]: return x
19
+ parent[x] = find(parent[x])
20
+ return parent[x]
21
22
+ def union(x, y):
23
+ x = find(x)
24
+ y = find(y)
25
+ if x != y: parent[x] = y
26
27
+ for z, x, y in edges:
28
+ if find(x) == find(y): continue
29
+ mst.append(z)
30
+ union(x, y)
31
+ if len(mst) == m-1: break
32
33
+ print(sum([i[0] for i in edges]) - sum(mst))
0 commit comments