Skip to content

Commit 092499a

Browse files
committed
πŸ‘‘ G4-1647
1 parent 1acdaf1 commit 092499a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

β€ŽπŸ‘‘ G4/1647.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
n, m = map(int, input().split())
2+
edges = [list(map(int, input().split())) for _ in range(m)]
3+
edges.sort(key=lambda x:x[2])
4+
5+
parent = [i for i in range(n+1)]
6+
mst = []
7+
8+
def find(x):
9+
if x == parent[x]: return x
10+
parent[x] = find(parent[x])
11+
return parent[x]
12+
13+
def union(x, y):
14+
x = find(x)
15+
y = find(y)
16+
if x != y:
17+
parent[x] = y
18+
19+
for a, b, c in edges:
20+
if len(mst) == n-2: break
21+
if find(a) == find(b): continue
22+
mst.append(c)
23+
union(a, b)
24+
25+
print(sum(mst))

0 commit comments

Comments
 (0)