Problem p02936 (python)

Fixed submission:
from collections import defaultdict, deque


def main():
    N, Q = map(int, input().split())

    tree = defaultdict(list)
    counter = defaultdict(int)
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    for _ in range(Q):
        p, x = map(int, input().split())
        counter[p] += x

    # bfs
    q = deque([1])
    visited = {1}

    while q:
        i = q.popleft()

        for j in tree[i]:
            if j not in visited:
                q.append(j)
                counter[j] += counter[i]
                visited.add(j)

    counts = [counter[i] for i in range(1, N+1)]
    ans = " ".join(map(str, counts))
    print(ans)



if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def input_undirected_graph(V: int, E: int, origin: int = 1):
    graph = [list() for _ in range(V)]
    for i in range(E):
        u, v = [int(x) for x in input().split()]
        u -= origin
        v -= origin
        graph[u].append(v)
        graph[v].append(u)
    return graph


def main():
    N, Q = [int(x) for x in input().split()]
    graph = input_undirected_graph(N, N-1)

    vals = [0] * N
    for _ in range(Q):
        p, x = [int(x) for x in input().split()]
        vals[p-1] += x
    
    ans = [0] * N
    # stack = [(0, -1, 0)]  # node, par, val
    # while stack:
    #     node, par, val = stack.pop()
    #     ans[node] += vals[node] + val

    #     for child in graph[node]:
    #         if child == par:
    #             continue
    #         stack.append((child, node, ans[node]))

    def dfs(node, par, val):
        ans[node] += vals[node] + val
        for child in graph[node]:
            if child == par:
                continue
            dfs(child, node, ans[node])
    dfs(0,-1,0)

    for i in range(N):
        print(ans[i])
 
 
if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(20005)
    main()

Buggy submission (verdict: Runtime Error):
def input_undirected_graph(V: int, E: int, origin: int = 1):
    graph = [list() for _ in range(V)]
    for i in range(E):
        u, v = [int(x) for x in input().split()]
        u -= origin
        v -= origin
        graph[u].append(v)
        graph[v].append(u)
    return graph


def main():
    N, Q = [int(x) for x in input().split()]
    graph = input_undirected_graph(N, N-1)

    vals = [0] * N
    for _ in range(Q):
        p, x = [int(x) for x in input().split()]
        vals[p-1] += x
    
    ans = [0] * N
    def dfs(node, par, val):
        ans[node] += vals[node] + val

        for child in graph[node]:
            if child == par:
                continue
            dfs(child, node, ans[node])

    dfs(0, -1, 0)
    for i in range(N):
        print(ans[i])
 
 
if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10000)
    main()

Buggy submission (verdict: Runtime Error):
def main():
    N, Q = map(int, input().split())

    tree = [[] for _ in range(N)]
    f = [0] * N
    ans = [0] * N
    for _ in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        tree[a].append(b)
        tree[b].append(a)

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

    # dfs
    visited = {0}
    def dfs(i, s):
        ans[i] = s
        for j in tree[i]:
            if j in visited:
                continue
            visited.add(j)
            s += f[j]
            dfs(j, s)
            s -= f[j]

    dfs(0, f[0])

    print(*ans)


if __name__ == '__main__':
    main()

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


def main():
    N, Q = map(int, input().split())

    tree = defaultdict(list)
    f = defaultdict(int)
    ans = defaultdict(int)
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)

    for _ in range(Q):
        p, x = map(int, input().split())
        f[p] += x

    # dfs
    visited = {1}
    def dfs(i, s):
        ans[i] = s
        for j in tree[i]:
            if j in visited:
                continue
            visited.add(j)
            s += f[j]
            dfs(j, s)
            s -= f[j]

    dfs(1, f[1])

    counts = [ans[i] for i in range(1, N+1)]
    ans = " ".join(map(str, counts))
    print(ans)



if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def dfs(tree, count, p):
    for i in tree[p]:
        count[i] += count[p]
        dfs(tree, count, i)


def main():
    N, Q = map(int, input().split())
    tree = {i+1: [] for i in range(N)}
    count = {i+1: 0 for i in range(N)}

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

    for _ in range(Q):
        p, x = map(int, input().split())
        count[p] += x

    dfs(tree, count, 1)

    ans = [str(count[k]) for k in sorted(count.keys())]
    print(" ".join(ans))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def dfs(tree, count, p):
    for i in tree[p]:
        count[i] += count[p]
        dfs(tree, count, i)


def find_root(inv):
    return [k for k in inv if len(inv[k]) == 0][0]


def main():
    N, Q = map(int, input().split())
    tree = {i+1: [] for i in range(N)}
    inv = {i+1: [] for i in range(N)}
    count = {i+1: 0 for i in range(N)}

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

    for _ in range(Q):
        p, x = map(int, input().split())
        count[p] += x

    dfs(tree, count, find_root(inv))

    print(" ".join(map(str, sorted(count.values()))))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def dfs(tree, count, p):
    for i in tree[p]:
        count[i] += count[p]
        dfs(tree, count, i)


def find_root(inv):
    return [k for k in inv if len(inv[k]) == 0][0]


def main():
    N, Q = map(int, input().split())
    tree = {i+1: [] for i in range(N)}
    inv = {i+1: [] for i in range(N)}
    count = {i+1: 0 for i in range(N)}

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

    for _ in range(Q):
        p, x = map(int, input().split())
        count[p] += x

    dfs(tree, count, find_root(inv))

    print(" ".join(map(str, count.values())))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def dfs(tree, count, p, x):
    count[p] += x
    for i in tree[p]:
        dfs(tree, count, i, x)


def main():
    N, Q = map(int, input().split())
    tree = {i+1: [] for i in range(N)}
    count = {i+1: 0 for i in range(N)}

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

    for _ in range(Q):
        p, x = map(int, input().split())
        dfs(tree, count, p, x)

    print(" ".join(map(str, count.values())))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
class Node:

    def __init__(self, p):
        self.index = p
        self.count = 0

    def __repr__(self):
        return f"{self.index}({self.count})"


def dfs(tree, count, p, x):
    count[p] += x
    for node in tree[p]:
        dfs(tree, count, node.index, x)


def main():
    N, Q = map(int, input().split())
    tree = {i+1: [] for i in range(N)}
    count = {i+1: 0 for i in range(N)}

    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(Node(b))
    # pprint.pprint(tree)

    for _ in range(Q):
        p, x = map(int, input().split())
        dfs(tree, count, p, x)

    # pprint.pprint(count)
    print(" ".join(map(str, count.values())))


if __name__ == '__main__':
    main()

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


class Node:

    def __init__(self, p):
        self.index = p
        self.count = 0

    def __repr__(self):
        return f"{self.index}({self.count})"


def dfs(tree, count, p, x):
    # print("dfs", p, x)
    count[p] += x
    for node in tree[p]:
        dfs(tree, count, node.index, x)


def main():
    N, Q = map(int, input().split())
    tree = defaultdict(list)
    count = {i+1: 0 for i in range(N)}

    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(Node(b))
    # pprint.pprint(tree)

    for _ in range(Q):
        p, x = map(int, input().split())
        dfs(tree, count, p, x)

    # pprint.pprint(count)
    print(" ".join(map(str, count.values())))


if __name__ == '__main__':
    main()