Problem p02936 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,Q = map(int,readline().split())
ABPX = list(map(int,read().split()))
AB = iter(ABPX[:N+N-2])
PX = iter(ABPX[N+N-2:])

graph = [[] for _ in range(N+1)]
for a,b in zip(AB,AB):
    graph[a].append(b)
    graph[b].append(a)

val = [0] * (N+1)
for p,x in zip(PX,PX):
    val[p] += x

stack = [1]

parent = [0] * (N+1)
while stack:
    x = stack.pop()
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
        val[y] += val[x]

print(' '.join(map(str,val[1:])))

Buggy submission (verdict: Runtime Error):
import sys
inputs = sys.stdin.readlines()

from collections import deque, defaultdict

def dfs_generator(nodes, start):
    nexts = deque([start])
    while nexts:
        node = nexts.popleft()
        nexts.extend(nodes[node]['childs'])
        yield node

nodes = dict({1: dict({'val': 0, 'childs': []})})
for x in inputs[1:n]:
    from_, to_ = map(int, x.split())
    nodes[from_]['childs'].append(to_)
    nodes[to_] = dict({'val': 0, 'childs': []})
'''
subtree_roots = defaultdict(int)
for x in inputs[n:]:
    n, v = map(int, x.split())
    subtree_roots[n] += v

subtrees = defaultdict(list)
for r, v in subtree_roots.items():
    gen = dfs_generator(nodes, r)
    try:
        while True:
            node = next(gen)
            subtrees[r].append(node) 
    except StopIteration:
        pass
    for n in subtrees[r]:
        nodes[n]['val'] += v

for i in range(1, n):
    print(str(nodes[i]['val']), end=' ')
print(str(nodes[i+1]['val']))
'''

Buggy submission (verdict: Runtime Error):
import sys
inputs = sys.stdin.readlines()

from collections import deque, defaultdict

def dfs_generator(nodes, start):
    nexts = deque([start])
    while nexts:
        node = nexts.popleft()
        nexts.extend(nodes[node]['childs'])
        yield node

nodes = dict({1: dict({'val': 0, 'childs': []})})
for x in inputs[1:n]:
    from_, to_ = map(int, x.split())
    nodes[from_]['childs'].append(to_)
    nodes[to_] = dict({'val': 0, 'childs': []})

subtree_roots = defaultdict(int)
for x in inputs[n:]:
    n, v = map(int, x.split())
    subtree_roots[n] += v
'''
subtrees = defaultdict(list)
for r, v in subtree_roots.items():
    gen = dfs_generator(nodes, r)
    try:
        while True:
            node = next(gen)
            subtrees[r].append(node) 
    except StopIteration:
        pass
    for n in subtrees[r]:
        nodes[n]['val'] += v

for i in range(1, n):
    print(str(nodes[i]['val']), end=' ')
print(str(nodes[i+1]['val']))
'''

Buggy submission (verdict: Runtime Error):
import sys
inputs = sys.stdin.readlines()

n, q = list(map(int, inputs[0].split()))
edges = []
for i in range(n-1):
    edges.append(tuple(map(int, inputs[i+1].split())))
p_x_list = []
for i in range(q):
    p_x_list.append(tuple(map(int, inputs[n+i].split())))

from collections import deque, defaultdict

def dfs_generator(nodes, start):
    nexts = deque([start])
    while nexts:
        node = nexts.popleft()
        nexts.extend(nodes[node]['childs'])
        yield node

nodes = dict({1: dict({'val': 0, 'childs': []})})
for from_, to_ in edges:
    nodes[from_]['childs'].append(to_)
    nodes[to_] = dict({'val': 0, 'childs': []})

part_tree_roots = defaultdict(int)
for p, x in p_x_list:
    part_tree_roots[p] += x

part_tree = defaultdict(list)
for r in part_tree_roots:
    gen = dfs_generator(nodes, r)
    try:
        while True:
            part_tree[r].append(next(gen))
    except StopIteration:
        pass

for n, v in part_tree_roots.items():
    for i in part_tree[n]:
        nodes[i]['val'] += v

for i in range(n-1):
    print(str(nodes[i+1]['val']), end=' ')
print(str(nodes[i+2]['val']))

Buggy submission (verdict: Runtime Error):
import sys
inputs = sys.stdin.readlines()

n, q = list(map(int, inputs[0].split()))
edges = []
for i in range(n-1):
    edges.append(tuple(map(int, inputs[i+1].split())))
p_x_list = []
for i in range(q):
    p_x_list.append(tuple(map(int, inputs[n+i].split())))
n, q
edges
p_x_list

from collections import deque, defaultdict

def dfs_generator(nodes, start):
    nexts = deque([start])
    while nexts:
        node = nexts.popleft()
        nexts.extend(nodes[node]['childs'])
        yield node

nodes = dict({1: dict({'val': 0, 'parent': 0, 'childs': []})})
for from_, to_ in edges:
    nodes[from_]['childs'].append(to_)
    nodes[to_] = dict({'val': 0, 'parent': from_, 'childs': []})

part_tree_roots = set([p for p, x in p_x_list])

part_tree = defaultdict(list)
for r in part_tree_roots:
    gen = dfs_generator(nodes, r)
    try:
        while True:
            part_tree[r].append(next(gen))
    except StopIteration:
        pass

for p, x in p_x_list:
    for i in part_tree[p]:
        nodes[i]['val'] += x

for i in range(n-1):
    print(str(nodes[i+1]['val']), end=' ')
print(str(nodes[i+2]['val']))

Buggy submission (verdict: Runtime Error):
import sys
inputs = sys.stdin.readlines()

n, q = list(map(int, inputs[0].split()))
edges = []
for i in range(n-1):
    edges.append(tuple(map(int, inputs[i+1].split())))
p_x_list = []
for i in range(q):
    p_x_list.append(tuple(map(int, inputs[n+i].split())))
    
from collections import deque

def dfs_generator(nodes, start):
    nexts = deque([start])
    while nexts:
        node = nexts.popleft()
        nexts.extend(nodes[node]['childs'])
        yield node

nodes = dict({1: dict({'val': 0, 'parent': 0, 'childs': []})})
for from_, to_ in edges:
    nodes[from_]['childs'].append(to_)
    nodes[to_] = dict({'val': 0, 'parent': from_, 'childs': []})

for p, x in p_x_list:
    gen = dfs_generator(nodes, p)
    try:
        while True:
            nodes[next(gen)]['val'] += x
    except StopIteration:
        pass

for i in range(n-1):
    print(str(nodes[i+1]['val']), end=' ')
print(str(nodes[i+2]['val']))