Problem p02936 (python)

Fixed submission:
# -*- coding: utf-8 -*-
# 整数の入力
# from functools import reduce
import sys
from collections import defaultdict

sys.setrecursionlimit(1000000)


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

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

    values = [0 for i in range(n+1)]
    # p, x
    for _ in range(0, q):
        p, x = map(int, input().split())
        values[p] += x
    process(1, -1, trees, values)
    print(*values[1:])


def process(i, parent, trees, values):
    for j in trees[i]:
        if j == parent:
            continue
        values[j] += values[i]
        process(j, i, trees, values)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# 整数の入力
# from functools import reduce
from collections import defaultdict

sys.setrecursionlimit(1000000)


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

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

    values = [0 for i in range(n+1)]
    # p, x
    for _ in range(0, q):
        p, x = map(int, input().split())
        values[p] += x
    process(1, -1, trees, values)
    print(*values[1:])


def process(i, parent, trees, values):
    for j in trees[i]:
        if j == parent:
            continue
        values[j] += values[i]
        process(j, i, trees, values)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# 整数の入力
# from functools import reduce
from collections import defaultdict


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

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

    values = [0 for i in range(n+1)]
    # p, x
    for _ in range(0, q):
        p, x = map(int, input().split())
        values[p] += x
    process(1, -1, trees, values)
    print(*values[1:])


def process(i, parent, trees, values):
    for j in trees[i]:
        if j == parent:
            continue
        values[j] += values[i]
        process(j, i, trees, values)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# 整数の入力
# from functools import reduce
from collections import defaultdict


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

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

    values = [0 for i in range(n+1)]
    # p, x
    for _ in range(0, q):
        p, x = map(int, input().split())
        values[p] += x
    print(trees)
    process(1, -1, trees, values)
    print(*values[1:])


def process(i, parent, trees, values):
    for j in trees[i]:
        if j == parent:
            continue
        values[j] += values[i]
        process(j, i, trees, values)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# 整数の入力
# from functools import reduce
from collections import defaultdict


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

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

    # p, x
    p = []
    x = []
    for _ in range(0, q):
        pi, xi = map(int, input().split())
        p.append(pi)
        x.append(xi)

    print(process(n, q, a, b, p, x))


def process(n, q, a, b, p, x):
    points = [0] * (n + 1)
    graph = defaultdict(list)
    for i in range(0, n - 1):
        graph[a[i]].append(b[i])

    for i in range(0, q):
        target = p[i]
        xi = x[i]
        subtree(points, target, graph, xi)

    # print(points)
    points.pop(0)

    points_sorted = map(str, points)
    return ' '.join(points_sorted)


def subtree(points, target, graph, x):
    points[target] += x
    for i in graph[target]:
        subtree(points, i, graph, x)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# 整数の入力
# from functools import reduce
from collections import defaultdict


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

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

    # p, x
    p = []
    x = []
    for _ in range(0, q):
        pi, xi = map(int, input().split())
        p.append(pi)
        x.append(xi)

    print(process(n, q, a, b, p, x))


def process(n, q, a, b, p, x):
    points = defaultdict(int)
    graph = defaultdict(list)
    for i in range(0, n - 1):
        graph[a[i]].append(b[i])

    for i in range(0, q):
        target = p[i]
        xi = x[i]
        subtree(points, target, graph, xi)

    points_sorted = map(lambda x: str(x[1]), sorted(
        points.items(), key=lambda x: x[0]))
    return ' '.join(points_sorted)


def subtree(points, target, graph, x):
    points[target] += x
    for i in graph[target]:
        subtree(points, i, graph, x)


if __name__ == "__main__":
    main()