-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1197.py
46 lines (33 loc) · 912 Bytes
/
1197.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
# 최소 스패닝 트리
V, E = map(int, input().split())
# 노드 초기화
parent = [0] * (V+1)
for i in range(1, V+1):
parent[i] = i
# 간선 초기화
edges = []
answer = 0
for _ in range(E):
a, b, weight = map(int, input().split())
edges.append((weight, a, b))
edges.sort()
# parent - 부모값 리스트, x - 현재값 부모
def find_parent(parent, x):
if parent[x] != x:
parent[x] = find_parent(parent, parent[x])
return parent[x]
# 작은 값이 부모로 만들기
def union_parent(parent, a, b):
if a < b:
parent[b] = a
else:
parent[a] = b
for i in range(E):
# 가중치가 작은값부터 하나씩 꺼내기
weight, a, b = edges[i]
a_parent = find_parent(parent, a)
b_parent = find_parent(parent, b)
if a_parent != b_parent:
union_parent(parent, a_parent, b_parent)
answer += weight
print(answer)