Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, Q = map(int, input().split())

    graph = dict()
    for i in range(N-1):
        a, b = map(int, input().split())
        if a not in graph: graph[a] = [b]
        else: graph[a].append(b)
        # if b not in graph: graph[b] = [a]
        # else: graph[b].append(a)

    ops = dict()
    for i in range(Q):
        p, x = map(int, input().split())
        if p not in ops: ops[p] = x
        else: ops[p] += x

    # print(graph)
    # print(ops)
    vals = [0 for _ in range(N)]
    v = 0
    if 1 in ops: v = ops[1]
    cur = [[1, v]]

    while cur:
        temp = []
        for a, v in cur:
            vals[a-1] += v
            if a in graph:
                for b in graph[a]:
                    if b in ops:
                        temp.append([b, v+ops[b]])
                    else:
                        temp.append([b, v])
        cur = temp

    print(' '.join(map(str, vals)))

if __name__ == '__main__':
    main()

Fixed submission:
import collections

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

    graph = collections.defaultdict(list)
    for i in range(N-1):
        a, b = map(int, input().split())
        graph[a].append(b)

    ops = collections.defaultdict(int)
    for i in range(Q):
        p, x = map(int, input().split())
        ops[p] += x

    vals = [0 for _ in range(N)]
    v = 0
    if 1 in ops: v = ops[1]
    cur = [[1, v]]

    while cur:
        temp = []
        for a, v in cur:
            vals[a-1] += v
            for b in graph[a]:
                temp.append([b, v+ops[b]])
        cur = temp

    print(' '.join(map(str, vals)))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, Q = map(int, input().split())

    graph = dict()
    for i in range(N-1):
        a, b = map(int, input().split())
        if a not in graph: graph[a] = [b]
        else: graph[a].append(b)
        # if b not in graph: graph[b] = [a]
        # else: graph[b].append(a)

    ops = dict()
    for i in range(Q):
        p, x = map(int, input().split())
        if p not in ops: ops[p] = x
        else: ops[p] += x

    # print(graph)
    # print(ops)
    vals = [0 for _ in range(N)]
    v = 0
    if 1 in ops: v = ops[1]
    cur = [[1, v]]

    visited = set([1])
    while cur:
        temp = []
        for a, v in cur:
            vals[a-1] += v
            if a in graph:
                for b in graph[a]:
                    if b not in visited:
                        if b in ops:
                            temp.append([b, v+ops[b]])
                        else:
                            temp.append([b, v])
                        visited.add(b)
        cur = temp

    print(' '.join(map(str, vals)))

if __name__ == '__main__':
    main()

Fixed submission:
import collections

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

    graph = collections.defaultdict(list)
    for i in range(N-1):
        a, b = map(int, input().split())
        graph[a].append(b)

    ops = collections.defaultdict(int)
    for i in range(Q):
        p, x = map(int, input().split())
        ops[p] += x

    vals = [0 for _ in range(N)]
    v = 0
    if 1 in ops: v = ops[1]
    cur = [[1, v]]

    while cur:
        temp = []
        for a, v in cur:
            vals[a-1] += v
            for b in graph[a]:
                temp.append([b, v+ops[b]])
        cur = temp

    print(' '.join(map(str, vals)))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, Q = map(int, input().split())

    graph = dict()
    for i in range(N-1):
        a, b = map(int, input().split())
        if a not in graph: graph[a] = [b]
        else: graph[a].append(b)
        # if b not in graph: graph[b] = [a]
        # else: graph[b].append(a)

    ops = dict()
    for i in range(Q):
        p, x = map(int, input().split())
        if p not in ops: ops[p] = x
        else: ops[p] += x

    # print(graph)
    # print(ops)
    vals = [0 for _ in range(N)]
    for p in ops:
        v = ops[p]
        cur = [p]
        visited = set()
        while cur:
            temp = []
            for a in cur:
                vals[a-1] += v
                if a in graph:
                    for b in graph[a]:
                        if b not in visited:
                            temp.append(b)
                            visited.add(b)
            cur = temp

    print(' '.join(map(str, vals)))


if __name__ == '__main__':
    main()

Fixed submission:
import collections

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

    graph = collections.defaultdict(list)
    for i in range(N-1):
        a, b = map(int, input().split())
        graph[a].append(b)

    ops = collections.defaultdict(int)
    for i in range(Q):
        p, x = map(int, input().split())
        ops[p] += x

    vals = [0 for _ in range(N)]
    v = 0
    if 1 in ops: v = ops[1]
    cur = [[1, v]]

    while cur:
        temp = []
        for a, v in cur:
            vals[a-1] += v
            for b in graph[a]:
                temp.append([b, v+ops[b]])
        cur = temp

    print(' '.join(map(str, vals)))

if __name__ == '__main__':
    main()