Problem p02936 (python)

Fixed submission:
from collections import deque 
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)
X=[0]*N
for i in range(Q):
    p,x=map(int, input().split())
    X[p-1]+=x
que=deque([(0,-1)])
#print(que)
while que:
    now, pre=que.pop()
    for nex in G[now]:
        if nex==pre:
            continue
        else:
            X[nex]+=X[now]
            que.append((nex,now))

print(" ".join(map(str, X)))

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


add=[0]*N
for i in range(Q):
  p,x=map(int, input().split())
  add[p-1]+=x

ans=[0]*N
def dfs(start, pre, preval):
  addval=preval+add[start]
  ans[start]+=addval
  for next_node in tree[start]:
    dfs(next_node, start,addval)
dfs(0,-1,0)  
for a in ans:
  print(a, end=" ")

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

add=[0]*N
for i in range(Q):
  p,x=map(int, input().split())
  add[p-1]+=x

ans=[0]*N
def dfs(start, pre, preval):
  addval=preval+add[start]
  ans[start]+=addval
  for next_node in tree[start]:
    if next_node==pre:
      continue
    dfs(next_node, start,addval)
dfs(0,-1,0)  
for a in ans:
  print(a, end=" ")

Buggy submission (verdict: Runtime Error):
N, Q=map(int, input().split())
T=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int, input().split())
  T[a-1].append(b-1)
  T[b-1].append(a-1)
P=[0]*N
ans=[0]*N
for i in range(Q):
  p,x=map(int, input().split())
  P[p-1]+=x

def dfs(start, pre_node, pre_val):
  now_val=(pre_val+P[start])
  ans[start]=now_val
  
  for next_node in T[start]:
    if next_node==pre_node:
      continue
    dfs(next_node, start,now_val)
    
dfs(0,-1,0)   
for a in ans:
  print(a, end=" ")

Buggy submission (verdict: Runtime Error):
N, Q=map(int, input().split())
T=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int, input().split())
  T[a-1].append(b-1)
  T[b-1].append(a-1)
P=[0]*N
ans=[0]*N
for i in range(Q):
  p,x=map(int, input().split())
  P[p-1]+=x

def dfs(start, pre_node, pre_val):
  now_val=(pre_val+P[start])
  ans[start]=now_val
  
  for next in T[start]:
    if next==pre_node:
      continue
    dfs(next, start,now_val)
    
dfs(0,0,0)   
for a in ans:
  print(a, end=" ")

Buggy submission (verdict: Runtime Error):
N, Q=map(int, input().split())
T=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int, input().split())
  T[a-1].append(b-1)
  
P=[0]*N
ans=[0]*N
for i in range(Q):
  p,x=map(int, input().split())
  P[p-1]+=x

def dfs(start, pre_node, pre_val):
  now_val=(pre_val+P[start])
  ans[start]=now_val
  
  for next in T[start]:
    dfs(next, start,now_val)
    
dfs(0,0,0)   
for a in ans:
  print(a, end=" ")

Buggy submission (verdict: Runtime Error):

N, Q=map(int, input().split())
value=np.array([0]*N)
#各ノードの下につくノード

oya=[-1]*N
for i in range(N-1):
  a,b=map(int, input().split())
  
  oya[b-1]=a-1
  
#adds=[0]*N



for i in range(Q):
  p,x=map(int, input().split())
  value[p-1]+=x

for i in range(N):
  if i >0:
    value[i]+=value[oya[i]]
    ans=value[i]
  else:
    ans=value[i]
  print(ans, end=" ")

Buggy submission (verdict: Runtime Error):
import numpy as np
N, Q=map(int, input().split())
value=np.array([0]*N)
#各ノードの下につくノード

oya=[-1]*N
for i in range(N-1):
  a,b=map(int, input().split())
  
  oya[b-1]=a-1
  
#adds=[0]*N



for i in range(Q):
  p,x=map(int, input().split())
  value[p-1]+=x

for i in range(N):
  if i >0:
    value[i]+=value[oya[i]]
    ans=value[i]
  else:
    ans=value[i]
  print(ans, end=" ")

Buggy submission (verdict: Runtime Error):
N, Q=map(int, input().split())
value=[0]*N
#各ノードの下につくノード
road=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int, input().split())
  road[a-1].append(b-1)
  
adds=[0]*N
for i in range(Q):
  p,x=map(int, input().split())
  adds[p-1]+=x
  
#自身のノード番号 足していく
#a: add[なんとか]
def dfs(a,  i):
  value[i]+=a
  L=road[i]
  for l in L:
    dfs(a,l)
for i in range(N):
  dfs(adds[i], i)


for v in value:
  print(v, end=" ")