Problem p02936 (python)

Fixed submission:
def main():
    from collections import deque
    n, q = map(int, input().split())
    g = [[] for i in range(n+1)]
    point = [0 for i in range(n+1)]
    for i in range(n-1):
        a, b = map(int, input().split())
        g[a].append(b)
        g[b].append(a)
    for i in range(q):
        p, x = map(int, input().split())
        point[p] += x

    q = deque([[1,0]])
    for j in range(n):
        now = q.pop()
        point[now[0]] += point[now[1]]
        for i in g[now[0]]:
            if i==now[1]:
                continue
            q.append([i,now[0]])
    for i in range(1, n+1):
        print(point[i], end='')
        if i!=n:
            print(' ', end='')

if __name__ == '__main__':
    main()

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

def dfs(now, fro, point):
    now, fro, point = int(now), int(fro), int(point)
    point += c[now]
    ans[now] = point
    for i in range(len(g[now])):
        if g[now][i]==fro:
            continue
        dfs(g[now][i], now, point)


dfs(1, -1, 0)
for i in range(1, n+1):
    print(ans[i], end='')
    if i!=n:
        print(' ', end='')
print('')

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

def dfs(now, fro, point):
    point += c[now]
    ans[now] = point
    for i in g[now]:
        if i==int(fro):
            continue
        dfs(i, now, point)

dfs(1, -1, 0)
for i in range(1, n+1):
    print(ans[i], end='')
    if i!=n:
        print(' ', end='')

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

def dfs(now, fro, point):
    point += c[now]
    ans[now] = point
    for i in g[now]:
        if i==int(fro):
            continue
        dfs(i, now, point)

dfs(1, -1, 0)
for i in range(1, n+1):
    print(ans[i], end='')
    if i!=n:
        print(' ', end='')

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

def dfs(now, fro, point):
    point += c[now]
    ans[now] = point
    for i in g[now]:
        if i==fro:
            continue
        dfs(i, now, point)

dfs(1, -1, 0)
for i in range(1, n+1):
    print(ans[i], end='')
    if i!=n:
        print(' ', end='')

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

def dfs(now, fro, point):
    point += c[now]
    ans[now] = point
    for i in g[now]:
        if i==fro:
            continue
        dfs(i, now, point)

dfs(1, -1, 0)
for i in range(1, n+1):
    print(ans[i], end='')
    if i!=n:
        print(' ', end='')