DevLog ๐Ÿ“จ/Baekjoon

[BaekJoon] 1005: ACM Craft

cece00 2023. 2. 14. 21:01

BaekJoon 1005: ACM Craft

ํ’€์ด ๋ฐฉ๋ฒ•

์œ„์ƒ ์ •๋ ฌ topological sorting ๊ณผ DP๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค. ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์œ ํ–ฅ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค๊ณ , ์ง„์ž… ๊ฐ„์„ ์˜ ์ˆ˜๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด degree ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค. ์ •์ ์„ ๋ชจ๋‘ ๋‹ด์„ ํ๋„ ํ•˜๋‚˜ ์„ ์–ธํ•ด ์ค€๋‹ค.


from collections import deque
time = [0] + list(map(int, input().split())) # time to build
result = time.copy()

degree = [0] * (N + 1)
graph = [[] for _ in range(N+1)]
queue = deque([i+1 for i in range(N)])

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


graph

๊ทธ๋ž˜ํ”„ ์ž๋ฃŒ๊ตฌ์กฐ
vertex: 0 - none
vertex: 1 - 2, 3
vertex: 2 - 4
vertex: 3 - 4


์œ„์ƒ ์ •๋ ฌ ์ˆ˜ํ–‰๊ณผ์ •

ํ์—์„œ ์ •์  1๋ฅผ pop
์ •์  1์˜ ์ง„์ž… ๊ฐ„์„ ์€ ์—†์Šต๋‹ˆ๋‹ค.
์ •์  1์™€ ์ธ์ ‘ํ•œ ์ •์  2์˜ result ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๊ฐ„์„  <1, 2>๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
์ •์  1์™€ ์ธ์ ‘ํ•œ ์ •์  3์˜ result ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๊ฐ„์„  <1, 3>๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
ํ์—์„œ ์ •์  2๋ฅผ pop
์ •์  2์˜ ์ง„์ž… ๊ฐ„์„ ์€ ์—†์Šต๋‹ˆ๋‹ค.
์ •์  2์™€ ์ธ์ ‘ํ•œ ์ •์  4์˜ result ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๊ฐ„์„  <2, 4>๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
ํ์—์„œ ์ •์  3๋ฅผ pop
์ •์  3์˜ ์ง„์ž… ๊ฐ„์„ ์€ ์—†์Šต๋‹ˆ๋‹ค.
์ •์  3์™€ ์ธ์ ‘ํ•œ ์ •์  4์˜ result ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๊ฐ„์„  <3, 4>๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
ํ์—์„œ ์ •์  4๋ฅผ pop
์ •์  4์˜ ์ง„์ž… ๊ฐ„์„ ์€ ์—†์Šต๋‹ˆ๋‹ค.
์œ„์ƒ ์ •๋ ฌ ์ข…๋ฃŒ

์‹œํ–‰์ฐฉ์˜ค

  • DFS, DP ๋“ฑ๋“ฑ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•ด ๋ณด๋‹ค๊ฐ€ ๋„์ €ํžˆ ์•ˆ ๋˜๊ฒ ์–ด์„œ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด '์œ„์ƒ ์ •๋ ฌ'์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค. ๊ณจ๋“œ ๋‹จ๊ณ„๋ถ€ํ„ฐ ์—ฌ๋Ÿฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์„ ๋ณต์ˆ˜๋กœ ์ ์šฉํ•˜์—ฌ ํ’€์ดํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋งŽ์•„์ง„๋‹ค๊ณ  ํ•˜๋‹ˆ ์ฐธ๊ณ ํ•  ๊ฒƒ..
  • ์ฒ˜์Œ์—๋Š” double linked list ์ฒ˜๋Ÿผ ๊ฐ ์ •์ ์˜ in&out ๊ฐ„์„ ์„ ๋ชจ๋‘ ์ €์žฅํ–ˆ์œผ๋‚˜, ์œ„์ƒ ์ •๋ ฌ์—๋Š” ์ง„์ž… ๊ฐ„์„ ์˜ ์ˆ˜๋งŒ ์ฒดํฌํ•˜๋ฉด ๋˜๋ฏ€๋กœ degree ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ๋‹จ์ˆœํžˆ ๋‹ต๋งŒ ๋‚˜์˜จ๋‹ค๊ณ  ๋์ด ์•„๋‹ˆ๋ผ, ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•ด ๋ณด์ž.
  • ๋กœ์ปฌ์—์„œ๋Š” ๋Œ์•„๊ฐ€๋Š” ์ฝ”๋“œ๊ฐ€ ๋ฐฑ์ค€ Python3 ์œผ๋กœ ์ œ์ถœํ–ˆ์„ ๋•Œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋  ๊ฒฝ์šฐ, PyPy3 ์œผ๋กœ ์ œ์ถœํ•ด ๋ณผ ๊ฒƒ.

์ „์ฒด ์ฝ”๋“œ


from collections import deque

T = int(input())

for i in range(T):
    N, K = map(int, input().split())

    time = [0] + list(map(int, input().split()))
    result = time.copy()

    degree = [0] * (N + 1)
    graph = [[] for _ in range(N+1)]
    queue = deque([i+1 for i in range(N)])

    # graph as adjacency list
    for _ in range(K):
        src, des = map(int, input().split())
        graph[src].append(des)
        degree[des] += 1

    # topological sort
    while(queue):
        v = queue.popleft()
        if not degree[v]:
            for i in graph[v]:
                result[i] = max(result[i], result[v] + time[i])
                degree[i] -= 1
        else:
            queue.append(v)

    W = int(input())
    print(result[W])