Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = {}
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    # parents[b] = a
    # childrens[a].add(b)
    relations.setdefault(a, [])
    relations.setdefault(b, [])
    relations[a].append(b)
    relations[b].append(a)

  def f(parent, relations, parents, childrens):
    shown.add(parent)
    for c in relations[parent]:
      if c not in shown:
        parents[c] = parent
        childrens[parent].add(c)
        f(c, relations, parents, childrens)

  f(0, relations, parents, childrens)
  points = {}
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points.setdefault(p, 0)
    points[p] += x

  def solve(idx, parents, childrens, points, result):
    # print(idx)
    if idx in points:
      result[idx] += points[idx]
    if parents[idx] is not None:
      result[idx] += result[parents[idx]]
    for child in childrens[idx]:
      solve(child, parents, childrens, points, result)
      
  solve(0, parents, childrens, points, result)
  print(*result)

if __name__ == "__main__":
  main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 9)

def main():
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = {}
  shown = set()

  for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    # parents[b] = a
    # childrens[a].add(b)
    relations.setdefault(a, [])
    relations.setdefault(b, [])
    relations[a].append(b)
    relations[b].append(a)

  def f(parent, relations, parents, childrens):
    shown.add(parent)
    for c in relations[parent]:
      if c not in shown:
        parents[c] = parent
        childrens[parent].add(c)
        f(c, relations, parents, childrens)

  f(0, relations, parents, childrens)
  points = {}
  result = [0] * N

  for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    points.setdefault(p, 0)
    points[p] += x

  def solve(idx, parents, childrens, points, result):
    # print(idx)
    if idx in points:
      result[idx] += points[idx]
    if parents[idx] is not None:
      result[idx] += result[parents[idx]]
    for child in childrens[idx]:
      solve(child, parents, childrens, points, result)
      
  solve(0, parents, childrens, points, result)
  print(*result)

if __name__ == "__main__":
  main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 9)

def main():
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = {}
  shown = set()

  for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    # parents[b] = a
    # childrens[a].add(b)
    relations.setdefault(a, [])
    relations.setdefault(b, [])
    relations[a].append(b)
    relations[b].append(a)

  def f(parent, relations, parents, childrens):
    shown.add(parent)
    for c in relations[parent]:
      if c not in shown:
        parents[c] = parent
        childrens[parent].add(c)
        f(c, relations, parents, childrens)

  f(0, relations, parents, childrens)
  points = {}
  result = [0] * N

  for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    points.setdefault(p, 0)
    points[p] += x

  def solve(idx, parents, childrens, points, result):
    # print(idx)
    if idx in points:
      result[idx] += points[idx]
    if parents[idx] is not None:
      result[idx] += result[parents[idx]]
    for child in childrens[idx]:
      solve(child, parents, childrens, points, result)
      
  solve(0, parents, childrens, points, result)
  print(*result)

if __name__ == "__main__":
  main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import resource

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 9)
resource.setrlimit(resource.RLIMIT_STACK, (-1, -1))

def main():
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = {}
  shown = set()

  for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    # parents[b] = a
    # childrens[a].add(b)
    relations.setdefault(a, [])
    relations.setdefault(b, [])
    relations[a].append(b)
    relations[b].append(a)

  def f(parent, relations, parents, childrens):
    shown.add(parent)
    for c in relations[parent]:
      if c not in shown:
        parents[c] = parent
        childrens[parent].add(c)
        f(c, relations, parents, childrens)

  f(0, relations, parents, childrens)
  points = {}
  result = [0] * N

  for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    points.setdefault(p, 0)
    points[p] += x

  def solve(idx, parents, childrens, points, result):
    # print(idx)
    if idx in points:
      result[idx] += points[idx]
    if parents[idx] is not None:
      result[idx] += result[parents[idx]]
    for child in childrens[idx]:
      solve(child, parents, childrens, points, result)
      
  solve(0, parents, childrens, points, result)
  print(*result)

if __name__ == "__main__":
  main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 9)

# Nがノードの数, Qが操作回数
N, Q = map(int, input().split())

parents = [None] * N
childrens = [set() for _ in range(N)]
relations = [set() for _ in range(N)]
shown = set()

for _ in range(N - 1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  # parents[b] = a
  # childrens[a].add(b)
  relations[a].add(b)
  relations[b].add(a)

def f(parent):
  global relations, parents, childrens
  shown.add(parent)
  for c in relations[parent]:
    if c not in shown:
      parents[c] = parent
      childrens[parent].add(c)
      f(c)

f(0)
points = {}
result = [0] * N

for _ in range(Q):
  p, x = map(int, input().split())
  p -= 1
  if p not in points:
    points[p] = 0
  points[p] += x

def solve(idx):
  global parents, childrens, points, result
  # print(idx)
  if idx in points:
    result[idx] += points[idx]
  if parents[idx] is not None:
    result[idx] += result[parents[idx]]
  for child in childrens[idx]:
    solve(child)
    

solve(0)
print(*result)


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 9)

