-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1647.py
More file actions
46 lines (35 loc) · 778 Bytes
/
1647.py
File metadata and controls
46 lines (35 loc) · 778 Bytes
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
#boj1647
def UNION(a,b):
root1 = FIND(a)
root2 = FIND(b)
if root1 != root2:
parent[root1] = root2
def FIND(curr):
if parent[curr] == -1:
return curr
while parent[curr] != -1:
curr = parent[curr]
return curr
# 집의 갯수 / 길의 갯수 입력
n, m = map(int, input().split())
edgeInfo = []
parent = []
for i in range(n+1):
parent.append(-1)
for i in range(m):
u, v, w = map(int, input().split())
edgeInfo.append([u, v, w])
# edge 값 weight 기준으로 sort
edgeInfo = sorted(edgeInfo, key=lambda x : x[2])
edge = 0
tot = 0
for u, v, w in edgeInfo:
if FIND(u) == FIND(v):
continue
if edge != n-2:
UNION(u, v)
tot += w
edge += 1
else:
break
print(tot)