-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2887.py
51 lines (36 loc) · 931 Bytes
/
2887.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 행성 터널
from collections import defaultdict
import sys
input = sys.stdin.readline
N = int(input())
planet = [[] for _ in range(3)]
for i in range(N):
loc = tuple(map(int, input().split()))
for j in range(3):
planet[j].append((loc[j], i))
tunels = defaultdict(lambda: 10**9)
for j in range(3):
planet[j].sort()
for i in range(N - 1):
p1, i1 = planet[j][i]
p2, i2 = planet[j][i + 1]
tunels[(i1, i2)] = min(abs(p1 - p2), tunels[(i1, i2)])
tunels = list(tunels.items())
tunels.sort(key=lambda x: -x[1])
parent = [i for i in range(N)]
def find(x):
if x != parent[x]:
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
parent[max(x, y)] = min(x, y)
cnt, d = 0, 0
while cnt < N - 1:
p, cost = tunels.pop()
a, b = p
pa, pb = find(a), find(b)
if pa != pb:
union(pa, pb)
d += cost
cnt += 1
print(d)