Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

d = deque()
def f(tree, counter, arr, n):
  global d
  if not arr:
    return
  for pp in arr if type(arr) == list else arr.popleft():
    if isinstance(tree[pp], list):
      if tree[pp]:
        d.append(tree[pp])
    counter[pp] += n
  if d:
    return f(tree, counter, d, n)

def main():
  N, Q = na()

  tree = [[] for _ in range(N+1)]
  for _ in range(N-1):
    a, b = na()
    tree[a] += [b]

  counter = [0] * (N+1)
  for _ in range(Q):
    p, x = na()
    f(tree, counter, tree[p], x)
    counter[p] += x
  print(*counter[1:])

if __name__ == "__main__":
  import sys
  sys.setrecursionlimit(10000000)

  stdin = sys.stdin
  na = lambda: map(int, stdin.readline().split())
  main()


Fixed submission:
import sys

stdin = sys.stdin
na = lambda: map(int, stdin.readline().split())

def main():
  N, Q = na()

  ab = []
  for _ in range(N-1):
    a, b = na()
    ab.append([a, b])
  ab.sort()

  counter = [0] * (N+1)
  for _ in range(Q):
    p, x = na()
    counter[p] += x
  for i in range(N - 1):
    counter[ab[i][1]] += counter[ab[i][0]]
  print(*counter[1:])

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

d = deque()
def f(tree, counter, arr, n):
  global d
  if not arr:
    return
  for pp in arr if type(arr) == list else arr.popleft():
    if isinstance(tree[pp], list):
      if tree[pp]:
        d.append(tree[pp])
    counter[pp] += n
  if d:
    return f(tree, counter, d, n)

def main():
  N, Q = na()

  tree = [[] for _ in range(N+1)]
  for _ in range(N-1):
    a, b = na()
    tree[a] += [b]

  counter = [0] * (N+1)
  for _ in range(Q):
    p, x = na()
    f(tree, counter, tree[p], x)
    counter[p] += x
  print(*counter[1:])

if __name__ == "__main__":
  import sys
  sys.setrecursionlimit(10000000)

  stdin = sys.stdin
  na = lambda: map(int, stdin.readline().split())
  main()

Fixed submission:
import sys

stdin = sys.stdin
na = lambda: map(int, stdin.readline().split())

def main():
  N, Q = na()

  ab = []
  for _ in range(N-1):
    a, b = na()
    ab.append([a, b])
  ab.sort()

  counter = [0] * (N+1)
  for _ in range(Q):
    p, x = na()
    counter[p] += x
  for i in range(N - 1):
    counter[ab[i][1]] += counter[ab[i][0]]
  print(*counter[1:])

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):

import sys
sys.setrecursionlimit(10000000)

N, Q = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N-1)]
px = [list(map(int,input().split())) for _ in range(Q)]

tree = [[] for _ in range(N+1)]
for a, b in ab:
  tree[a] += [b]
  #tree[b] += [a]

def isList(item):
  if not item:
    return False
  return isinstance(item, list)

from collections import deque

d = deque()
def f(arr, n):
  global d
  if not arr:
    return
  for pp in arr if type(arr) == list else arr.popleft():
    if isList(tree[pp]):
      d.append(tree[pp])
      counter[pp] += n
    else:
      counter[pp] += n
  if d:
    return f(d, n)

counter = [0] * (N+1)
for p, x in px:
  f(tree[p], x)
  counter[p] += x
print(*counter[1:])

Fixed submission:
import sys

stdin = sys.stdin
na = lambda: map(int, stdin.readline().split())

def main():
  N, Q = na()

  ab = []
  for _ in range(N-1):
    a, b = na()
    ab.append([a, b])
  ab.sort()

  counter = [0] * (N+1)
  for _ in range(Q):
    p, x = na()
    counter[p] += x
  for i in range(N - 1):
    counter[ab[i][1]] += counter[ab[i][0]]
  print(*counter[1:])

if __name__ == "__main__":
  main()