Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)

n, q = map(int, input().split())
C = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  C[a-1].append(b-1)
  C[b-1].append(a-1)
dist = [-1]*n
dist[0] = 1
A = [0]*n
F = [0]*n
for _ in range(q):
  p, x = map(int, input().split())
  F[p-1] += x
def dfs1(v):
  for nv in C[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      dfs1(nv)
dfs1(0)      
def dfs2(v):
  A[v] += F[v]
  for nv in C[v]:
    if dist[nv] > dist[v]:
      A[nv] += A[v]
      dfs2(nv)
dfs2(0)
print(*A)

Fixed submission:
n, q = map(int, input().split())
C = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  C[a-1].append(b-1)
  C[b-1].append(a-1)
dist = [-1]*n
dist[0] = 1
A = [0]*n
F = [0]*n
for _ in range(q):
  p, x = map(int, input().split())
  F[p-1] += x
S = [0]
T = [0]
while S:
  v = S.pop()
  for nv in C[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      S.append(nv)
while T:
  v = T.pop()
  A[v] += F[v]
  for nv in C[v]:
    if dist[nv] > dist[v]:
      A[nv] += A[v]
      T.append(nv)
print(*A)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)

n, q = map(int, input().split())
C = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  C[a-1].append(b-1)
  C[b-1].append(a-1)
dist = [-1]*n
dist[0] = 1
A = [0]*n
F = [0]*n
for _ in range(q):
  p, x = map(int, input().split())
  F[p-1] += x
def dfs1(v):
  for nv in C[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      dfs1(nv)
dfs1(0)      
def dfs2(v):
  A[v] += F[v]
  for nv in C[v]:
    if dist[nv] > dist[v]:
      A[nv] += A[v]
      dfs2(nv)
dfs2(0)
print(*A)

Fixed submission:
n, q = map(int, input().split())
C = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  C[a-1].append(b-1)
  C[b-1].append(a-1)
dist = [-1]*n
dist[0] = 1
A = [0]*n
F = [0]*n
for _ in range(q):
  p, x = map(int, input().split())
  F[p-1] += x
S = [0]
T = [0]
while S:
  v = S.pop()
  for nv in C[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      S.append(nv)
while T:
  v = T.pop()
  A[v] += F[v]
  for nv in C[v]:
    if dist[nv] > dist[v]:
      A[nv] += A[v]
      T.append(nv)
print(*A)

Buggy python submission (verdict: Runtime Error):
from collections import deque

n, q = map(int, input().split())
C = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  C[a-1].append(b-1)
  C[b-1].append(a-1)
dist = [-1]*n
dist[0] = 1
A = [0]*n
F = [0]*n
for _ in range(q):
  p, x = map(int, input().split())
  F[p-1] += x
dq = deque()
dq.append(0)
while dq:
  v = dq.popleft()
  for nv in C[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      dq.append(nv)
def dfs(v):
  A[v] += F[v]
  for nv in C[v]:
    if dist[nv] > dist[v]:
      A[nv] += A[v]
      dfs(nv)
dfs(0)
print(*A)

Fixed submission:
n, q = map(int, input().split())
C = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  C[a-1].append(b-1)
  C[b-1].append(a-1)
dist = [-1]*n
dist[0] = 1
A = [0]*n
F = [0]*n
for _ in range(q):
  p, x = map(int, input().split())
  F[p-1] += x
S = [0]
T = [0]
while S:
  v = S.pop()
  for nv in C[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      S.append(nv)
while T:
  v = T.pop()
  A[v] += F[v]
  for nv in C[v]:
    if dist[nv] > dist[v]:
      A[nv] += A[v]
      T.append(nv)
print(*A)