https://www.acmicpc.net/problem/1197
Input)
3 3
1 2 1
2 3 2
1 3 3
Output)
3
import heapq
v, e = [int(x) for x in input().split()]
par = [x for x in range(v+1)]
pq = []
for i in range(e):
a, b, c = [int(x) for x in input().split()]
heapq.heappush(pq, (c, a, b))
def find(x):
if par[x]==x:
return x
grand = find(par[x])
par[x] = grand
return grand
def union(x,y):
tree_x = find(x)
tree_y = find(y)
if tree_x == tree_y:
return False
par[tree_y] = tree_x
return True
count = 0
val = 0
while True:
c, a, b = heapq.heappop(pq)
if union(a,b):
count += 1
val += c
if count == v-1:
break
print(val)