Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 605f096

Browse files
Merge pull request #589 from JH-TT/BOJ_4386
별자리 만들기
2 parents 5db8fa5 + 155ceea commit 605f096

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎JongHo/BOJ/4386.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import math
2+
import sys
3+
input = sys.stdin.readline
4+
5+
def find_parent(parent, x):
6+
if parent[x] != x:
7+
parent[x] = find_parent(parent, parent[x])
8+
return parent[x]
9+
10+
def union_parent(parent, a, b):
11+
a = find_parent(parent, a)
12+
b = find_parent(parent, b)
13+
14+
if a < b:
15+
parent[b] = a
16+
else:
17+
parent[a] = b
18+
19+
n = int(input())
20+
21+
star = [[0, 0]]
22+
parent = list(range(n + 1))
23+
edge = []
24+
25+
for _ in range(n):
26+
star.append(list(map(float, input().split())))
27+
28+
for i in range(1, n + 1):
29+
for j in range(i + 1, n + 1):
30+
edge.append([round(math.sqrt((star[i][0]-star[j][0])**2 + (star[i][1]-star[j][1])**2), 2), i, j])
31+
32+
edge.sort()
33+
34+
res = 0
35+
for e in edge:
36+
cost, a, b = e
37+
if find_parent(parent, a) != find_parent(parent, b):
38+
union_parent(parent, a, b)
39+
res += cost
40+
print(res)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /