Problem p02936 (python)

Fixed submission:
# https://atcoder.jp/contests/abc138/tasks/abc138_d

from collections import defaultdict
import sys
sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n, q = input_int_list()
    cnt = [0] * (n + 1)  # 1-indexed
    tree = defaultdict(list)

    for _ in range(n - 1):
        a, b = input_int_list()
        tree[a].append(b)
        tree[b].append(a)

    # 遅延評価をする O(q)
    for _ in range(q):
        p, x = input_int_list()
        cnt[p] += x

    # 遅延評価（DFS）O(n)
    stack = [(1, 0)]
    visited = set([1])
    while stack:
        e, add = stack.pop()
        cnt[e] += add
        for node in tree[e]:
            if node not in visited:
                visited.add(node)
                stack.append((node, cnt[e]))

    print(" ".join([str(i) for i in cnt[1:]]))
    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/abc138/tasks/abc138_d

from collections import defaultdict
import sys
sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n, q = input_int_list()
    cnt = [0] * (n + 1)  # 1-indexed
    tree = defaultdict(list)

    for _ in range(n - 1):
        a, b = input_int_list()
        tree[a].append(b)
        tree[b].append(a)

    # 遅延評価をする O(q)
    for _ in range(q):
        p, x = input_int_list()
        cnt[p] += x

    # 遅延評価（DFS）O(n)
    def tree_walk(root, add):
        cnt[root] += add
        for node in tree[root]:
            if node not in visited:
                visited.add(node)
                tree_walk(node, cnt[root])

    visited = set([1])
    # ルート(1)から結果を評価する
    tree_walk(1, 0)

    print(" ".join([str(i) for i in cnt[1:]]))
    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/abc138/tasks/abc138_d

from collections import deque
from collections import defaultdict
import sys
sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n, q = input_int_list()
    cnt = [0] * (n + 1)  # 1-indexed
    tree = defaultdict(list)

    for _ in range(n - 1):
        a, b = input_int_list()
        tree[a].append(b)
        tree[b].append(a)

    # 遅延評価をする
    for _ in range(q):
        p, x = input_int_list()
        cnt[p] += x

    # 遅延評価（DFS）
    def tree_walk(root, add):
        cnt[root] += add
        for node in tree[root]:
            if node not in visited:
                visited.add(node)
                tree_walk(node, cnt[root])

    visited = set([1])
    # ルート(1)から結果を評価する
    tree_walk(1, 0)

    print(*cnt[1:])
    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/abc138/tasks/abc138_d

from collections import deque
from collections import defaultdict
import sys
sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n, q = input_int_list()
    cnt = [0] * (n + 1)  # 1-indexed
    tree = defaultdict(list)
    depth = [0] * (n + 1)  # 1-indexed

    for _ in range(n - 1):
        a, b = input_int_list()
        tree[a].append(b)
        tree[b].append(a)

    dq = deque()

    dq.append((1, 0))  # node,depth
    visited = set([1])

    while dq:
        root, dep = dq.popleft()
        for node in tree[root]:
            if node not in visited:
                visited.add(node)
                depth[node] = dep + 1
                dq.append((node, dep + 1))

    def tree_walk(root, x):
        cnt[root] += x
        d = depth[root]
        for node in tree[root]:
            if node not in visited and d < depth[node]:
                visited.add(node)
                tree_walk(node, x)

    for _ in range(q):
        p, x = input_int_list()
        visited = set([p])
        tree_walk(p, x)

    print(*cnt[1:])
    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/abc138/tasks/abc138_d

import sys
# sys.setrecursionlimit(100000)
from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n, q = input_int_list()
    cnt = [0] * (n + 1)  # 1-indexed
    tree = defaultdict(list)

    for _ in range(n - 1):
        a, b = input_int_list()
        tree[a].append(b)

    def tree_walk(root, x):
        cnt[root] += x
        for node in tree[root]:
            tree_walk(node, x)

    for _ in range(q):
        p, x = input_int_list()
        tree_walk(p, x)

    print(*cnt[1:])
    return


if __name__ == "__main__":
    main()