Problem p02936 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())
 
stack = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  stack[a].append(b)
  stack[b].append(a)
ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x
def dfs(cur, par):
    for c in stack[cur]:
      if c == par:
        continue
      ans[c] += ans[cur] 
      dfs(c, cur)
dfs(0, -1)
print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**5)
n, q = map(int, input().split())

stack = [[] for _ in range(n+1)]
stack[0].append(1)
for i in range(n-1):
  a,b = map(int, input().split())
  stack[a].append(b)

ans = [0] * n
pool = {}
def dfs(cur, num):
	ans[cur-1] += num
	if len(stack[cur]) <= 0:
		return 
	for c in stack[cur]:
		dfs(c, num)

for i in range(q):
  p, x = map(int, input().split())
  pool[p] = pool.get(p, 0) + x
for k, v in pool.items():
  dfs(k, v)
print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())

stack = [[] for _ in range(n+1)]
stack[0].append(1)
for i in range(n-1):
  a,b = map(int, input().split())
  stack[a].append(b)

ans = [0] * n
pool = {}
def dfs(cur, num):
	ans[cur-1] += num
	if len(stack[cur]) <= 0:
		return
	for c in stack[cur]:
		dfs(c, num)

for i in range(q):
  p, x = map(int, input().split())
  pool[p] = pool.get(p, 0) + x
for k, v in pool.items():
  dfs(k, v)
print(*ans)

Buggy submission (verdict: Runtime Error):
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)]

stack = [[] for _ in range(n+1)]
stack[0].append(1)
for i in range(n-1):
	stack[AB[i][0]].append(AB[i][1])

ans = [0] * n
pool = {}
def dfs(cur, num):
	ans[cur-1] += num
	if len(stack[cur]) <= 0:
		return
	for c in stack[cur]:
		dfs(c, num)

for px in PX:
	pool[px[0]] = pool.get(px[0], 0) + px[1]
for k, v in pool.items():
  	dfs(k, v)
print(*ans)

Buggy submission (verdict: Runtime Error):
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)]

stack = [[] for _ in range(n+1)]
stack[0].append(1)
for i in range(n-1):
	stack[AB[i][0]].append(AB[i][1])

ans = [0] * n
def dfs(cur, num):
	ans[cur-1] += num
	if len(stack[cur]) <= 0:
		return
	for c in stack[cur]:
		dfs(c, num)

for px in PX:
	dfs(px[0], px[1])

print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
AB = []
ans = [0]*n
for i in range(n-1):
  a,b = map(int, input().split())
  AB.append([a,b])
tmp = [[] for _ in range(n)]
tmp[0].append(1)
for i in range(n-1):
  tmp[AB[i][0]].append(AB[i][1])
def add_num(num, cur):
  ans[cur-1] += num
  if len(tmp[cur-1]) <= 0:
    return
  for i in tmp[cur]:
    add_num(num, i)
PX = [list(map(int, input().split())) for _ in range(q)]
for px in PX:
  add_num(px[1], px[0])
print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
AB = []
ans = [0]*n
for i in range(n-1):
  a,b = map(int, input().split())
  AB.append([a,b])
tmp = [[] for _ in range(n)]
tmp[0].append(1)
for i in range(n-1):
  tmp[AB[i][0]].append(AB[i][1])
def add_num(num, cur):
  ans[cur-1] += num
  if len(tmp[cur-1]) <= 0:
    return
  for i in tmp[cur]:
    add_num(num, i)
PX = [list(map(int, input().split())) for _ in range(q)]
for px in PX:
  add_num(px[1], px[0])
print(*ans)