Algorithm/boj

[파이썬] 1647 도시 분할 계획

takeU 2022. 6. 16. 03:55
반응형
import sys
input = sys.stdin.readline

v, e = map(int, input().split())
parent = list(range(v + 1))

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

edges = []
total_cost = 0

for _ in range(e):
    a, b, cost = map(int, input().split())
    edges.append((cost, a, b))

edges.sort()
m = 0
for i in range(e):
    cost, a, b = edges[i]
    if find_parent(parent, a) != find_parent(parent, b):
        union_parent(parent, a, b)
        total_cost += cost
        m = max(m, cost)

print(total_cost - m)

MST, 크루스칼