Problem p02936 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())
G = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(lambda x: x-1, map(int, input().split()))
    G[a].append(b)
    G[b].append(a)

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

def dfs(v, pre=-1):  
    for nx in G[v]:
        if nx==pre:
            continue
        ans[nx] += ans[v]
        dfs(nx,v)
dfs(0)
print(*ans)

Buggy submission (verdict: Runtime Error):
def dfs(v, p=-1):
    for nx in G[v]:
        if nx==p:
            continue
        ans[nx] += ans[v]
        dfs(nx, v)

n, q = map(int, input().split())
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(lambda x:x-1, map(int, input().split()))
    G[a].append(b)
    G[b].append(a)

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

dfs(0)
print(*ans)

Buggy submission (verdict: Runtime Error):
def dfs(v, p=-1):
    for nx in G[v]:
        if nx==p: continue
        ans[nx] += ans[v]
        dfs(nx, v)

n, q = map(int, input().split())
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(lambda x:x-1, map(int, input().split()))
    G[a].append(b)
    G[b].append(a)

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

dfs(0)
print(*ans)

Buggy submission (verdict: Runtime Error):
def dfs(v, p, ans, G):
    if p!=-1: ans[v] += ans[p]
    for g in G[v]:
        if g==p: continue
        ans = dfs(g, v, ans, G)
    return ans

n, q = map(int, input().split())
ans = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = list(map(lambda x: x-1, map(int, input().split())))
    G[a].append(b)
    G[b].append(a)

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

ans = dfs(0, -1, ans, G)

print(*ans)

Buggy submission (verdict: Runtime Error):
def dfs(v, p, ans):
    if p!=-1: ans[v] += ans[p]
    for g in G[v]:
        if g==p: continue
        dfs(g, v, ans)
    return ans

n, q = map(int, input().split())
ans = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = list(map(lambda x: x-1, map(int, input().split())))
    G[a].append(b)
    G[b].append(a)

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

ans = dfs(0, -1, ans)

print(*ans)

Buggy submission (verdict: Runtime Error):
def dfs(v, p, ans):
    if p!=-1: ans[v] += ans[p]
    for g in G[v]:
        if g==p: continue
        dfs(g, v, ans)
    return ans

n, q = map(int, input().split())
ans = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = list(map(lambda x: x-1, map(int, input().split())))
    G[a].append(b)
    G[b].append(a)

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

dfs(0, -1, ans)

print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
ans = [0]*n
AB = [[] for _ in range(n-1)]
for i in range(n-1):
    AB[i] = list(map(lambda x: x-1, map(int, input().split())))
AB.sort(key=lambda x: x[1])

for (a,b) in AB:
    T[a] = T[a].union(T[b])

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

for (a,b) in AB:
    ans[b] += ans[a]

print(*ans)