https://programmers.co.kr/learn/courses/30/lessons/42861
from collections import deque
def unionParent(cycle, a,b):
a=cycle[a]
b=cycle[b]
if a< b:
cycle[b] =a
else:
cycle[a] = b
return cycle
def findParent(cycle,a):
if cycle[a] != a:
return findParent(cycle, cycle[a])
else:
return a
def solution(n, costs):
answer = 0
cycle = [i for i in range(n)]
costs.sort(key = lambda x : x[2])
cnt =0
for start, end, cost in costs:
if cnt == n-1:
break
start=findParent(cycle,start)
end=findParent(cycle,end)
if start != end:
unionParent(cycle, start, end)
cnt +=1
answer +=cost
elif start == end:
continue
return answer