Problem p02936 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)

n, q = map(int, input().split())

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

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

ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)

dfs(0, -1, 0)

print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit = 2 * 10 ** 6
 
n, q = map(int, input().split())
 
connect = [set() for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)
 
counter = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    counter[p-1] += x
 
ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)
 
dfs(0, -1, 0)
 
print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit = 2 * 10 ** 6

n, q = map(int, input().split())

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

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

ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)

dfs(0, -1, 0)

print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit = 2 * 10 ** 5

n, q = map(int, input().split())

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

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

ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)

dfs(0, -1, 0)

print(*ans)

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

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

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

ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)

dfs(0, -1, 0)

print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
connect = [set() for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)

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

ans = [0] * n

def dfs(count, v, parent):
    count += counter[v]
    ans[v] = count
    for child in connect[v]:
        if child != parent:
            dfs(count, child, v)

dfs(0, 0, -1)

print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())
edge = [set() for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    edge[a-1].add(b-1)
    edge[b-1].add(a-1)

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

ans = [0] * n
def dfs(v,p,value): #p: parent of v
    value += cost[v]
    ans[v] = value
    for c in g[v]:
        if c == p: continue
        dfs(c,v,value)
# def dfs(cur, c, root):
    # # print(cur, c, root)
    # ans[cur] = c
    # for nex in edge[cur]:
        # if nex != root:
            # dfs(nex, c + cost[nex], cur)

dfs(0, -1, 0)
print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(30000)
n, q = map(int, input().split())
edge = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

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

ans = [0] * n
def dfs(cur, c, parent):
    c += cost[cur]
    ans[cur] = c
    for next in edge[cur]:
        if next != parent:
            dfs(next, c, cur)

dfs(0, 0, -1)
print(*ans)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
edge = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

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

ans = [0] * n
def dfs(cur, c, parent):
    c += cost[cur]
    ans[cur] = c
    for next in edge[cur]:
        if next != parent:
            dfs(next, c, cur)

dfs(0, 0, -1)
print(*ans)