[BOJ][๐ŸŸก3][๋ฐฑ์ค€#14621] ๋‚˜๋งŒ ์•ˆ๋˜๋Š” ์—ฐ์• 

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #14621


๋ฌธ์ œ

๊นฝ๋ฏธ๋Š” 24์‚ด ๋ชจํƒœ์†”๋กœ์ด๋‹ค. ๊นฝ๋ฏธ๋Š” ๋Œ€๋งˆ๋ฒ•์‚ฌ๊ฐ€ ๋  ์ˆœ ์—†๋‹ค๋ฉฐ ์ž์‹ ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋Šฅ๋ ฅ์„ ์ด์šฉํ•˜์—ฌ ๋ฏธํŒ… ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๊ธฐ๋กœ ๊ฒฐ์‹ฌํ–ˆ๋‹ค. ๋ฏธํŒ… ์•ฑ์€ ๋Œ€ํ•™์ƒ์„ ํƒ€๊ฒŸ์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ์œผ๋ฉฐ ๋Œ€ํ•™๊ต๊ฐ„์˜ ๋„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ ๋งŒ๋“ค์—ˆ๋‹ค. ์ด ์•ฑ์€ ์‚ฌ์šฉ์ž๋“ค์„ ์œ„ํ•ด ์‚ฌ์‹ฌ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ๊ฒฝ๋กœ๋Š” 3๊ฐ€์ง€ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์‚ฌ์‹ฌ ๊ฒฝ๋กœ๋Š” ์‚ฌ์šฉ์ž๋“ค์˜ ์‚ฌ์‹ฌ์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋‚จ์ดˆ ๋Œ€ํ•™๊ต์™€ ์—ฌ์ดˆ ๋Œ€ํ•™๊ต๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž๋“ค์ด ๋‹ค์–‘ํ•œ ์‚ฌ๋žŒ๊ณผ ๋ฏธํŒ…ํ•  ์ˆ˜ ์žˆ๋„๋ก ์–ด๋–ค ๋Œ€ํ•™๊ต์—์„œ๋“  ๋ชจ๋“  ๋Œ€ํ•™๊ต๋กœ ์ด๋™์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ์ด๋‹ค. ์‹œ๊ฐ„์„ ๋‚ญ๋น„ํ•˜์ง€ ์•Š๊ณ  ๋ฏธํŒ…ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค.

๋งŒ์•ฝ ๋„๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŒ์•ฝ ์™ผ์ชฝ์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค๋ฉด, ์˜ค๋ฅธ์ชฝ ๊ทธ๋ฆผ์˜ ๋ณด๋ผ์ƒ‰ ์„ ๊ณผ ๊ฐ™์ด ๊ฒฝ๋กœ๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉด ์œ„์˜ 3๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ, ์ฃผ์–ด์ง€๋Š” ๊ฑฐ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์‹ฌ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•ด๋ณด์ž.


์ž…๋ ฅ

์ž…๋ ฅ์˜ ์ฒซ์งธ ์ค„์— ํ•™๊ต์˜ ์ˆ˜ N์™€ ํ•™๊ต๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (2 โ‰ค N โ‰ค 1,000) (1 โ‰ค M โ‰ค 10,000) ๋‘˜์งธ ์ค„์— ๊ฐ ํ•™๊ต๊ฐ€ ๋‚จ์ดˆ ๋Œ€ํ•™๊ต๋ผ๋ฉด M, ์—ฌ์ดˆ ๋Œ€ํ•™๊ต๋ผ๋ฉด W์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์— u v d๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ uํ•™๊ต์™€ vํ•™๊ต๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉฐ ์ด ๊ฑฐ๋ฆฌ๋Š” d์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. (1 โ‰ค u, v โ‰ค N) , (1 โ‰ค d โ‰ค 1,000)


์ถœ๋ ฅ

๊นฝ๋ฏธ๊ฐ€ ๋งŒ๋“  ์•ฑ์˜ ๊ฒฝ๋กœ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. (๋ชจ๋“  ํ•™๊ต๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ -1์„ ์ถœ๋ ฅํ•œ๋‹ค.)


์˜ˆ์ œ

์ž…๋ ฅ

5 7
M W W W M
1 2 12
1 3 10
4 2 5
5 2 5
2 5 10
3 4 3
5 4 7


์ถœ๋ ฅ

34


My Sol

import sys
input = sys.stdin.readline
from heapq import heappop, heappush

def find_x(x):
    if x!= p[x]:
        x = find_x(p[x])
    return x

V, E = map(int, input().split())
G = [''] + list(input().split())
p = [i for i in range(V+1)]
Q = []
for _ in range(E):
    u, v, w = map(int, input().split())
    heappush(Q, (w, u, v))

cnt = 0
ssum = 0
while cnt < V-1 and Q:
    w, u, v = heappop(Q)
    if G[u] == G[v]: continue

    a, b = find_x(u), find_x(v)
    if a == b: continue

    p[a] = b
    cnt += 1
    ssum += w

ret = ssum if cnt == V-1 else -1
print(ret)

์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ์™€ ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ์˜€๋‹ค. ํŠน์ดํ–ˆ๋˜ ์ ์€ ๋‘ ๋…ธ๋“œ๊ฐ€ ๊ฐ™์€ ์„ฑ๋ณ„์ด๋ฉด ์•ˆ ๋œ๋‹ค๋Š” ์กฐ๊ฑด์ด์—ˆ๋Š”๋ฐ, ์„ฑ๋ณ„ ์ •๋ณด๋ฅผ ๋‹ด์€ G ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๋‘ ๋…ธ๋“œ u, v์˜ ๊ฐ’์„ ๋น„๊ตํ•ด ๊ฐ™์œผ๋ฉด ๋‹ค์Œ ๊ฐ„์„  ์ •๋ณด๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

ํŠน๋ณ„ํžˆ ์–ด๋ ค์šธ ๊ฒƒ์€ ์—†์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import sys
input = sys.stdin.readline

def find(u):
    if u != unionfind[u]:
        unionfind[u] = find(unionfind[u])
    return unionfind[u]
def union(u, v):
    root1 = find(u)
    root2 = find(v)
    unionfind[root2] = root1
def kruskal():
    edges = 0
    dist = 0
    while True:
        if edges == v-1: break
        if len(graph) == 0:
            print(-1)
            sys.exit(0)
        u, ve, w = graph.pop()
        if find(u) != find(ve):
            union(u, ve)
            dist += w
            edges += 1
    return dist

v, e = map(int, input().split())
l = input().rstrip().split()
graph = []
for _ in range(e):
    a, b, c = map(int, input().split())
    if l[a-1] != l[b-1]:
        graph.append((a,b,c))
graph.sort(key = lambda x : -x[2])
unionfind = [0]
for i in range(1, v+1):
    unionfind.append(i)
print(kruskal())

์—ฐ์‚ฐ์‹œ๊ฐ„์ด 70% ์ •๋„์ธ ๋ฌธ์ œํ’€์ด๋ฅผ ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. ๋‹ค๋ฅธ ์ ‘๊ทผ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋˜ ๊ฒƒ์€ ์ฒ˜์Œ์— ๊ฐ„์„ ์ •๋ณด๋ฅผ ๋ฐ›์„ ๋•Œ๋ถ€ํ„ฐ ์„ฑ๋ณ„ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜์—ฌ ๊ฐ™์€ ์„ฑ๋ณ„์ด๋ผ๋ฉด ๊ฐ„์„  ์ •๋ณด ํž™์— ๋„ฃ์ง€ ์•Š๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํผํฌ๋จผ์Šค๊ฐ€ ์ข‹์•„์ง€๋Š”๊ฐ€ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค. ๋ฐ›์„ ๋•Œ ํ™•์ธํ•˜๋‚˜ ๋Œ๋ฆด ๋•Œ ํ™•์ธํ•˜๋‚˜ ์กฐ์‚ผ๋ชจ์‚ฌ์ด๋‹ค. ๋ฐ›์„ ๋•Œ ์„ฑ๋ณ„์ด ๋‹ค๋ฅธ์ง€๋ฅผ ๊ฐ€์žฅ ๋จผ์ € ์ฒดํฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•œ ์—ฐ์‚ฐ์ด ์—†์–ด ํฐ ์ฐจ์ด๋Š” ์—†์„ ๊ฒƒ์ด๋‹ค.

๋‚˜๋Š” heap์„ ์ด์šฉํ•ด ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ ๊ฐ„์„ ์„ heappopํ•˜๋Š” ๋ฐฉ์‹์„ ํ™œ์šฉํ–ˆ๋Š”๋ฐ, ์ด ๋ถ„์€ lambda๋ฅผ ์ด์šฉํ•œ sort๋ฅผ ์‚ฌ์šฉํ•˜์…จ๋‹ค. heap์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ 1๋ฒˆ ์œ„์น˜์— ๋ฐฐ์น˜ํ•˜๊ณ  ์ด๊ฒƒ์ด heappop๋  ๋•Œ๋งˆ๋‹ค O(log N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ์žฌ์ •๋ ฌํ•˜๋Š”๋ฐ, ์ด ๋•Œ๋ฌธ์ธ๊ฐ€ ์‹ถ์–ด sort()๋ฅผ ์ด์šฉํ•ด๋ณด์•„๋„ ์‹œ๊ฐ„ ์ฐจ์ด๊ฐ€ ํฌ์ง€ ์•Š์•˜๋‹ค.(๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์€ 5% ์ •๋„ ๊ฐ์†Œํ–ˆ๋‹ค.)

๊ทธ ์™ธ์—๋Š” ๋กœ์ง ๊ตฌ์„ฑ์ด ๋ชจ๋‘ ๊ฐ™์€๋ฐ ๋น ๋ฅธ ์ด์œ ๋ฅผ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