Problem p02936 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

#def input():
#  return sys.stdin.readline()[:-1]

n,q=map(int,input().split())
cnt=[0]*(n)
g=[[] for _ in range(n)]
for _ in range(n-1): #木グラフの隣接リストを作成
  a,b=map(int,input().split())
  g[a-1].append(b-1)
  g[b-1].append(a-1)
for _ in range(q):
    a , b = map(int,input().split())
    a = a - 1
    cnt[a] += b

def dfs(now,prev=-1):
  for next in g[now]:
    if next==prev:
      continue
      
    cnt[next]+=cnt[now]
    dfs(next,now)
dfs(0)
print(*cnt)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b - 1].append(a - 1)

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
visited = [0]*n

def dfs(idx):
    visited[idx]=1
    for i in g[idx]:
        if visited[i]==1:
            continue
        costs[i]+=costs[idx]
        dfs(i)
    return

dfs(0)
print(*costs)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b - 1].append(a - 1)

def dfs(idx):
    visited[idx]=1
    for i in g[idx]:
        if visited[i]==1:
            continue
        costs[i]+=costs[idx]
        dfs(i)
    return

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
visited = [0]*n
dfs(0)
print(*costs)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b - 1].append(a - 1)

def dfs(idx,costs,visited):
    visited[idx]=1
    for i in g[idx]:
        if visited[i]==1:
            continue
        costs[i]+=costs[idx]
        costs = dfs(i,costs,visited)
    return costs

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
visited = [0]*n
costs = dfs(0,costs,visited)
print(*costs)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b - 1].append(a - 1)

def dfs(idx,c,res,visited):
    visited[idx]=1
    c+=costs[idx]
    res[idx]=c
    for i in g[idx]:
        if visited[i]==1:
            continue
        res = dfs(i,c,res,visited)
    return res

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
res = [0]*n
visited = [0]*n
res = dfs(0,0,res,visited)
print(*res)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)

def dfs(idx,c,res):
    c+=costs[idx]
    res[idx]=c
    for i in g[idx]:
        res = dfs(i,c,res)
    return res

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
res = [0]*n
res = dfs(0,0,res)
print(*res)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)

def dfs(idx,c,res):
    c+=costs[idx]
    res[idx]=c
    for i in g[idx]:
        res = dfs(i,c,res)
    return res

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
res = [0]*n
res = dfs(0,0,res)
output=""
for i in range(n):
    output+=(str(res[i])+" ")
print(output[:-1])