Problem p02936 (python)

Fixed submission:
from collections import defaultdict

N, Q = map(int, input().split())
d = defaultdict(list)

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

c = defaultdict(int)

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

ans = [0 for i in range(N)]
que = [(1, 0, -1)]
while len(que) > 0:
    tmp = []
    for v, x, par in que:
        x += c[v]
        ans[v - 1] = str(x)
        for w in d[v]:
            if w != par:
                tmp.append((w, x, v))
    que = tmp

print(*ans)

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, index):
        self.index = index
        self.leaf = set()
        self.score = 0

    def set_parent(self, parent):
        self.leaf.remove(parent)
        for l in self.leaf:
            tree[l].set_parent(self.index)

    def add_score(self, score):
        self.score += score
        for l in self.leaf:
            tree[l].add_score(score)


N, Q = map(int, input().split())
print(N, Q)
# tree = {i: Node(i) for i in range(1, N + 1)}
tree = {}
for i in range(1, N+1):
    tree[i] = Node(i)
print(len(tree))
tree[1].leaf.add(1)
for i in range(N-1):
    n1, n2 = map(int, input().split())
    tree[n1].leaf.add(n2)
    tree[n2].leaf.add(n1)

tree[1].set_parent(1)

for _ in range(Q):
    p, x = map(int, input().split())
    tree[p].add_score(x)

print(' '.join([str(t.score) for t in tree.values()]))

Buggy submission (verdict: Runtime Error):
class Node:
    def __init__(self, index):
        self.index = index
        self.leaf = set()
        self.score = 0

    def set_parent(self, parent):
        self.leaf.remove(parent)
        for l in self.leaf:
            tree[l].set_parent(self.index)

    def add_score(self, score):
        self.score += score
        for l in self.leaf:
            tree[l].add_score(score)


N, Q = map(int, input().split())
tree = {i: Node(i) for i in range(1, N+1)}
tree[1].leaf.add(1)
tmp = [tuple(map(int, input().split())) for _ in range(N-1)]
for i in range(len(tmp)):
    n1, n2 = tmp[i][0], tmp[i][1]
    tree[n1].leaf.add(n2)
    tree[n2].leaf.add(n1)

tree[1].set_parent(1)

for p, x in [tuple(map(int, input().split())) for _ in range(Q)]:
    tree[p].add_score(x)

print(' '.join([str(t.score) for t in tree.values()]))

Buggy submission (verdict: Runtime Error):
import numpy as np

N, Q = map(int, input().split())


class Node:
    def __init__(self, index):
        self.index = index
        self.leaf = set()
        self.score = 0

    def set_parent(self, parent):
        self.leaf.remove(parent)
        for l in self.leaf:
            tree[l].set_parent(self.index)

    def add_score(self, score):
        self.score += score
        for l in self.leaf:
            tree[l].add_score(score)


tree = {1: Node(1)}
tree[1].leaf.add(1)
for i in [tuple(map(int, input().split())) for _ in range(N-1)]:
    n1, n2 = i[0], i[1]
    if n1 not in tree:
        tree[n1] = Node(n1)
    if n2 not in tree:
        tree[n2] = Node(n2)
    tree[n1].leaf.add(n2)
    tree[n2].leaf.add(n1)

tree[1].set_parent(1)

for p, x in [tuple(map(int, input().split())) for _ in range(Q)]:
    tree[p].add_score(x)

print(' '.join([str(t.score) for t in tree.values()]))

Buggy submission (verdict: Runtime Error):
import numpy as np

N, Q = map(int, input().split())


class Node:
    def __init__(self, parent):
        self.parent = parent
        self.leaf = []
        self.score = 0

    def add_score(self, score):
        self.score += score
        for l in self.leaf:
            l.add_score(score)


tree = {1: Node(None)}
tmp = np.array([list(map(int, input().split())) for _ in range(N-1)])
for i in np.argsort(tmp[:, 0]):
    p, c = tmp[i][0], tmp[i][1]
    if p not in tree:
        tree[p] = Node(None)
    if c not in tree:
        tree[c] = Node(p)
    tree[p].leaf.append(tree[c])

for p, x in [tuple(map(int, input().split())) for _ in range(Q)]:
    tree[p].add_score(x)

print(' '.join([str(t.score) for t in tree.values()]))

Buggy submission (verdict: Runtime Error):
import numpy as np

N, Q = map(int, input().split())


class Node:
    def __init__(self, parent):
        self.parent = parent
        self.leaf = []
        self.score = 0

    def add_score(self, score):
        self.score += score
        for l in self.leaf:
            l.add_score(score)


tree = {1: Node(None)}
tmp = np.array([list(map(int, input().split())) for _ in range(N-1)])
for i in np.argsort(tmp[:, 0]):
    p, c = tmp[i][0], tmp[i][1]
    if p not in tree:
        tree[p] = Node(None)
    if c not in tree:
        tree[c] = tree[p]
    tree[p].leaf.append(tree[c])

for p, x in [tuple(map(int, input().split())) for _ in range(Q)]:
    tree[p].add_score(x)

print(' '.join([str(t.score) for t in tree.values()]))

Buggy submission (verdict: Runtime Error):
import numpy as np

N, Q = map(int, input().split())


class Node:
    def __init__(self, parent):
        self.parent = parent
        self.leaf = []
        self.score = 0

    def add_score(self, score):
        self.score += score
        for l in self.leaf:
            l.add_score(score)


tree = {1: Node(None)}
tmp = np.array([list(map(int, input().split())) for _ in range(N-1)])
for i in np.argsort(tmp[:, 0]):
    p, c = tmp[i][0], tmp[i][1]
    if p not in tree:
        tree[p] = Node(None)
    if c not in tree:
        tree[c] = Node(p)
    tree[p].leaf.append(tree[c])

for p, x in [tuple(map(int, input().split())) for _ in range(Q)]:
    tree[p].add_score(x)

print(' '.join([str(t.score) for t in tree.values()]))

Buggy submission (verdict: Runtime Error):
import numpy as np

N, Q = map(int, input().split())


class Node:
    def __init__(self, parent):
        self.parent = parent
        self.leaf = []
        self.score = 0

    def add_score(self, score):
        self.score += score
        for l in self.leaf:
            tree[l].add_score(score)


tree = {1: Node(None)}
tmp = np.array([list(map(int, input().split())) for _ in range(N-1)])
for i in np.argsort(tmp[:, 0]):
    p, c = tmp[i][0], tmp[i][1]
    if p not in tree:
        tree[p] = Node(None)
    tree[p].leaf.append(c)
    if c not in tree:
        tree[c] = Node(p)

for p, x in [tuple(map(int, input().split())) for _ in range(Q)]:
    tree[p].add_score(x)

print(' '.join([str(t.score) for t in tree.values()]))

Buggy submission (verdict: Runtime Error):
import numpy as np

N, Q = map(int, input().split())


class Node:
    def __init__(self, parent):
        self.parent = parent
        self.leaf = []
        self.score = 0

    def add_score(self, score):
        self.score += x
        for l in self.leaf:
            tree[l].add_score(x)


tree = {1: Node(None)}
tmp = np.array([list(map(int, input().split())) for _ in range(N-1)])
for i in np.argsort(tmp[:, 0]):
    p, c = tmp[i][0], tmp[i][1]
    if p not in tree:
        tree[p] = Node(None)
    tree[p].leaf.append(c)
    if c not in tree:
        tree[c] = Node(p)

for p, x in [tuple(map(int, input().split())) for _ in range(Q)]:
    tree[p].add_score(x)

print(' '.join([str(t.score) for t in tree.values()]))