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 1acdaf1 commit 092499aCopy full SHA for 092499a
βπ G4/1647.py
@@ -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