[BOJ][๐ŸŸก5][๋ฐฑ์ค€#02056] ์ž‘์—…

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2056


๋ฌธ์ œ

์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ž‘์—… N๊ฐœ (3 โ‰คย N โ‰คย 10000)๊ฐ€ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ž‘์—…๋งˆ๋‹ค ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„(1 โ‰คย ์‹œ๊ฐ„ โ‰ค 100)์ด ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋ช‡๋ช‡ ์ž‘์—…๋“ค ์‚ฌ์ด์—๋Š” ์„ ํ–‰ ๊ด€๊ณ„๋ผ๋Š” ๊ฒŒ ์žˆ์–ด์„œ, ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ๋จผ์ € ์™„๋ฃŒ๋˜์–ด์•ผ ํ•  ์ž‘์—…๋“ค์ด ์žˆ๋‹ค. ์ด ์ž‘์—…๋“ค์€ ๋ฒˆํ˜ธ๊ฐ€ ์•„์ฃผ ์˜ˆ์˜๊ฒŒ ๋งค๊ฒจ์ ธ ์žˆ์–ด์„œ, K๋ฒˆ ์ž‘์—…์— ๋Œ€ํ•ด ์„ ํ–‰ ๊ด€๊ณ„์— ์žˆ๋Š”(์ฆ‰, K๋ฒˆ ์ž‘์—…์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ๋จผ์ € ์™„๋ฃŒ๋˜์–ด์•ผ ํ•˜๋Š”) ์ž‘์—…๋“ค์˜ ๋ฒˆํ˜ธ๋Š” ๋ชจ๋‘ 1 ์ด์ƒ (K-1) ์ดํ•˜์ด๋‹ค. ์ž‘์—…๋“ค ์ค‘์—๋Š”, ๊ทธ๊ฒƒ์— ๋Œ€ํ•ด ์„ ํ–‰ ๊ด€๊ณ„์— ์žˆ๋Š” ์ž‘์—…์ด ํ•˜๋‚˜๋„ ์—†๋Š” ์ž‘์—…์ด ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜ ์ด์ƒ ์กด์žฌํ•œ๋‹ค. (1๋ฒˆ ์ž‘์—…์ด ํ•ญ์ƒ ๊ทธ๋Ÿฌํ•˜๋‹ค) ๋ชจ๋“  ์ž‘์—…์„ ์™„๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ ์‹œ๊ฐ„์„ ๊ตฌํ•˜์—ฌ๋ผ. ๋ฌผ๋ก , ์„œ๋กœ ์„ ํ–‰ ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ์ž‘์—…๋“ค์€ ๋™์‹œ์— ์ˆ˜ํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ N๊ฐœ์˜ ์ค„์ด ์ฃผ์–ด์ง„๋‹ค. 2๋ฒˆ์งธ ์ค„์€ 1๋ฒˆ ์ž‘์—…, 3๋ฒˆ์งธ ์ค„์€ 2๋ฒˆ ์ž‘์—…, โ€ฆ, N+1๋ฒˆ์งธ ์ค„์€ N๋ฒˆ ์ž‘์—…์„ ๊ฐ๊ฐ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ฐ ์ค„์˜ ์ฒ˜์Œ์—๋Š”, ํ•ด๋‹น ์ž‘์—…์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ๋จผ์ € ์ฃผ์–ด์ง€๊ณ , ๊ทธ ๋‹ค์Œ์— ๊ทธ ์ž‘์—…์— ๋Œ€ํ•ด ์„ ํ–‰ ๊ด€๊ณ„์— ์žˆ๋Š” ์ž‘์—…๋“ค์˜ ๊ฐœ์ˆ˜(0 โ‰คย ๊ฐœ์ˆ˜ โ‰ค 100)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„ ํ–‰ ๊ด€๊ณ„์— ์žˆ๋Š” ์ž‘์—…๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ชจ๋“  ์ž‘์—…์„ ์™„๋ฃŒํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

7
5 0
1 1 1
3 1 2
6 1 1
1 2 2 4
8 2 2 4
4 3 3 5 6


์ถœ๋ ฅ

23


My Sol

def calc_time(n):
    global time, dp, first
    if dp[n]: return dp[n]

    if not first[n]:
        dp[n] = time[n]
        return dp[n]

    maxx = 0
    while first[n]:
        nn = first[n].pop()
        ret = calc_time(nn)
        if maxx < ret:
            maxx = ret

    dp[n] = time[n] + maxx
    return dp[n]


N = int(input())
dp = [0]*(N+1)
time = [0]*(N+1)
first = [set() for _ in range(N+1)]
for i in range(1, N+1):
    t, n, *arr = map(int, input().split())
    time[i] = t
    first[i] = set(arr)

for i in range(1, N+1):
    dp[i] = calc_time(i)

print(max(dp))

DP๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณ„์‚ฐ๋˜์–ด์•ผ ํ•˜๋Š” ์ž‘์—…์„ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

  1. ๋ฉ”๋ชจ์ด์ œ์ด์…˜์œผ๋กœ ๊ธฐ๋กํ•˜๋Š” DP ๋ฐฐ์—ด์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  2. ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  3. ๋งŒ์•ฝ dp ๋ฐฐ์—ด์˜ ํŠน์ • ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ’์ด ์ถ”๊ฐ€๋œ๋‹ค๋ฉด ์ด๋ฏธ ํ•ด๋‹น ์ธ๋ฑ์Šค๊นŒ์ง€์˜ ์ž‘์—…์€ ์„ ํ–‰ ์ž‘์—…์„ ๋งˆ์นœ ๋’ค์˜ ์ตœ์†Œ ์‹œ๊ฐ„์„ ์˜๋ฏธํ•œ๋‹ค.
  4. ์„ ํ–‰ ์ž‘์—…์˜ ๋ชจ๋“  dp ๋ฐฐ์—ด์„ ์ฑ„์šฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ์„ ํ–‰ ์ž‘์—…์˜ ์‹œ๊ฐ„๋“ค์„ ๊ฐ€์ ธ์˜จ ๋’ค, ์„ ํ–‰์ž‘์—…๋“ค์˜ ์ž‘์—…์‹œ๊ฐ„์˜ ์ตœ๋Œ“๊ฐ’๊ณผ ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋”ํ•œ ๊ฐ’์„ ํ˜„์žฌ ์ธ๋ฑ์Šค์˜ dp์— ์ €์žฅํ•œ๋‹ค.
  5. ์ด๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋ชจ๋“  dp ๋ฐฐ์—ด์„ ์ฑ„์šด๋‹ค.
  6. dp ๋ฐฐ์—ด์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

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

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