Skip to content

Commit fb5618c

Browse files
committed
👑 G4-6497
1 parent 7f0243b commit fb5618c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

👑 G4/6497.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)