๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปAlgorithm/PS

[BOJ/๋ฐฑ์ค€] 14907 ํ”„๋กœ์ ํŠธ ์Šค์ผ€์ค„๋ง - ํŒŒ์ด์ฌ

๋ฌธ์ œ

https://www.acmicpc.net/problem/14907

 

๋ฌธ์ œํ’€์ด

์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ

์ž…๋ ฅ์ด ํŠน์ดํ•ด์„œ ์‹ ๊ฒฝ์จ์ค˜์•ผํ•˜๋Š” ๋ฌธ์ œ

INPUT์„ ๊ธฐ์ค€์œผ๋กœ for๋ฌธ์„ ๋Œ๋ ธ์œผ๋ฉฐ ๊ฐœํ–‰๋งŒ ๋“ค์–ด์˜ฌ ์‹œ break ํ•˜๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.

๋˜ ์•ŒํŒŒ๋ฒณ์ด ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๊ธฐ๋•Œ๋ฌธ์— index๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ord ๋ณ€ํ™˜ ํ•ด์ฃผ์—ˆ๋‹ค.

 

์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ณดํ†ต์˜ ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ์™€ ํฌ๊ฒŒ ๋‹ค๋ฅผ ๊ฒƒ์ด ์—†์œผ๋‚˜, result๋ฅผ ๊ตฌํ•  ๋•Œ max ํ•จ์ˆ˜๋กœ ๋ชจ๋“  ์ž‘์—…์ด ๋๋‚ ๋•Œ์˜ ์‹œ๊ฐ„์„ ๊ตฌํ•ด์คŒ

 

 

 

์ฝ”๋“œ

import sys
input=sys.stdin.readline
##์œ„์ƒ์ •๋ ฌ

res=[0]*(26)
graph=[[] for _ in range(26)]
indegree=[0]*(26)
time=[0]*(26)


for INPUT in sys.stdin:
    if INPUT=='\n':
        break
    temp=INPUT.split()
    tmp=ord(temp[0])-65
    time[tmp]=int(temp[1])
    
    prev=list(str(temp[2:]))[2:-2]
    graph[tmp].append(prev)
    for i in prev:
        index=ord(i)-65
        indegree[index]+=1

    
result=0
que=[]
for i in range(26):
    if indegree[i]==0 and time[i]!=0:
        res[i]=time[i]
        result=max(result,res[i])
        que.append(i)
        
while que:
    temp=que.pop()

    for i in range(len(graph[temp][0])):
        index=graph[temp][0][i]
        
        index=ord(index)-65
        indegree[index]-=1

        res[index]=max(res[index],res[temp]+time[index])
        result=max(result,res[index])
            
        if indegree[index]==0:
            que.append(index)
print(result)