forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
35 lines (30 loc) · 745 Bytes
/
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
# Authored by : gusdn3477
# Co-authored by : -
# Link : http://boj.kr/e8d47d936e104388916666605df5778d
import sys
def input():
return sys.stdin.readline().rstrip()
def union(x, y):
x = find_parent(x)
y = find_parent(y)
if x < y:
parent[y] = x
else:
parent[x] = y
def find_parent(x):
if x != parent[x]:
parent[x] = find_parent(parent[x])
return parent[x]
N = int(input())
M = int(input())
parent = [i for i in range(N+1)]
arr = []
ans = 0
for i in range(M):
arr.append(list(map(int, input().split())))
arr.sort(key = lambda x : x[2])
for i in range(M):
if find_parent(arr[i][0]) != find_parent(arr[i][1]):
union(arr[i][0], arr[i][1])
ans += arr[i][2]
print(ans)