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 092499a commit f4e42a0Copy full SHA for f4e42a0
βπ G3/4386.py
@@ -0,0 +1,30 @@
1
+n = int(input())
2
+points = [list(map(float, input().split())) for _ in range(n)]
3
+parent = [i for i in range(n)]
4
+mst = []
5
+edges = []
6
+for i in range(n-1):
7
+ for j in range(i+1, n):
8
+ distance = (points[i][0]-points[j][0])**2 + (points[i][1]-points[j][1])**2
9
+ distance = distance**0.5
10
+ edges.append((distance, i, j))
11
+edges.sort(key=lambda x:x[0])
12
+
13
+def find(x):
14
+ if x == parent[x]: return x
15
+ parent[x] = find(parent[x])
16
+ return parent[x]
17
18
+def union(x, y):
19
+ x = find(x)
20
+ y = find(y)
21
+ if x != y:
22
+ parent[x] = y
23
24
+for c, a, b in edges:
25
+ if find(a) == find(b): continue
26
+ union(a, b)
27
+ mst.append(c)
28
+ if len(mst) == n-1: break
29
30
+print(sum(mst))
0 commit comments