Problem p02936 (python)

Fixed submission:
import sys
stdin = sys.stdin
 
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
s = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n, q = na()
a, b = [0]*n, [0]*n
G = [[] for i in range(n)]
for i in range(n-1):
        a, b = na()
        G[a-1].append(b-1)
        G[b-1].append(a-1)

counter = [0] * n
for i in range(q):
        p, x = na()
        counter[p-1] += x

visit = [False] * n
visit[0] = True
stack = [0]

while stack:
        parent = stack.pop()
        for child in G[parent]:
                if visit[child]:
                        None
                else:
                        visit[child] = True
                        counter[child] += counter[parent]
                        stack.append(child)

print(*counter)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
stdin = sys.stdin
 
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
s = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n, q = na()
a, b = [0]*n, [0]*n
G = [[] for i in range(n)]
for i in range(n-1):
        a, b = na()
        G[a-1].append(b-1)

seen = [0] * n
counter = [0] * n
for i in range(q):
        p, x = na()
        seen = np.zeros(n)
        counter[p-1] += x

visit = [False] * n
visit[0] = True
stack = [0]

while stack:
        parent = stack.pop()
        for child in G[parent]:
                if visit[child]:
                        None
                else:
                        visit[child] = True
                        counter[child] += counter[parent]
                        stack.append(child)

print(*counter)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
stdin = sys.stdin
 
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
s = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n, q = na()
a, b = [0]*n, [0]*n
G = [[] for i in range(n)]
for i in range(n-1):
        a, b = na()
        G[a-1].append(b-1)

seen = [0] * n

def dfs(v):
        seen[v] = True
        for next_v in G[v]:
                if seen[next_v]:
                        continue
                dfs(next_v)
ans = np.zeros(n)
for i in range(q):
        p, x = na()
        seen = np.zeros(n)
        dfs(p-1)
        seen[p-1] = True
        ans += x * seen
ans = ans.astype(np.int32)

print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
stdin = sys.stdin
from functools import lru_cache
 
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
s = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n, q = na()
a, b = [0]*n, [0]*n
G = [[] for i in range(n)]
for i in range(n-1):
        a, b = na()
        G[a-1].append(b-1)

seen = [0] * n

def dfs(v):
        seen[v] = True
        for next_v in G[v]:
                if seen[next_v]:
                        continue
                dfs(next_v)
ans = np.zeros(n)
for i in range(q):
        p, x = na()
        seen = np.zeros(n)
        dfs(p-1)
        seen[p-1] = True
        ans += x * seen
ans = ans.astype(np.int32)

print(*ans)