Problem p02936 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
inf = 10**17
mod = 10**9+7


n, q = map(int, input().split())
g = [[] for _ in range(n)]
val = [0] * n

for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)

for _ in range(q):
    p, x = map(int, input().split())
    val[p - 1] += x

seen = [False] * n
def dfs(v):
    seen[v] = True
    for nv in g[v]:
        if seen[nv]:
            continue
        val[nv] += val[v]
        dfs(nv)

dfs(0)
print(*val)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
g = [[] for _ in range(n)]
val = [0] * n

for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)

for _ in range(q):
    p, x = map(int, input().split())
    val[p - 1] += x

seen = [False] * n
def dfs(v):
    seen[v] = True
    for nv in g[v]:
        if seen[nv]:
            continue
        val[nv] += val[v]
        dfs(nv)

dfs(0)
print(*val)

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
root = [[] for _ in range(N)]
ans = [0] * N

for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    root[a].append(b)
    root[b].append(a)

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

visit = [False] * N
def dfs(n):
    visit[n] = True
    for go in root[n]:
        if visit[go]:
            continue
        ans[go] += ans[n]
        dfs(go)

dfs(0)
print(*ans)

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
root = [[] for i in range(N)]
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  root[a].append(b)
  root[b].append(a)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)
    
dfs(0)
print(*ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
N, Q = map(int, input().split())
root = [[] for i in range(N)]
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  root[a].append(b)
  root[b].append(a)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)
    
dfs(0)
print(' '.join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
from collections import deque
N, Q = map(int, input().split())
root = [[] for i in range(N)]
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  root[a-1].append(b-1)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)
    
dfs(0)
print(' '.join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
from collections import deque
N, Q = map(int, input().split())
root = [[] for i in range(5)]
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  root[a-1].append(b)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
now = 1
visit = deque([1])
while len(visit) < N:
  if root[now-1] == []:
    now = visit[visit.index(now)-1]
  else:
    go = root[now-1][0]
    root[now-1].pop(0)
    visit.append(go)
    ans[go-1] += ans[now-1]
    now = go
    
print(' '.join(map(str, ans)))