Problem p02936 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

def dfs(fr, now, num):
    global graph, addsum, ans
    tmp = num + addsum[now]
    for a in graph[now]:
        if a == fr:
            continue
        dfs(now, a, tmp)
    ans[now] = tmp


def main():
    global graph, addsum, ans
    N, Q = map(int, readline().split())
    graph = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, readline().split())
        a -= 1; b -= 1
        graph[a].append(b)
        graph[b].append(a)
    addsum = [0]*N
    for _ in range(Q):
        p, x = map(int, readline().split())
        p -= 1
        addsum[p] += x
    
    ans = [0]*N
    dfs(-1, 0, 0)

    print(*ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def dfs(p, v, num):
    global g, plus_list, ans
    tmp = num + plus_list[v]
    for x in g[v]:
        if x == p:
            continue
        dfs(v, x, tmp)
    ans[v] = tmp
    
def main():
    global g, plus_list, ans
    N, Q = map(int, input().split())
    g = [[] for i in range(N)] 
    for i in range(N-1):
        a, b = map(int, readline().split())
        a -= 1
        b -= 1
        g[a].append(b)
        g[b].append(a)

    plus_list = [0] * N
    for i in range(Q):
        q, y = map(int, input().split())
        plus_list[q-1] += y

    ans = [0] * N
    
    dfs(-1, 0, 0)
    print(*ans)
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def dfs(p, v, num):
    global g, plus_list, ans
    tmp = num + plus_list[v]
    for x in g[v]:
        if v == p:
            continue
        dfs(v, x, tmp)
    ans[v] = tmp
    
def main():
    import sys
    sys.setrecursionlimit(500000)
    global g, plus_list, ans
    N, Q = map(int, input().split())
    g = [[] for i in range(N)] 
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        g[a].append(b)
        g[b].append(a)

    plus_list = [0] * N
    for i in range(Q):
        q, y = map(int, input().split())
        plus_list[q-1] += y

    ans = [0] * N
    
    dfs(-1, 0, 0)
    print(*ans)
if __name__ == "__main__":
    main()

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

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

ans = [0] * N
def dfs(v, p):
    ans[v] = plus_list[v] + ans[p]
    for i in range(len(g[v])):
        x = g[v][i]
        if x != v:
            dfs(x, v)

dfs(0, -1)
print(*ans)

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

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

ans = [0] * N
def dfs(v, p):
    ans[v] = plus_list[v] + ans[p]
    for i in range(len(g[v])):
        if g[v][i] != v:
            dfs(x, v)

dfs(0, -1)
print(*ans)

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
gyaku_lis = [0] * N
plus_lis = [0] * N
for i in range(N-1):
  a,b = map(int, input().split())
  gyaku_lis[b-1] = a-1
for i in range(Q):
  p,x = map(int, inpu().split())
  plus_lis[p-1] += x
ans_lis = [0]*N
ans_lis[0] = plus_lis[0]
for i in range(1,N):
  ans_lis[i] = ans_lis[gyaku_lis[i]] + plus_lis[i]
print(*ans_lis)

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
jun_lis = [[] for i in range(N)]
gyaku_lis = [[] for i in range(N)]
plus_lis = [0] * N
for i in range(N-1):
  a,b = map(int, input().split())
  jun_lis[a-1].append(b-1)
  gyaku_lis[b-1].append(a-1)
for i in range(Q):
  p,x = map(int, inpu().split())
  plus_lis[p-1] += x
ans_lis = [0]*N
ans_lis[0] = plus_lis[0]
for i in range(1,N):
  ans_lis[i] = ans_lis[gyaku_lis[i]] + plus_lis[i]
print(*ans_lis)