forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (51 loc) · 1.48 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
# Authored by : cieske
# Co-authored by : -
# Link : http://boj.kr/b650c3b805ac4251a7a125606197836a
import sys
def input():
return sys.stdin.readline().rstrip()
while True:
n, m = map(int, input().split())
if n == 0 and m == 0:
break
edge = []
total = 0
for _ in range(m):
x, y, w = map(int, input().split())
edge.append([x, y, w])
total += w
num_edge = 0
edge.sort(key=lambda x: -x[2])
# Disjoint set 구성
dis_set = [-1 for _ in range(n+1)]
def upward(x, change_lst):
if dis_set[x] < 0:
return x
change_lst.append(x)
return upward(dis_set[x], change_lst)
def find_root(x):
change_lst = []
res = upward(x, change_lst)
for idx in change_lst:
dis_set[idx] = res
return res
def union(x, y):
x_root = find_root(x)
y_root = find_root(y)
if x_root != y_root: # 두 node의 root가 다르다면?
if dis_set[x_root] < dis_set[y_root]:
dis_set[y_root] = x_root
if dis_set[x_root] > dis_set[y_root]:
dis_set[x_root] = y_root
else:
dis_set[x_root] = -1
dis_set[y_root] = x_root
# 크루스칼 시작
sol = 0
while num_edge < n-1:
x, y, w = edge.pop()
if find_root(x) != find_root(y):
union(x, y)
sol += w
num_edge += 1
print(total - sol)