Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions 이니지수/섬 연결하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
parent=[]

def find(parent,x):
if(parent[x]==x):
return x
return find(parent,parent[x])

def union(parent,a,b):
rootA=find(parent,a)
rootB=find(parent,b)
if(rootA<rootB):
parent[rootB]=rootA
else:
parent[rootA]=rootB

def solution(n, costs):
global parent
result=0
parent=[i for i in range(n+1)]
costs.sort(key=lambda x:x[2]) # [1, 2, 5] : 노드 1과 2가 가중치 5의 값을 갖는 간선으로 연결되어있음
for c in costs:
a,b,cost= c
if find(parent,a)!=find(parent,b):
union(parent,a,b)
result+=cost
return result