Problem p02936 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import deque

n, q = map(int, input().split())

counts = [0] * (n+1)
adjs = {a: [] for a in range(1,n+1)}

for i in range(n-1):
    a, b = map(int, input().split())
    adjs[a].append(b)
    adjs[b].append(a)

costs = [[int(x) for x in input().split()] for i in range(q)]

for cost in costs:
    counts[cost[0]] += cost[1]

visited = set()
queue = deque()
queue.append(1)

while len(queue) > 0:
    a = queue.popleft()
    visited.add(a)
    for b in adjs[a]:
        if b in visited:
            continue
        counts[b] += counts[a]
        queue.append(b)

for c in counts[1:]:
    print(c, end=" ")
print()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys

n, q = map(int, input().split())

counts = [0] * (n+1)
adjs = {a: [] for a in range(1,n+1)}

for i in range(n-1):
    a, b = map(int, input().split())
    adjs[a].append(b)
    adjs[b].append(a)

costs = [[int(x) for x in input().split()] for i in range(q)]

for cost in costs:
    counts[cost[0]] += cost[1]

def visit(v: int, parent: int):
    counts[v] += counts[parent]
    for child in adjs[v]:
        if child == parent:
            continue
        visit(child, v)

visit(1, 0)

print(' '.join(list(map(lambda x: str(x), counts[1:]))))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys

class Tree:
    def __init__(self, v):
        self.v = 0
        self.counter = 0
        self.leaves = []
        self.offspring = []

[n, q] = [int(x) for x in sys.stdin.readline().rstrip().split()]

a = [0] * (n-1)
b = [0] * (n-1)
for i in range(n-1):
    [a[i], b[i]] = [int(x) for x in sys.stdin.readline().rstrip().split()]
p = [0] * q
x = [0] * q
for i in range(q):
    [p[i], x[i]] = [int(x) for x in sys.stdin.readline().rstrip().split()]

trees = [Tree(0)]
tree_dict = {}

for i in range(1,n+1):
    tree = Tree(i)
    trees.append(tree)
    tree_dict[i] = tree
    
root = tree_dict[1]
        
for i in range(n-1):
    tree_dict[a[i]].leaves.append(tree_dict[b[i]])

def rec_offspring(t: Tree):
    for c in t.leaves:
        rec_offspring(c)
    for c in t.leaves:
        t.offspring.extend(c.offspring)
    t.offspring.extend(t.leaves)

rec_offspring(trees[1])

for i in range(q):
    t = tree_dict[p[i]]
    t.counter += x[i]
    for c in t.offspring:
        c.counter += x[i]

print(' '.join(list(map(lambda x: str(x.counter), trees[1:]))))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys

class Tree:
    def __init__(self, v):
        self.v = 0
        self.counter = 0
        self.leaves = []

[n, q] = [int(x) for x in sys.stdin.readline().rstrip().split()]

a = [0] * (n-1)
b = [0] * (n-1)
for i in range(n-1):
    [a[i], b[i]] = [int(x) for x in sys.stdin.readline().rstrip().split()]
p = [0] * q
x = [0] * q
for i in range(q):
    [p[i], x[i]] = [int(x) for x in sys.stdin.readline().rstrip().split()]

trees = [Tree(0)]
tree_dict = {}

for i in range(1,n+1):
    tree = Tree(i)
    trees.append(tree)
    tree_dict[i] = tree
    
root = tree_dict[1]
        
for i in range(n-1):
    tree_dict[a[i]].leaves.append(tree_dict[b[i]])

def rec_add(t: Tree, x: int):
    t.counter += x
    for child in t.leaves:
        rec_add(child, x)

for i in range(q):
    rec_add(tree_dict[p[i]], x[i])

print(' '.join(list(map(lambda x: str(x.counter), trees[1:]))))