[BOJ][๐ŸŸก4][๋ฐฑ์ค€#16562] ์นœ๊ตฌ๋น„

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #16562


๋ฌธ์ œ

19ํ•™๋ฒˆ ์ด์ค€์„์€ ํ•™์ƒ์ด N๋ช…์ธ ํ•™๊ต์— ์ž…ํ•™์„ ํ–ˆ๋‹ค. ์ค€์„์ด๋Š” ์ž…ํ•™์„ ๋งž์•„ ๋ชจ๋“  ํ•™์ƒ๊ณผ ์นœ๊ตฌ๊ฐ€ ๋˜๊ณ  ์‹ถ์–ดํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ค€์„์ด๋Š” ํ‰์ƒ ์ปดํ“จํ„ฐ๋ž‘๋งŒ ๋Œ€ํ™”๋ฅผ ํ•˜๋ฉฐ ์‚ด์•„์™”๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ๋žŒ๊ณผ ๋ง์„ ํ•˜๋Š” ๋ฒ•์„ ๋ชจ๋ฅธ๋‹ค. ๊ทธ๋Ÿฐ ์ค€์„์ด์—๊ฒŒ๋„ ํฌ๋ง์ด ์žˆ๋‹ค. ๋ฐ”๋กœ ์นœ๊ตฌ๋น„๋‹ค! ํ•™์ƒ i์—๊ฒŒ Ai๋งŒํผ์˜ ๋ˆ์„ ์ฃผ๋ฉด ๊ทธ ํ•™์ƒ์€ 1๋‹ฌ๊ฐ„ ์นœ๊ตฌ๊ฐ€ ๋˜์–ด์ค€๋‹ค! ์ค€์„์ด์—๊ฒŒ๋Š” ์ด k์›์˜ ๋ˆ์ด ์žˆ๊ณ  ๊ทธ ๋ˆ์„ ์ด์šฉํ•ด์„œ ์นœ๊ตฌ๋ฅผ ์‚ฌ๊ท€๊ธฐ๋กœ ํ–ˆ๋‹ค. ๋ง‰์ƒ ์นœ๊ตฌ๋ฅผ ์‚ฌ๊ท€๋‹ค ๋ณด๋ฉด ๋ˆ์ด ๋ถ€์กฑํ•ด์งˆ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ค€์„์ด๋Š” โ€œ์นœ๊ตฌ์˜ ์นœ๊ตฌ๋Š” ์นœ๊ตฌ๋‹คโ€๋ฅผ ์ด์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ค€์„์ด๋Š” ์ด์ œ ๋ชจ๋“  ์นœ๊ตฌ์—๊ฒŒ ๋ˆ์„ ์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค! ์œ„์™€ ๊ฐ™์€ ๋…ผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ, ๊ฐ€์žฅ ์ ์€ ๋น„์šฉ์œผ๋กœ ๋ชจ๋“  ์‚ฌ๋žŒ๊ณผ ์นœ๊ตฌ๊ฐ€ ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ตฌํ•˜๋ผ.


์ž…๋ ฅ

์ฒซ ์ค„์— ํ•™์ƒ ์ˆ˜ Nย (1ย โ‰ค Nย โ‰คย 10,000)๊ณผ ์นœ๊ตฌ๊ด€๊ณ„ ์ˆ˜ Mย (0ย โ‰คย Mย โ‰คย 10,000), ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ˆ kย (1ย โ‰คย kย โ‰คย 10,000,000)๊ฐ€ย ์ฃผ์–ด์ง„๋‹ค. ๋‘๋ฒˆ์งธ ์ค„์— N๊ฐœ์˜ย ๊ฐ๊ฐ์˜ ํ•™์ƒ์ด ์›ํ•˜๋Š” ์นœ๊ตฌ๋น„ Ai๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1ย โ‰คย Aiย โ‰คย 10,000, 1ย โ‰ค iย โ‰ค N) ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ์ˆซ์ž v, w๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๊ฒƒ์€ ํ•™์ƒ v์™€ ํ•™์ƒ w๊ฐ€ ์„œ๋กœ ์นœ๊ตฌ๋ผ๋Š” ๋œป์ด๋‹ค.


์ถœ๋ ฅ

์ค€์„์ด๊ฐ€ ๋ชจ๋“  ํ•™์ƒ์„ ์นœ๊ตฌ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋ฉด,ย ์นœ๊ตฌ๋กœ ๋งŒ๋“œ๋Š”๋ฐ ๋“œ๋Š” ์ตœ์†Œ๋น„์šฉ์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ์นœ๊ตฌ๋ฅผ ๋‹ค ์‚ฌ๊ทˆย ์ˆ˜ ์—†๋‹ค๋ฉด, โ€œOh noโ€(๋”ฐ์˜ดํ‘œ ์ œ๊ฑฐ)๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

