Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x
    G_color = ['WHITE' for _ in range(len(G))]
    G_parent = ['NIL' for _ in range(len(G))]

    def bfs(s):
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    G_parent[v] = u
                    Q.append(v)
            G_color[u] = 'BLACK'

    def dfs(v):
        seen = [False] * N
        todo = []
        seen[v] = True
        todo.append(v)
        while todo:
            u = todo[-1]
            for next_v in G[u]:
                if seen[next_v] or next_v == G_parent[u]:
                    continue
                seen[next_v] = True
                todo.append(next_v)
                cost[next_v] += cost[G_parent[next_v]]
                break
            else:
                todo.pop()

    bfs(0)
    dfs(0)
    print(*cost)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x

    def bfs(s):
        G_color = ['WHITE' for _ in range(len(G))]
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    cost[v] += cost[u]
                    Q.append(v)
            G_color[u] = 'BLACK'

    bfs(0)
    print(*cost)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    G_color = ['WHITE' for _ in range(len(G))]
    G_parent = ['NIL' for _ in range(len(G))]

    def bfs(s):
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    G_parent[v] = u
                    Q.append(v)
            G_color[u] = 'BLACK'

    def dfs(v, x):
        seen = [False] * N
        todo = []
        seen[v] = True
        todo.append(v)
        while todo:
            u = todo[-1]
            for next_v in G[u]:
                if seen[next_v] or next_v == G_parent[u]:
                    continue
                seen[next_v] = True
                todo.append(next_v)
                break
            else:
                w = todo.pop()
                cost[w] += x

    bfs(0)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        dfs(p-1, x)
    print(*cost)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x

    def bfs(s):
        G_color = ['WHITE' for _ in range(len(G))]
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    cost[v] += cost[u]
                    Q.append(v)
            G_color[u] = 'BLACK'

    bfs(0)
    print(*cost)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    G_color = ['WHITE' for _ in range(len(G))]
    G_distance = [float('inf') for _ in range(len(G))]
    G_parent = ['NIL' for _ in range(len(G))]

    def bfs(s):
        """
        breadth-first search
        -----
        G_color: BLACK if visited, GRAY if discovered, but will
             visit, WHITE if not discovered
        G_distance: a distance from s to v when algorithm computed
        G_parent: a predecessor of v. NIL if it not exist
        """
        G_color[s] = 'GRAY'
        G_distance[s] = 0
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    G_distance[v] = G_distance[u] + 1
                    G_parent[v] = u
                    Q.append(v)
            G_color[u] = 'BLACK'

    def dfs(v, x):
        seen = [False] * N
        todo = []
        seen[v] = True
        todo.append(v)
        while todo:
            u = todo[-1]
            for next_v in G[u]:
                if seen[next_v] or next_v == G_parent[u]:
                    continue
                seen[next_v] = True
                todo.append(next_v)
                break
            else:
                w = todo.pop()
                cost[w] += x

    bfs(0)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        dfs(p-1, x)
    print(*cost)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x

    def bfs(s):
        G_color = ['WHITE' for _ in range(len(G))]
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    cost[v] += cost[u]
                    Q.append(v)
            G_color[u] = 'BLACK'

    bfs(0)
    print(*cost)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n, q = (int(i) for i in input().split())
ab = [[int(i) for i in input().split()] for _ in range(n-1)]
child_dict = {}
for i in range(n-2,-1,-1):
    cur = child_dict.get(ab[i][0],set({}))
    cur.add(ab[i][1])
    child_dict[ab[i][0]] = cur

for i, j in child_dict.items():
    for k in range(1,n+1):
        if k in child_dict.keys() and i in child_dict[k]:
            child_dict[k] = child_dict[k].union(j)


px = [[int(i) for i in input().split()] for _ in range(q)]

p = [0] * n

for j in range(q):
    rootidx = px[j][0]
    p[rootidx - 1] += px[j][1]
    if rootidx in child_dict.keys():
        for k in child_dict[rootidx]:
            p[k-1] += px[j][1]

print(*p)

Fixed submission:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x

    def bfs(s):
        G_color = ['WHITE' for _ in range(len(G))]
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    cost[v] += cost[u]
                    Q.append(v)
            G_color[u] = 'BLACK'

    bfs(0)
    print(*cost)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n, q = (int(i) for i in input().split())
ab = [[int(i) for i in input().split()] for _ in range(n-1)]
child_dict = {}
for i in range(n-2,-1,-1):
    cur = child_dict.get(ab[i][0],set({}))
    cur.add(ab[i][1])
    child_dict[ab[i][0]] = cur

for i, j in child_dict.items():
    for k in range(1,n+1):
        if k in child_dict.keys() and i in child_dict[k]:
            child_dict[k] = child_dict[k].union(j)


px = [[int(i) for i in input().split()] for _ in range(q)]

p = [0] * n

for j in range(q):
    rootidx = px[j][0]
    p[rootidx - 1] += px[j][1]
    if rootidx in child_dict.keys():
        for k in child_dict[rootidx]:
            p[k-1] += px[j][1]

print(*p)

Fixed submission:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x

    def bfs(s):
        G_color = ['WHITE' for _ in range(len(G))]
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    cost[v] += cost[u]
                    Q.append(v)
            G_color[u] = 'BLACK'

    bfs(0)
    print(*cost)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n, q = (int(i) for i in input().split())
ab = [[int(i) for i in input().split()] for _ in range(n-1)]
child_dict = {}
for i in range(n-2,-1,-1):
    cur = child_dict.get(ab[i][0],set({}))
    cur.add(ab[i][1])
    child_dict[ab[i][0]] = cur
px = [[int(i) for i in input().split()] for _ in range(q)]

p = [0] * n

for j in range(q):
    rootidx = px[j][0]
    p[rootidx - 1] += px[j][1]
    if rootidx in child_dict.keys():
        for k in child_dict[rootidx]:
            p[k-1] += px[j][1]
            while k in child_dict.keys():
                for l in child_dict[k]:
                    p[l-1] += px[j][1]
                k = l
print(*p)

Fixed submission:
def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x

    def bfs(s):
        G_color = ['WHITE' for _ in range(len(G))]
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    cost[v] += cost[u]
                    Q.append(v)
            G_color[u] = 'BLACK'

    bfs(0)
    print(*cost)


if __name__ == '__main__':
    main()