Problem p02936 (python)

Fixed submission:
n, q = map(int, input().split())
connect = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a - 1].append(b-1)
    connect[b - 1].append(a - 1)
    
values = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    values[p - 1] += x

from collections import deque
que = deque([[-1, 0]])
while que:
    parent, child = que.popleft()
    for num in connect[child]:
        if num == parent:
            continue
        values[num] += values[child]
        que.append([child, num])
print(*values)

Buggy submission (verdict: Runtime Error):
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]

n,q = LI()

v = [[] for i in range(n)]
for i in range(n-1):
  a,b = LI()
  a -= 1 # listのindex参照をそのまま使いたいので頂点id(1から始まっている)を-1しておく
  b -= 1 # aと同様
  v[a].append(b)
  v[b].append(a)

f = [0]*n
for i in range(q):
  p,x = LI()
  p -= 1 # aと同様
  f[p] += x

def dfs(x,d,s): # x:計算する頂点（上側）, d:計算済み(0)か否か(1), s:頂点xの値
  for y in v[x]: # 頂点xとつながる各頂点yの計算を行う
    if d[y]: # 頂点yが計算未了の場合
      s += f[y] # 頂点xの値sに頂点yの値を足す
      ans[y] = s # 頂点yの値はsに確定
      d[y] = 0 # 頂点yの計算は終わったのでd[y]=0とする
      dfs(y,d,s) # 頂点yの下層についても再帰的に計算
      s -= f[y] # 頂点yの下層の計算が終わったので、sを頂点xの値に戻す

ans = [0]*n
d = [1]*n
d[0] = 0
ans[0] = f[0]
s = f[0]
dfs(0,d,s)
print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
connect = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a - 1].append(b-1)
    connect[b - 1].append(a - 1)
    
values = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    values[p - 1] += x

def dfs(parent, flags, add_num):
    for num in connect[parent]:
        if flags[num]:
            add_num += values[num]
            result[num] = add_num
            flags[num] = 0
            dfs(num, flags, add_num)
            add_num -= values[num]
result = [0] * n
result[0] = values[0]
flags = [1] * n
flags[0] = 0
add_num = values[0]
dfs(0, flags, add_num)
print(' '.join(str(i) for i in result))

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
connect = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a - 1].append(b-1)
    connect[b - 1].append(a-1)
values = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    values[p - 1] += x
result = [values[0]] + [0] * (n - 1)
flags = [0] + [1] * (n - 1)
def dfs(parent, flags, add_num):
    for num in connect[parent]:
        if flags[num]:
            add_num += values[num]
            result[num] = add_num
            flags[num] = 0
            dfs(num, flags, add_num)
            add_num -= values[num]
dfs(0, flags, values[0])
print(' '.join(str(i) for i in result))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
n, q = map(int, input().split())
child = defaultdict(list)
ns = set()
for i in range(n - 1):
    a, b = map(int, input().split())
    child[a].append(b)
    ns.add(a)
    ns.add(b)
cnt = defaultdict(int)
for i in range(q):
    p, x = map(int, input().split())
    cnt[p] += x
def dfs(i):
    for num in child[i]:
        cnt[num] += cnt[i]
        dfs(num)
dfs(1)
print(' '.join(str(value) for _, value in sorted(cnt.items(), key=lambda x: x[0])))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
connect = defaultdict(list)
n, q = map(int, input().split())
for i in range(n - 1):
    a, b = map(int, input().split())
    connect[a].append(b)
sub = defaultdict(list)
def ki(key, root, dic):
    for i in dic[root]:
        sub[key].append(i)
        ki(key, i, dic)
for i in range(1, n+1):
    ki(i, i, connect)
for i in range(1, n+1):
    sub[i].append(i)
score = defaultdict(int)
for i in range(q):
    p, x = map(int, input().split())
    for num in sub[p]:
        score[num] += x
print(' '.join(str(j) for _, j in sorted(score.items(), key=lambda x: x[0])))