5 3 20
10 10 20 20 30
1 3
2 4
5 4


์ถœ๋ ฅ

20


์˜ˆ์ œ 2

์ž…๋ ฅ

5 3 10
10 10 20 20 30
1 3
2 4
5 4


์ถœ๋ ฅ

Oh no


My Sol

import sys
input = sys.stdin.readline
from collections import deque

def bfs(i):
    global v
    Q = deque()
    Q.append(i)

    minMuch = much[i]
    while Q:
        u = Q.popleft()
        minMuch = min(minMuch, much[u])
        if v[u]: continue
        v[u] = 1

        for e in G[u]:
            Q.append(e)

    return minMuch

N, M, k = map(int, input().split())
much = [0]
much += list(map(int, input().split()))
G = [[] for _ in range(N+1)]
v = [0]*(N+1)
for _ in range(M):
    s, e = map(int, input().split())
    G[s].append(e)
    G[e].append(s)

minSsum = 0
for i in range(1, N+1):
    if not v[i]:
        minSsum += bfs(i)

ans = minSsum if minSsum <= k else 'Oh no'
print(ans)

๊ทธ๋ž˜ํ”„๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฌธ์ œ์ผ ๊ฒƒ ๊ฐ™์€๋ฐ, BFS๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ž˜ ํ’€์–ด๋‚ธ ๊ฒƒ ๊ฐ™๋‹ค. visit ๋ฐฐ์—ด์„ ๋‘๊ณ , 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ˆœํšŒํ•˜๋ฉฐ visit ์ฒดํฌ๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ๋…ธ๋“œ๋“ค์„ visit์ฒดํฌํ•ด๊ฐ€๋ฉฐ ์ตœ์†Œ ์นœ๊ตฌ๋น„๋ฅผ ๊ตฌํ•œ๋‹ค. bfs๊ฐ€ ๋๋‚˜๋ฉด ์ „์—ญ์—์„œ ์ด ์ตœ์†Œ ์นœ๊ตฌ๋น„๋ฅผ ๋ˆ„์ ํ•ด๊ฐ„๋‹ค.

์ „์ฒด bfs๋ฅผ ๋ชจ๋‘ ๋งˆ์น˜๋ฉด ์ด๋•Œ์˜ ๋ˆ„์  ๊ฐ’์ด ์ตœ์†Œ ์นœ๊ตฌ๋น„์ด๋ฉฐ, ์ด๋ฅผ k์™€ ๋น„๊ตํ•ด์„œ k๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ˆ„์ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ , k๋ฅผ ๋„˜๊ธด๋‹ค๋ฉด ์ข…๋ฃŒํ•œ๋‹ค.


๊ฒฐ๊ณผ

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


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

์ถœ์ฒ˜

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10000)


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


def union_(a, b):
    a = find(a)
    b = find(b)

    if a == b:
        return

    parent[a] = b
    arr[b] = min(arr[a], arr[b])


N, M, K = map(int, input().split())

parent = [i for i in range(N + 1)]

arr = [0] + list(map(int, input().split()))
for _ in range(M):
    a, b = map(int, input().split())
    union_(a, b)

ans = 0

for i in range(1, N + 1):
    if parent[i] == i:
        ans += arr[i]

if ans > K:
    print('Oh no')
else:
    print(ans)

์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋Š” ํ•œ ๋ฃจํŠธ๋…ธ๋“œ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ์ฒ˜์Œ์— ์ดˆ๊ธฐํ™”ํ•˜์˜€๋“ฏ์ด parent๊ฐ€ ์ธ๋ฑ์Šค ๊ทธ ์ž์‹ ์ธ ๊ฒฝ์šฐ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์นœ๊ตฌ๋น„๋ฅผ ans์— ๋”ํ•ด์ค€๋‹ค.

๊ฐ„์„ ์˜ ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค union์„ ์ง„ํ–‰ํ•ด ์„œ๋กœ์†Œ ์ง‘ํ•ฉ์œผ๋กœ ๋ฌถ์–ด์ฃผ๊ณ , ๊ทธ ๊ณผ์ •์—์„œ ํ•ด๋‹นํ•˜๋Š” ๋ฃจํŠธ ๋…ธ๋“œ ์ธ๋ฑ์Šค b๋ฅผ ์ธ๋ฑ์Šค๋กœ ํ•˜๋Š” arr ๋ฐฐ์—ด์— ์ตœ์†Œ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค. ๋ถˆํ•„์š”ํ•œ ์ค‘๋ณต ์กฐํšŒ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— BFS๋ณด๋‹ค๋Š” ์—ฐ์‚ฐ์†๋„๊ฐ€ ๋” ๋น ๋ฅธ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

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