# Nがノードの数, Qが操作回数
N, Q = map(int, input().split())

parents = [None] * N
childrens = [set() for _ in range(N)]
relations = [set() for _ in range(N)]
shown = set()

for _ in range(N - 1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  # parents[b] = a
  # childrens[a].add(b)
  relations[a].add(b)
  relations[b].add(a)

def f(parent):
  global relations, parents, childrens
  shown.add(parent)
  for c in relations[parent]:
    if c not in shown:
      parents[c] = parent
      childrens[parent].add(c)
      f(c)

f(0)
points = {}
result = [0] * N

for _ in range(Q):
  p, x = map(int, input().split())
  p -= 1
  if p not in points:
    points[p] = 0
  points[p] += x

def solve(idx):
  global parents, childrens, points, result
  # print(idx)
  if idx in points:
    result[idx] += points[idx]
  if parents[idx] is not None:
    result[idx] += result[parents[idx]]
  for child in childrens[idx]:
    solve(child)
    

solve(0)
print(*result)


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 9)

# Nがノードの数, Qが操作回数
N, Q = map(int, input().split())

points = [0] * N
nodes = [set() for _ in range(N)]
parents = [-1] * N

for _ in range(N - 1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  nodes[a].add(b)
  nodes[b].add(a)

def fill_parent(parent, n):
  global parents, nodes
  parents[n] = parent
  if parent in nodes[n] and len(nodes[n]) == 1:
    return
  for i in nodes[n]:
    if i != parent:
      fill_parent(n, i)

for i in nodes[0]:
  fill_parent(0, i)

h = {}
for i in range(N):
  if parents[i] not in h:
    h[parents[i]] = set()
  h[parents[i]].add(i)

# print(h)
for _ in range(Q):
  p, x = map(int, input().split())
  p -= 1
  points[p] += x

c = [0] * N

def calc(n):
  if n == 0:
    c[n] = points[n]
  else:
    c[n] = points[n] + c[parents[n]]
  if n in h:
    for i in h[n]:
      calc(i)

calc(0)
print(*c)


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Runtime Error):
# Nがノードの数, Qが操作回数
N, Q = map(int, input().split())

points = [0] * N
nodes = [set() for _ in range(N)]
parents = [-1] * N

for _ in range(N - 1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  nodes[a].add(b)
  nodes[b].add(a)

def fill_parent(parent, n):
  global parents, nodes
  parents[n] = parent
  if parent in nodes[n] and len(nodes[n]) == 1:
    return
  for i in nodes[n]:
    if i != parent:
      fill_parent(n, i)

for i in nodes[0]:
  fill_parent(0, i)

h = {}
for i in range(N):
  if parents[i] not in h:
    h[parents[i]] = set()
  h[parents[i]].add(i)

# print(h)
for _ in range(Q):
  p, x = map(int, input().split())
  p -= 1
  points[p] += x

c = [0] * N

def calc(n):
  if n == 0:
    c[n] = points[n]
  else:
    c[n] = points[n] + c[parents[n]]
  if n in h:
    for i in h[n]:
      calc(i)

calc(0)
print(*c)


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Runtime Error):
def main():
  N, Q = map(lambda i: int(i), input().split(' '))
  vertices = [[] for i in range(N)]
  points = [0] * N
  for n in range(N - 1):
    a, b = map(lambda i: int(i), input().split(' '))
    vertices[a - 1].append(b - 1)
  # print(vertices)
  for q in range(Q):
    p, x = map(lambda i: int(i), input().split(' '))
    add(points, vertices, p - 1, x)
  print(' '.join(list(map(lambda p: str(p), points))))

def add(points, vertices, i, x):
  points[i] += x
  if len(vertices[i]) > 0:
    for v in vertices[i]:
        add(points, vertices, v, x)

main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)

def main():
  input = sys.stdin.buffer.readline
  read = sys.stdin.buffer.read
  # Nがノードの数, Qが操作回数
  N, Q = map(int, input().split())
  ABPX = list(map(int, read().split()))
  AB = iter(ABPX[:N+N-2])
  PX = iter(ABPX[N+N-2:])

  parents = [None] * N
  childrens = [set() for _ in range(N)]
  relations = [[] for _ in range(N)]
  shown = set()

  for a, b in zip(AB, AB):
    a -= 1
    b -= 1
    relations[a].append(b)
    relations[b].append(a)

  points = [0] * N
  result = [0] * N

  for p, x in zip(PX, PX):
    p -= 1
    points[p] += x

  def solve(parent, relations, points, result):
    result[parent] += points[parent]
    shown.add(parent)
    for child in relations[parent]:
      if child not in shown:
        result[child] += result[parent]
        solve(child, relations, points, result)
      
  solve(0, relations, points, result)
  print(*result)

if __name__ == "__main__":
  main()