Buggy python submission (verdict: Time Limit Exceeded):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

from collections import defaultdict

N, Q = map(int, input().split())
counts = [0] * N

# これだと N がでかすぎる時に TLE になるので、入ってきた値だけの配列を作る
#graph = [ [0 for _ in range(N)] for _ in range(N) ]
def get_graph(N):
    tmp = []
    graph = [ [] for _ in range(1) for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())

        # 1 - 3 - 2 みたいなケース
        if (b-1) in tmp:
            graph[b-1].append(a-1)
        else:
            graph[a-1].append(b-1)
            tmp.append(b-1)

    return graph


graph = get_graph(N)
#print(graph)

# 指示はまとめる
dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x


def inc(p, x, graph):
    # 0 index
    counts[p] += x

    for c in graph[p]:
        inc(c, x, graph)


for P, X in dd.items():
    P -= 1
    inc(P, X, graph)

print(*counts)


Fixed submission:

import sys
input = sys.stdin.readline #for input speed
sys.setrecursionlimit(10**6) #for deep recursion
n, q = map(int, input().split())

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) # a<bとは限らないため、双方向にする
  tree_index[b].append(a)

#print(tree_index)


ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x

def dfs(cur, par):
    for chl in tree_index[cur]:
      if chl == par: #parentとchildが等しい時はスキップ
        continue
      ans[chl] += ans[cur]
      dfs(chl, cur)
dfs(0, -1)
print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

from collections import defaultdict

N, Q = map(int, input().split())
graph = [ [0 for _ in range(N)] for _ in range(N) ]
counts = [0] * N

def get_graph(N):
    tmp = [False] * N
    for i in range(N-1):
        a, b = map(int, input().split())

        # 1 - 3 - 2 みたいなケース
        if tmp[b-1]:
            graph[b-1][a-1] = 1
        else:
            graph[a-1][b-1] = 1
            tmp[b-1] = True

    return graph

graph = get_graph(N)
#print(graph)

# 指示はまとめる
dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x


def inc(p, x):
    # 0 index
    counts[p] += x

    for i in range(N):
        if graph[p][i] == 0:
            continue
        inc(i, x)

for P, X in dd.items():
    P -= 1
    inc(P, X)

print(*counts)


Fixed submission:

import sys
input = sys.stdin.readline #for input speed
sys.setrecursionlimit(10**6) #for deep recursion
n, q = map(int, input().split())

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) # a<bとは限らないため、双方向にする
  tree_index[b].append(a)

#print(tree_index)


ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x

def dfs(cur, par):
    for chl in tree_index[cur]:
      if chl == par: #parentとchildが等しい時はスキップ
        continue
      ans[chl] += ans[cur]
      dfs(chl, cur)
dfs(0, -1)
print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):

import sys
sys.setrecursionlimit(1000000)

from collections import defaultdict

def inc(tree, point, p, x):
    # 自分
    point[p] += x
    if tree[p] == -1:
        return

    for child in tree[p]:
        inc(tree, point, child, x)


N, Q = map(int, input().split())

tree = [-1] * (N+1)
point = [0] * (N+1)

for i in range(N-1):
    a, b = map(int, input().split())
    if tree[a] == -1:
        tree[a] = [b]
    else:
        tree[a] += [b]

dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x

for p, x in dd.items():
    inc(tree, point, p, x)

point.pop(0)
print(*point)


Fixed submission:

import sys
input = sys.stdin.readline #for input speed
sys.setrecursionlimit(10**6) #for deep recursion
n, q = map(int, input().split())

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) # a<bとは限らないため、双方向にする
  tree_index[b].append(a)

#print(tree_index)


ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x

def dfs(cur, par):
    for chl in tree_index[cur]:
      if chl == par: #parentとchildが等しい時はスキップ
        continue
      ans[chl] += ans[cur]
      dfs(chl, cur)
dfs(0, -1)
print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):

import sys
sys.setrecursionlimit(1000000)

from collections import defaultdict

def inc(tree, point, p, x):
    # 自分
    point[p] += x
    if tree[p] == -1:
        return

    for child in tree[p]:
        inc(tree, point, child, x)


N, Q = map(int, input().split())

tree = [-1] * (N+1)
point = [0] * (N+1)

for i in range(N-1):
    a, b = map(int, input().split())
    if tree[a] == -1:
        tree[a] = [b]
    else:
        tree[a] += [b]

dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x

for p, x in dd.items():
    inc(tree, point, p, x)

point.pop(0)
print(*point)


Fixed submission:

import sys
input = sys.stdin.readline #for input speed
sys.setrecursionlimit(10**6) #for deep recursion
n, q = map(int, input().split())

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) # a<bとは限らないため、双方向にする
  tree_index[b].append(a)

#print(tree_index)


ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x

def dfs(cur, par):
    for chl in tree_index[cur]:
      if chl == par: #parentとchildが等しい時はスキップ
        continue
      ans[chl] += ans[cur]
      dfs(chl, cur)
dfs(0, -1)
print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):

import sys
sys.setrecursionlimit(1000000)

def inc(tree, point, p, x):
    # 自分
    point[p] += x
    if tree[p] == -1:
        return

    for child in tree[p]:
        inc(tree, point, child, x)


N, Q = map(int, input().split())

tree = [-1] * (N+1)
point = [0] * (N+1)

for i in range(N-1):
    a, b = map(int, input().split())
    if tree[a] == -1:
        tree[a] = [b]
    else:
        tree[a] += [b]

for i in range(Q):
    p, x = map(int, input().split())

    inc(tree, point, p, x)

point.pop(0)
print(*point)

Fixed submission:

import sys
input = sys.stdin.readline #for input speed
sys.setrecursionlimit(10**6) #for deep recursion
n, q = map(int, input().split())

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) # a<bとは限らないため、双方向にする
  tree_index[b].append(a)

#print(tree_index)


ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x

def dfs(cur, par):
    for chl in tree_index[cur]:
      if chl == par: #parentとchildが等しい時はスキップ
        continue
      ans[chl] += ans[cur]
      dfs(chl, cur)
dfs(0, -1)
print(*ans)

Buggy python submission (verdict: Runtime Error):

def inc(tree, point, p, x):
    # 自分
    point[p] += x
    if tree[p] == -1:
        return

    for child in tree[p]:
        inc(tree, point, child, x)


N, Q = map(int, input().split())

tree = [-1] * (N+1)
point = [0] * (N+1)

for i in range(N-1):
    a, b = map(int, input().split())
    if tree[a] == -1:
        tree[a] = [b]
    else:
        tree[a] += [b]

for i in range(Q):
    p, x = map(int, input().split())

    inc(tree, point, p, x)

point.pop(0)
print(*point)

Fixed submission:

import sys
input = sys.stdin.readline #for input speed
sys.setrecursionlimit(10**6) #for deep recursion
n, q = map(int, input().split())

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) # a<bとは限らないため、双方向にする
  tree_index[b].append(a)

#print(tree_index)


ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x

def dfs(cur, par):
    for chl in tree_index[cur]:
      if chl == par: #parentとchildが等しい時はスキップ
        continue
      ans[chl] += ans[cur]
      dfs(chl, cur)
dfs(0, -1)
print(*ans)