Buggy python submission (verdict: Time Limit Exceeded):
N, Q = list(map(int, input().split()))
d = {}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    if a in d:
        d[a].add(b)
    else:
        d[a] = {b}
    if b in d:
        d[b].add(a)
    else:
        d[b] = {a}
counter = [0] * (N + 1)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

nodes = [1]
visit = set()
while nodes:
    node = nodes.pop()
    visit.add(node)
    v = counter[node]
    for child in d[node]:
        if child not in visit:
            nodes.append(child)
            counter[child] += v
print(*counter[1:])

Fixed submission:
N, Q = list(map(int, input().split()))
d = {i: set() for i in range(1, N + 1)}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    d[a].add(b)
    d[b].add(a)
counter = [0] * (N + 1)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

parents = {1}
while parents:
    children = set()
    for parent in parents:
        v = counter[parent]
        for child in d[parent]:
            d[child].remove(parent)
            children.add(child)
            counter[child] += v
    parents = children
print(*counter[1:])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = list(map(int, input().split()))
d = {}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    if a in d:
        d[a].add(b)
    else:
        d[a] = {b}
    if b in d:
        d[b].add(a)
    else:
        d[b] = {a}
counter = [0] * (N + 1)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

parents = {1}
visit = set()
while True:
    children = set()
    for parent in parents:
        if parent in visit:
            continue
        visit.add(parent)
        v = counter[parent]
        children |= d[parent]
        for child in d[parent]:
            if child in visit:
                continue
            counter[child] += v
    if len(children) == 0:
        break
    parents = children
print(*counter[1:])

Fixed submission:
N, Q = list(map(int, input().split()))
d = {i: set() for i in range(1, N + 1)}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    d[a].add(b)
    d[b].add(a)
counter = [0] * (N + 1)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

parents = {1}
while parents:
    children = set()
    for parent in parents:
        v = counter[parent]
        for child in d[parent]:
            d[child].remove(parent)
            children.add(child)
            counter[child] += v
    parents = children
print(*counter[1:])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = list(map(int, input().split()))
d = {}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    if a in d:
        d[a].add(b)
    else:
        d[a] = {b}
    if b in d:
        d[b].add(a)
    else:
        d[b] = {a}
counter = [0] * (N + 1)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

parents = {1}
while True:
    children = set()
    for parent in parents:
        v = counter[parent]
        for child in d[parent]:
            d[child].remove(parent)
            children.add(child)
            counter[child] += v
    if len(children) == 0:
        break
    parents = children
print(*counter[1:])

Fixed submission:
N, Q = list(map(int, input().split()))
d = {i: set() for i in range(1, N + 1)}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    d[a].add(b)
    d[b].add(a)
counter = [0] * (N + 1)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

parents = {1}
while parents:
    children = set()
    for parent in parents:
        v = counter[parent]
        for child in d[parent]:
            d[child].remove(parent)
            children.add(child)
            counter[child] += v
    parents = children
print(*counter[1:])