노트

[백준] 14267번 회사 문화 1 python 본문

알고리즘

[백준] 14267번 회사 문화 1 python

_Myway 2023. 6. 17. 22:31
import sys

sys.setrecursionlimit(10 ** 6)

a,b = map(int,input().split())
boss = list(map(int,input().split()))
gujo = [[] for _ in range(a)]
my_boss = [0]*(a+1)
ans = [0]*(a+1)
for i in range(1,a):
    if boss[i]!=-1:
        gujo[boss[i]].append(i+1)
        my_boss[i+1]=boss[i]

def tamsaek(k):
    ans[k] += ans[my_boss[k]]
    if k<a and gujo[k]:
        for j in range(len(gujo[k])):
            tamsaek(gujo[k][j])

for i in range(b):
    x,y = map(int, input().split())
    ans[x] += y

tamsaek(1)

ans.pop(0)
print(*ans)
Comments