Problem p02936 (python)

Fixed submission:
import collections
import sys

input = sys.stdin.readline

n, q = map(int, input().split())
es = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    es[a].append(b)
    es[b].append(a)

ps = [0] * (n + 1)
for _ in range(q):
    p, v = map(int, input().split())
    ps[p] += v

remaining = collections.deque([[0, 1]])
while len(remaining) > 0:
    a, b = remaining.popleft()
    ps[b] += ps[a]
    for c in es[b]:
        if c != a:
            remaining.append([b, c])
print(*ps[1:])

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, index, parent_index):
        self.index = index
        self.parent_index = parent_index
        self.children = []
        self.value = 0

    def add_value(self, value):
        if self.index > 0:
            self.value += value
            for child in self.children:
                child.add_value(value)


class Tree:
    def __init__(self):
        self.node_indices = {}
        self.nodes = []

        self.add_node(1, 0)

    def add_node(self, index, parent_index):
        node = Node(index, parent_index)
        self.node_indices[index] = len(self.nodes)
        self.nodes.append(node)

        if index > 1:
            parent_node = self.nodes[self.node_indices[parent_index]]
            parent_node.children.append(node)

    def add_value(self, index, value):
        node = self.nodes[self.node_indices[index]]
        node.add_value(value)


def main():
    n, q = map(int, input().split())

    tree = Tree()
    for _ in range(n - 1):
        parent_index, child_index = map(int, input().split())
        tree.add_node(child_index, parent_index)

    for _ in range(q):
        index, value = map(int, input().split())
        tree.add_value(index, value)

    res = []
    for index in range(1, n + 1):
        node = tree.nodes[tree.node_indices[index]]
        res.append(node.value)
    print(' '.join(map(str, res)))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, index, parent_index):
        self.index = index
        self.parent_index = parent_index
        self.children = []
        self.value = 0

    def add_value(self, value):
        if self.index > 0:
            self.value += value
            for child in self.children:
                child.add_value(value)


class Tree:
    def __init__(self):
        self.node_indices = {}
        self.nodes = []

        self.add_node(1, 0)

    def add_node(self, index, parent_index):
        node = Node(index, parent_index)
        self.node_indices[index] = len(self.nodes)
        self.nodes.append(node)

        if index > 1:
            parent_node = self.nodes[self.node_indices[parent_index]]
            parent_node.children.append(node)

    def add_value(self, index, value):
        node = self.nodes[self.node_indices[index]]
        node.add_value(value)


def main():
    n, q = map(int, input().split())

    tree = Tree()
    for _ in range(n - 1):
        parent_index, child_index = map(int, input().split())
        tree.add_node(child_index, parent_index)

    for _ in range(q):
        index, value = map(int, input().split())
        print(index, value)
        tree.add_value(index, value)

    res = []
    for index in range(1, n + 1):
        node = tree.nodes[tree.node_indices[index]]
        res.append(node.value)
    print(' '.join(map(str, res)))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, index, parent_index):
        self.index = index
        self.parent_index = parent_index
        self.children = []
        self.value = 0

    def add_value(self, value):
        if self.index > 0:
            self.value += value
            for child in self.children:
                child.add_value(value)


class Tree:
    def __init__(self):
        self.node_indices = {}
        self.nodes = []

        self.add_node(1, 0)

    def add_node(self, index, parent_index):
        node = Node(index, parent_index)
        self.node_indices[index] = len(self.nodes)
        self.nodes.append(node)

        if index > 1:
            parent_node = self.nodes[self.node_indices[parent_index]]
            parent_node.children.append(node)

    def add_value(self, index, value):
        node = self.nodes[self.node_indices[index]]
        node.add_value(value)


def main():
    n, q = map(int, input().split())

    tree = Tree()
    for _ in range(n - 1):
        parent_index, child_index = map(int, input().split())
        tree.add_node(child_index, parent_index)

    for _ in range(q):
        index, value = map(int, input().split())
        print(index, value)
        tree.add_value(index, value)

    res = []
    for index in range(1, n + 1):
        node = tree.nodes[tree.node_indices[index]]
        res.append(node.value)
    print(' '.join(map(str, res)))


if __name__ == '__main__':
    main()