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

trees = {}
for _ in range(N - 1):
    id1, id2 = map(int, input().split())
    if id1 not in trees:
        trees[id1] = []
    trees[id1].append(id2)

values = {}
for _ in range(Q):
    nid, val = map(int, input().split())
    if nid not in values:
        values[nid] = 0
    values[nid] += val

counters = {}
stack = [[1, 0]] # id, value from parent
current_val = 0
while len(stack) > 0:
    myid, parent_val = stack.pop()
#     if myid not in counters:
#         counters[myid] = 0
    
    value_add = 0
    if myid in values:
        value_add = values[myid]
    
    counters[myid] = parent_val + value_add
    if myid in trees:
        for cid in trees[myid]:
            stack.append([cid, parent_val + value_add])

ret = []
for myid in range(1, N + 1):
    if myid not in counters:
        ret.append(str(0))
    else:
        ret.append(str(counters[myid]))
        
print(' '.join(ret))

Fixed submission:
N, Q = map(int, input().split())

trees = {}
for _ in range(N - 1):
    id1, id2 = map(int, input().split())
    if id1 not in trees:
        trees[id1] = []
    trees[id1].append(id2)

from collections import defaultdict
values = defaultdict(int)
for _ in range(Q):
    nid, val = map(int, input().split())
    values[nid] += val

counters = defaultdict(int)
stack = [[1, 0]] # id, value from parent
while len(stack) > 0:
    myid, parent_val = stack.pop()
    current_val = parent_val + values[myid]
    counters[myid] = current_val
    if myid in trees:
        for cid in trees[myid]:
            stack.append([cid, current_val])

print(' '.join(map(str, [counters[myid] for myid in range(1, N + 1)])))

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

trees = {}
for _ in range(N - 1):
    id1, id2 = map(int, input().split())
    if id1 not in trees:
        trees[id1] = []
    trees[id1].append(id2)

values = {}
for _ in range(Q):
    nid, val = map(int, input().split())
    if nid not in values:
        values[nid] = 0
    values[nid] += val

counters = {}
for nid, val in values.items():
    stack = [nid]
    while len(stack) > 0:
        myid = stack.pop()
        if myid not in counters:
            counters[myid] = 0
        counters[myid] += val
        if myid in trees:
            stack.extend(trees[myid])

ret = []
for myid in range(1, N + 1):
    if myid not in counters:
        ret.append(str(0))
    else:
        ret.append(str(counters[myid]))
        
print(' '.join(ret))

Fixed submission:
N, Q = map(int, input().split())

trees = {}
for _ in range(N - 1):
    id1, id2 = map(int, input().split())
    if id1 not in trees:
        trees[id1] = []
    trees[id1].append(id2)

from collections import defaultdict
values = defaultdict(int)
for _ in range(Q):
    nid, val = map(int, input().split())
    values[nid] += val

counters = defaultdict(int)
stack = [[1, 0]] # id, value from parent
while len(stack) > 0:
    myid, parent_val = stack.pop()
    current_val = parent_val + values[myid]
    counters[myid] = current_val
    if myid in trees:
        for cid in trees[myid]:
            stack.append([cid, current_val])

print(' '.join(map(str, [counters[myid] for myid in range(1, N + 1)])))

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

trees = {}
for _ in range(N - 1):
    id1, id2 = map(int, input().split())
    if id1 not in trees:
        trees[id1] = []
    trees[id1].append(id2)

counters = {}
for _ in range(Q):
    nid, val = map(int, input().split())
    stack = [nid]
    while len(stack) > 0:
        myid = stack.pop()
        if myid not in counters:
            counters[myid] = 0
        counters[myid] += val
        if myid in trees:
            stack.extend(trees[myid])

ret = []
for myid in range(1, N + 1):
    if myid not in counters:
        ret.append(str(0))
    else:
        ret.append(str(counters[myid]))
        
print(' '.join(ret))

Fixed submission:
N, Q = map(int, input().split())

trees = {}
for _ in range(N - 1):
    id1, id2 = map(int, input().split())
    if id1 not in trees:
        trees[id1] = []
    trees[id1].append(id2)

from collections import defaultdict
values = defaultdict(int)
for _ in range(Q):
    nid, val = map(int, input().split())
    values[nid] += val

counters = defaultdict(int)
stack = [[1, 0]] # id, value from parent
while len(stack) > 0:
    myid, parent_val = stack.pop()
    current_val = parent_val + values[myid]
    counters[myid] = current_val
    if myid in trees:
        for cid in trees[myid]:
            stack.append([cid, current_val])

print(' '.join(map(str, [counters[myid] for myid in range(1, N + 1)])))