Problem p02936 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10**6) #再帰の回数制限を増やす
input = sys.stdin.readline


def add_num(i, total, parent=-1):
    """隣接リストを取り出して深さ優先探索
    自分より上層の total のカウント数と部分木の頂点としてのカウント数を足す"""
    for p in adj[i]:
        if p != parent:
            ans_lst[p] = total + tmp[p]
            add_num(p, total + tmp[p], i)
    return
    
    
N, Q = [int(i) for i in input().split()]

adj = [[] for _ in range(N)] # 隣接リスト
tmp = [0] * N # 部分木の根としてのカウント数 
ans_lst = [0] * N # 答えのリスト

# 隣接リストの作成
for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    adj[a].append(b)
    adj[b].append(a)

# ｔｍｐリストの作成
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    tmp[p-1] += x

ans_lst[0] = tmp[0]
total = tmp[0]
add_num(0, total)

print(*ans_lst, sep=' ')

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(2 * 10**5) #再帰の回数制限を増やす
input = sys.stdin.readline


def add_num(i, total, parent=-1):
    """隣接リストを取り出して深さ優先探索
    自分より上層の total のカウント数と部分木の頂点としてのカウント数を足す"""
    for p in adj[i]:
        if p != parent:
            ans_lst[p] = total + tmp[p]
            add_num(p, total + tmp[p], i)
    return
    
    
N, Q = [int(i) for i in input().split()]

adj = [[] for _ in range(N)] # 隣接リスト
tmp = [0] * N # 部分木の根としてのカウント数 
ans_lst = [0] * N # 答えのリスト

# 隣接リストの作成
for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    adj[a].append(b)
    adj[b].append(a)

# ｔｍｐリストの作成
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    tmp[p-1] += x

ans_lst[0] = tmp[0]
total = tmp[0]
add_num(0, total)

print(*ans_lst, sep=' ')

Buggy submission (verdict: Runtime Error):
from collections import defaultdict


def add_num(i, adj, tmp, ans_lst, total):
    for p in adj[i]:
        ans_lst[p] = total + tmp[p]
        if adj[p]:
            add_num(p, adj, tmp, ans_lst, total + tmp[p])
    
    
N, Q = [int(i) for i in input().split()]

adj = [[] * N for _ in range(N)]
tmp = defaultdict(int)
ans_lst = [0] * N


for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    adj[a].append(b)
    
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    tmp[p-1] += x


ans_lst[0] = tmp[0]
total = tmp[0]
add_num(0, adj, tmp, ans_lst, total)

print(*ans_lst, sep=' ')

Buggy submission (verdict: Runtime Error):
def search(lst, x, al, A, tmp, key):

    for p in lst:
        if p in tmp:
            for v in tmp[p]:
                A[v] += x
        else:
            A[p] += x
            tmp[key].add(p)
            search(al[p], x, al, A, tmp, key)

N, Q = [int(i) for i in input().split()]

al = [[] * N for _ in range(N)]
tmp = {}
A = [0] * N


for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    al[a].append(b)
    
for i in range(Q):
    p, x = [int(i) - 1 for i in input().split()]
    if p in tmp:
        for v in tmp[p]:
            A[v] += x + 1
            continue
    else:
        A[p] += (x + 1)
        tmp[p] = {p}
        search(al[p], x + 1, al, A, tmp, p)

print(*A, sep=' ')

Buggy submission (verdict: Runtime Error):
def search(lst, x, al, A):
    if not lst:
        return
    for p in lst:
        A[p] += x
        search(al[p], x, al, A)

N, Q = [int(i) for i in input().split()]

al = [[] * N for _ in range(N)]
A = [0] * N

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

for i in range(Q):
    p, x = [int(i) - 1 for i in input().split()]
    A[p] += (x + 1)
    search(al[p], x + 1, al, A)

print(*A, sep=' ')