Problem p02936 (python)

Fixed submission:
from collections import deque
N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

edge_map = [[] for i in range(N)]

for edge in edges:
    a = edge[0]
    b = edge[1]
    edge_map[a-1].append(b-1)
    edge_map[b-1].append(a-1)


counters = [0]*N

for operation in operations:
    p = operation[0]
    x = operation[1]
    counters[p-1] += x


res_list = [counters[i] for i in range(N)]
calculated = [False for i in range(N)]

target = [0]
while len(target) > 0:
    t = target.pop()
    calculated[t] = True
    for child in edge_map[t]:
        if calculated[child]:
            continue
        target.append(child)
        res_list[child] += res_list[t]

print(*res_list)

Buggy submission (verdict: Runtime Error):
from collections import deque
N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

edge_map = {}
for i in range(N):
    edge_map[i] = deque([])

for edge in edges:
    a = edge[0]
    b = edge[1]
    edge_map[a-1].append(b-1)
    edge_map[b-1].append(a-1)


counters = [0 for i in range(N)]

for operation in operations:
    p = operation[0]
    x = operation[1]
    counters[p-1] += x


res_list = [counters[i] for i in range(N)]
calculated = [False for i in range(N)]

def dfs_res(idx, parent, diff):
    global res_list
    global counters
    if calculated[i]:
        return res_list[idx]
    res = counters[idx] + diff
    res_list[idx] = res
    calculated[idx] = True
    for child in edge_map[idx]:
        if child == parent:
            continue
        dfs_res(child, idx, res)
    return

dfs_res(0, None, 0)

ans = " ".join([str(i) for i in res_list])
print(ans)

Buggy submission (verdict: Runtime Error):

N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

edge_map = {}
for i in range(N):
    edge_map[i] = []

for edge in edges:
    a = edge[0]
    b = edge[1]
    edge_map[a-1].append(b-1)
    edge_map[b-1].append(a-1)


counters = [0 for i in range(N)]

for operation in operations:
    p = operation[0]
    x = operation[1]
    counters[p-1] += x


res_list = [counters[i] for i in range(N)]
calculated = [False for i in range(N)]

def dfs_res(idx, parent, diff):
    global res_list
    global counters
    if calculated[i]:
        return res_list[idx]
    res = counters[idx] + diff
    res_list[idx] = res
    calculated[idx] = True
    for child in edge_map[idx]:
        if child == parent:
            continue
        dfs_res(child, idx, res)
    return

dfs_res(0, None, 0)

ans = ""
for res in res_list:
    ans += str(res) + " "
print(ans)

Buggy submission (verdict: Runtime Error):

N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

edge_map = {}
for i in range(N):
    edge_map[i] = []

for edge in edges:
    a = edge[0]
    b = edge[1]
    edge_map[a-1].append(b-1)
    edge_map[b-1].append(a-1)

counters_map = [None for i in range(N)]

def dfs(idx, parent):
    global counters_map
    if counters_map[idx]:
        return counters_map[idx]
    counter = [idx]
    for child in edge_map[idx]:
        if child == parent:
            continue
        counter.extend(dfs(child, idx))
    counter = list(set(counter))
    counters_map[idx] = counter
    return counter

dfs(0, None)

counters = [0 for i in range(N)]

for operation in operations:
    p = operation[0]
    x = operation[1]
    counters[p-1] += x


res_list = [0 for i in range(N)]
for i in range(N):
    diff = counters[i]
    for child_idx in counters_map[i]:
        res_list[child_idx] += diff

ans = ""
for res in res_list:
    ans += str(res) + " "
print(ans)

Buggy submission (verdict: Runtime Error):

N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

edge_map = {}
for i in range(N):
    edge_map[i+1] = []

for edge in edges:
    a = edge[0]
    b = edge[1]
    edge_map[a].append(b)
    edge_map[b].append(a)

counters_map = [None for i in range(N)]

def dfs(idx, parent):
    global counters_map
    if counters_map[idx - 1]:
        return counters_map[idx - 1]
    counter = [idx]
    for child in edge_map[idx]:
        if parent and child == parent:
            continue
        counter.extend(dfs(child, idx))
    counters_map[idx-1] = counter
    return counter

dfs(1, None)

counters = [0 for i in range(N)]

for operation in operations:
    p = operation[0]
    x = operation[1]
    for idx in counters_map[p-1]:
        counters[idx-1] += x

print(counters)