💡문제접근
💡코드(메모리 : 202896KB, 시간 : 4124ms)
import sys
input = sys.stdin.readline
def find_parent(parent, x):
if parent[x] != x:
parent[x] = find_parent(parent, parent[x])
return parent[x]
def union_parent(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else:
parent[a] = b
N = int(input())
parent = [0] * (N+1)
edges = []
result = 0
cost_array = [list(map(int, input().strip().split())) for _ in range(N)]
for i in range(N):
parent[i] = i
for i in range(N):
for j in range(N):
edges.append([cost_array[i][j], i, j])
edges.sort()
for edge in edges:
cost, a, b = edge
if find_parent(parent, a) != find_parent(parent, b):
union_parent(parent, a, b)
result += cost
print(result)
💡소요시간 : 24m