Problem p02936 (python)

Fixed submission:
import sys
sys.setrecursionlimit(400000)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)] # paths[i] : 頂点iと繋がってる点を全て表す
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
    paths[b].append(a)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

ans = [0]*(N+1) # ans[i] : 頂点iについて答える値を格納

counters = [0]*(N+1) # そのノードの部分木に与える点数の総和を格納
for x in px:
    counters[x[0]] += x[1]

visit = [0]*(N+1) #dfsをする時に一度訪れた点に再訪しないための確認
def dfs(node, score, ans):
    visit[node] = 1 # 訪問したことを記録
    score += counters[node] # 自分の部分木に加点する分を加えて後世に残す
    ans[node] = score # 点数の記録
    for loof in paths[node]: # 自分と繋がってる各ノードについて
        if visit[loof] == 0: # まだ探索していなければ
            dfs(loof, score, ans) # 探索

dfs(1, 0, ans)
    

for i in range(1, N+1):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(400000)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)] # paths[i] : 頂点iと繋がってる点を全て表す
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
    paths[b].append(a)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

ans = [0]*(N+1) # ans[i] : 頂点iについて答える値を格納

counters = [0]*(N+1) # そのノードの部分木に与える点数の総和を格納
for x in px:
    counters[x[0]] += x[1]

# visit = [0]*(N+1) #dfsをする時に一度訪れた点に再訪しないための確認
def dfs(node, score, ans):
    # visit[node] = 1 # 訪問したことを記録
    score += counters[node] # 自分の部分木に加点する分を加えて後世に残す
    ans[node] = score # 点数の記録
    for loof in paths[node]: # 自分と繋がってる各ノードについて
        # if visit[loof] == 0: # まだ探索していなければ
            dfs(loof, score, ans) # 探索

dfs(1, 0, ans)
    

# for i in range(1, N+1):
#     print(ans[i])
# print('\n'.join(map(str, ans[1:])))

# for i in range(1, N+1):
#     print(ans[i], end = " ")
# print()
print(" ".join(map(str, ans[1:])))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(400000)
n,q=map(int,input().split())
ans=[0]*(n+1) #答え
score=[0]*(n+1) #xの総和
g=[[] for _ 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 j in range(q):
    p,x=map(int,input().split())
    score[p]+=x

visited=[0]*(N+1)
def dfs(v):
    visited[v] += 1
    global ans
    ans[v]+=score[v]
    for i in range(len(g[v])):
        if visited[g[v][i]] == 0:
            ans[g[v][i]]+=ans[v]
            dfs(g[v][i])
dfs(1)
print(" ".join(str, map(ans[1:])))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(200001)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)] # paths[i] : 頂点iと繋がってる点を全て表す
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
    paths[b].append(a)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

ans = [0]*(N+1) # ans[i] : 頂点iについて答える値を格納

counters = [0]*(N+1) # そのノードの部分木に与える点数の総和を格納
for x in px:
    counters[x[0]] += x[1]

visit = [0]*(N+1) #dfsをする時に一度訪れた点に再訪しないための確認
def dfs(node, score, ans):
    visit[node] = 1 # 訪問したことを記録
    score += counters[node] # 自分の部分木に加点する分を加えて後世に残す
    ans[node] = score # 点数の記録
    for loof in paths[node]: # 自分と繋がってる各ノードについて
        if visit[loof] == 0: # まだ探索していなければ
            dfs(loof, score, ans) # 探索

dfs(1, 0, ans)
    

for i in range(1, N+1):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
import sys
# sys.setrecursionlimit(100000)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
    paths[b].append(a)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

nodes = [0]*(N+1)
# childs = [list() for _ in range(N+1)]

# def dfs(root, node, arr, check):
#     check[node] = 1
#     arr[root].append(node)
#     for loof in paths[node]:
#         if check[loof] == 0:
#             dfs(root, loof, arr, check)

# for i in range(1, N+1):
#     visit = [0]*(N+1)
#     dfs(i, i, childs, visit)


# for op in px:
#     for child in childs[op[0]]:
#         nodes[child] += op[1]

# for i in range(1, N+1):
#     print(nodes[i])

counters = [0]*(N+1)
for x in px:
    counters[x[0]] += x[1]

visit = [0]*(N+1)
def dfs(node, score, nodes):
    visit[node] = 1
    score += counters[node]
    nodes[node] = score
    for loof in paths[node]:
        if visit[loof] == 0:
            dfs(loof, score, nodes)

dfs(1, 0, nodes)
    

for i in range(1, N+1):
    print(nodes[i])

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

nodes = [0]*(N+1)
childs = [list() for _ in range(N+1)]

def dfs(root, node, arr):
    arr[root].append(node)
    for loof in paths[node]:
        dfs(root, loof, arr)

for i in range(1, N+1):
    dfs(i, i, childs)


for op in px:
    for child in childs[op[0]]:
        nodes[child] += op[1]

for i in range(1, N+1):
    print(nodes[i])

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
path = [list() for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    path[a].append(b)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

nodes = [0]*(N+1)

def dfs(p, x, arr):
    arr[p] += x
    for loof in path[p]:
        dfs(loof, x, arr)

for op in px:
    dfs(op[0], op[1], nodes)

for i in range(1, N+1):
    print(nodes[i])