Problem p02936 (python)

Fixed submission:
#python3
from collections import deque 
def main():
    n, q = map(int, input().split())
    to = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        to[a].append(b)
        to[b].append(a)

    node = [0 for _ in range(n)]

    for _ in range(q):
        p, x = map(int, input().split())
        node[p-1] += x

    dq = deque() 
    dq.append(0)
    done = [0 for _ in range(n)]
    done[0] = 1
    while len(dq) > 0:
        x = dq.popleft()
        for i in to[x]:
            if done[i] == 1:
                continue
            node[i] += node[x]
            done[i] = 1 
            dq.append(i)

    for i in range(n):
        print(node[i]) if i == n-1 else print(node[i], end=' ')
main()

Buggy submission (verdict: Runtime Error):
#python3

def main():
    n, q = map(int, input().split())
    to = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        to[a].append(b)

    node = [0 for _ in range(n)]
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        node[p] += x  
    def dfs(v, p=-1):
        for u in to[v]:
            if u == p:
                continue
            else:
                node[u] += node[v]
                dfs(u, v)
    dfs(0)
    for i in range(n):
        if i == n-1:
            print(node[i])
        else:
            print(node[i], end=' ')
main()

Buggy submission (verdict: Runtime Error):
#python3
import sys
sys.setrecursionlimit(2000)

def main():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        tree[a].append(b)
    op = []
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        op.append((p, x))
    
    cntr = [0 for _ in range(n)]
    
    def dfs(node, p):
        lng = len(tree[node])
        cntr[node] += p
        if lng == 0:
            return
        for i in range(lng):
            next_node = tree[node][i]
            dfs(next_node, p)

    for node, p in op:
        dfs(node, p)
    for i in range(n):
        if i == n-1:
            print(cntr[i])
        else:
            print(cntr[i], end=' ')
main()

Buggy submission (verdict: Runtime Error):

def main():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        tree[a].append(b)
    op = []
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        op.append((p, x))
    
    cntr = [0 for _ in range(n)]
    
    def dfs(node, p):
        lng = len(tree[node])
        cntr[node] += p
        if lng == 0:
            return
        for i in range(lng):
            next_node = tree[node][i]
            dfs(next_node, p)

    for node, p in op:
        dfs(node, p)
    for i in range(n):
        if i == n-1:
            print(cntr[i])
        else:
            print(cntr[i], end=' ')
main()

Buggy submission (verdict: Runtime Error):
#python3

def main():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        tree[a].append(b)
    op = []
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        op.append((p, x))
    
    cntr = [0 for _ in range(n)]
    
    def dfs(node, p):
        lng = len(tree[node])
        cntr[node] += p
        if lng == 0:
            return
        for i in range(lng):
            next_node = tree[node][i]
            dfs(next_node, p)

    for node, p in op:
        dfs(node, p)
    for i in range(n):
        if i == n-1:
            print(cntr[i])
        else:
            print(cntr[i], end=' ')
main()