Problem p02936 (python)

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    # edges = [None]*N
    edges = [[] for _ in range(N)]
    # edges[0] = 0
    # edges[0].append = 0
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[b-1].append(a-1)
        edges[a-1].append(b-1)

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

    parent_nodes = deque()
    parent_nodes.append(0)
    visited_nodes = set()
    while parent_nodes:
        parent = parent_nodes.popleft()
        if parent in visited_nodes:
            continue
        visited_nodes.add(parent)
        for child in edges[parent]:
            if child in visited_nodes:
                continue
            counts[child] += counts[parent]
            parent_nodes.append(child)

    print(' '.join(list(map(str, counts))))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    edges = [[] for _ in range(N)]
    edges[0].append(0)
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[b-1].append(a-1)

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

    print(counts[0], end=' ')
    for i in range(1, N):
        counts[i] += counts[edges[i][0]]
        print(counts[i], end=' ')

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
edges = [[] for _ in range(N)]
edges[0] = 0
for _ in range(N-1):
    a, b = map(int, input().split())
    edges[b-1] = a-1

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

print(counts[0], end=' ')
for i in range(1, N):
    counts[i] += counts[edges[i]]
    print(counts[i], end=' ')

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
edges = [None]*N
edges[0] = 0
for _ in range(N-1):
    a, b = map(int, input().split())
    edges[max(a, b)-1] = min(a, b)-1

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

print(counts[0], end=' ')
for i in range(1, N):
    counts[i] += counts[edges[i]]
    print(counts[i], end=' ')

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
edges = [None]*N
edges[0] = 0
for _ in range(N-1):
    a, b = map(int, input().split())
    edges[b-1] = a-1

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

print(counts[0], end=' ')
for i in range(1, N):
    counts[i] += counts[edges[i]]
    print(counts[i], end=' ')

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
input = sys.stdin.readline

N, Q = map(int, input().split())
children = [[i] for i in range(N)]  # 各頂点の持つ部分木の頂点の集合
edges = [tuple(map(int, input().split())) for _ in range(N-1)][::-1]
counts = np.zeros(N, dtype=np.int32)

for a, b in edges:
    children[a-1] += children[b-1]

for _ in range(Q):
    p, x = map(int, input().split())
    counts[children[p-1]] += x

for count in counts:
    print(count, end=' ')