Problem p02936 (python)

Fixed submission:

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

# edge = [[] for _ in range(n)]

# counter = [0] * n

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

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

# parent = [0]
# visited = []

# for i in parent:
#     visited.append(i)
#     for v in edge[i]:
#         if v in visited:
#             continue
#         parent.append(v)
#         counter[v] += counter[i]
        
# for i in range(n):
#     print(counter[i], end=" ")
# print("")

import sys
input = sys.stdin.readline
 
N, Q = [int(x) for x in input().split()]
 
edge = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    edge[a - 1].append(b - 1)
    edge[b - 1].append(a - 1)
 
cost = [0] * N
for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    cost[p - 1] += x
 
next = [0]
s_next = {0}
for i in next:
    for v in edge[i]:
        if v in s_next:
            continue
        s_next.add(v)
        next.append(v)
        cost[v] += cost[i]
 
print(" ".join(map(str, cost)))

Buggy submission (verdict: Runtime Error):

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

edge = [[] for _ in range(n)]

counter = [0] * n

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

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

parent = 0
child = 1
while True:
    while len(edge[parent]) == 0:
        parent += 1
        if parent == n-1:
            break
    if parent == n-1:
        break
    child = edge[parent].pop(0)
    edge[child].pop(0)
    counter[child] += counter[parent]
    

for i in range(n):
    print(counter[i], end=" ")
print("")

Buggy submission (verdict: Runtime Error):
#not clear
class Node:
    def __init__(self,id):
        self.id = id
        self.data = 0
        self.child = []
    
    def register(self, node):
        self.child.append(node)
    
    def add(self, x):
        self.data += x
        for i in range(len(self.child)):
            self.child[i].add(x)

    def search(self, id):
        if self.id == id:
            return self
        else:
            for i in range(len(self.child)):
                node = self.child[i].search(id)
                if node != -1:
                    return node
            return -1

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

a, b = map(int, input().split())
root_node = Node(a)
node = Node(b)
root_node.register(node)

for i in range(n-2):
    a, b = map(int, input().split())
    node1 = root_node.search(a)
    node2 = Node(b)
    node1.register(node2)


for i in range(q):
    a, b = map(int, input().split())
    node = root_node.search(a)
    node.add(b)

for i in range(1, n+1):
    node = root_node.search(i)
    print(node.data, end=" ")

Buggy submission (verdict: Runtime Error):

def add(tree,i, num):
    tree[i-1][0] += num
    for j in range(1,len(tree[i-1])):  
        add(tree, tree[i-1][j], num)

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

for i in range(q):
    a, b = map(int, input().split())
    add(tree, a, b)

for i in range(n):
    print(tree[i][0], end=" ")