Problem p02936 (python)

Fixed submission:
N, Q = map(int, input().split())
nodes = [[] for _ in range(2*(10**5)+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    nodes[a] += [b]

PX = [0 for _ in range(2*(10**5)+1)]
for _ in range(Q):
    p, x = map(int, input().split())
    PX[p] += x


node_cnts = [0] * (2*(10**5)+1)

q = [(1, PX[1])]
while q:
    node, count = q.pop()
    node_cnts[node] = count
    for n in nodes[node]:
        subtree_nodes = q.append((n, count + PX[n]))


print(*node_cnts[1:N+1], sep=' ')

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

PX = [0 for _ in range(N+1)]
for _ in range(Q):
    p, x = map(int, input().split())
    PX[p] += x


node_cnts = [0] * (N+1)


def gen_subtree_nodes(node, count):
    node_cnts[node] = count
    for n in nodes[node]:
        subtree_nodes = gen_subtree_nodes(n, count + PX[n])


gen_subtree_nodes(1, PX[1])

print(*node_cnts[1:])

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
nodes = {str(i): [] for i in range(1, N+1)}
for _ in range(N-1):
    a, b = input().split()
    nodes[a] += [b]

PX = {str(i): 0 for i in range(1, N+1)}
for _ in range(Q):
    p, x_str = input().split()
    PX[p] += int(x_str)


node_cnts = [0] * (N+1)


def gen_subtree_nodes(node, count):
    node_cnts[int(node)] = count
    for n in nodes.get(node):
        subtree_nodes = gen_subtree_nodes(n, count + PX[n])


gen_subtree_nodes('1', PX['1'])

print(*node_cnts[1:])

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
nodes = {str(i): [] for i in range(1, N+1)}
for _ in range(N-1):
    a, b = input().split()
    nodes[a] += [b]

PX = [input().split() for _ in range(Q)]

ptree_map = {}


def gen_subtree_nodes(node):
    res = [int(node)]
    for n in nodes.get(node, []):
        subtree_nodes = gen_subtree_nodes(n)
        ptree_map[n] = subtree_nodes
        res += subtree_nodes

    return res


ptree_map['1'] = gen_subtree_nodes('1')

node_cnts = [0] * (N+1)
for p, x_str in PX:
    x = int(x_str)
    for n in ptree_map[p]:
        node_cnts[n] += x

print(*node_cnts[1:])

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
nodes = {}
for _ in range(N-1):
    a, b = input().split()
    if nodes.get(a) is None:
        nodes[a] = [b]
    else:
        nodes[a] += [b]

PX = [input().split() for _ in range(Q)]

ptree_map = {}


def gen_subtree_nodes(node):
    res = [int(node)]
    for n in nodes.get(node, []):
        subtree_nodes = gen_subtree_nodes(n)
        ptree_map[n] = subtree_nodes
        res += subtree_nodes

    return res


ptree_map['1'] = gen_subtree_nodes('1')

node_cnts = [0] * (N+1)
for p, x_str in PX:
    x = int(x_str)
    for n in ptree_map[p]:
        node_cnts[n] += x

print(*node_cnts[1:])

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
nodes = {}
for _ in range(N-1):
    a, b = input().split()
    if nodes.get(a) is None:
        nodes[a] = [b]
    else:
        nodes[a] += [b]


ptree_map = {}


def gen_subtree_nodes(node):
    res = [int(node)]
    for n in nodes.get(node, []):
        subtree_nodes = gen_subtree_nodes(n)
        ptree_map[n] = subtree_nodes
        res += subtree_nodes

    return res


ptree_map['1'] = gen_subtree_nodes('1')

node_cnts = [0] * (N+1)
for _ in range(Q):
    p, x_str = input().split()
    x = int(x_str)
    for n in ptree_map[p]:
        node_cnts[n] += x

print(*node_cnts[1:])