문제 보러 가기 👈 클릭!
def get_p(connection, node):
if connection[node] == node:
return node
return get_p(connection, connection[node])
def solution(n, costs):
costs.sort(key = lambda x: x[2], reverse = True)
connection = [i for i in range(n)] #연결 정보
edges = []
while len(edges) != n - 1:
i1, i2, cost = costs.pop()
#부모 노드가 다를 경우 연결 가능
if get_p(connection, i1) != get_p(connection, i2):
#연결 정보 갱신
connection[get_p(connection, i1)] = i2
edges.append(cost)
return sum(edges)
📝 cycle 생성 여부를 다른 방식으로도 검증 할 수 있다.
#두 섬이 visited에 모두 존재 : 이미 건설한 다리
#두 섬이 visited에 모두 존재하지 X : 이미 연결되어 있는 섬들과 이 두 섬이 연결되지 X
#=> 해당 경우는 반복문을 끝낸 후 다시 반복문을 반복하며 연결
#두 섬 중 하나의 섬만 visited에 존재할 경우 다음과 같이 구현
def solution(n, costs):
costs.sort(key = lambda x: x[2])
visited = {costs[0][0]} #방문 한 섬
answer = 0
while len(visited) != n:
for island1, island2, cost in costs:
if len({island1, island2} & visited) == 1:
visited.update([island1, island2])
answer += cost
break #노드가 추가 되었으므로 len(visited) != n 조건 확인
return answer