Problem p02936 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
N,Q = map(int,input().split())
ab = [ tuple(map(int,input().split())) for i in range(N-1) ]
px = [ tuple(map(int,input().split())) for i in range(Q) ]
dic = {}
dic2=[0]*(N+1)
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for i in range(N-1):
    tmp1 = ab[i][0]
    tmp2 = ab[i][1]
    dic[tmp1].append(tmp2)
    dic[tmp2].append(tmp1)
ab=0
for i in range(Q):
    dic2[px[i][0]] += px[i][1]
px=0
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node-1] = nowval
    tg_list = dic[now_node][:]
    for elem in tg_list:
        dic[elem].remove(now_node)
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 
N,Q = map(int,readline().split())
abpx = list(map(int,read().split()))
ab = iter(abpx[:N+N-2])
px = iter(abpx[N+N-2:])
dic = {}
dic2=[0]*(N+1)
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for a,b in zip(ab,ab):
    dic[a].append(b)
    dic[b].append(a)
for p,x in zip(px,px):
    dic2[p] += x
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node-1] = nowval
    tg_list = dic[now_node][:]
    for elem in tg_list:
        dic[elem].remove(now_node)
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200001)
N,Q = map(int,input().split())
ab = [ tuple(map(int,input().split())) for i in range(N-1) ]
px = [ tuple(map(int,input().split())) for i in range(Q) ]
dic = {}
dic2=[0]*(N+1)
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for i in range(N-1):
    dic[ab[i][0]].append(ab[i][1])
ab=0
for i in range(Q):
    dic2[px[i][0]] += px[i][1]
px=0
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node-1] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200001)
N,Q = map(int,input().split())
ab = [ list(map(int,input().split())) for i in range(N-1) ]
px = [ list(map(int,input().split())) for i in range(Q) ]
dic = {}
dic2=[0]*N
dic3=[0]*N

for i in range(N-1):
    dic[i+1] = []
    dic[ab[i][0]].append(ab[i][1])
dic[N] = []
for i in range(Q):
    tmp1 = px[i][0]
    tmp2 = px[i][1]
    dic2[tmp1-1] += tmp2
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node-1]
    dic3[now_node-1] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(200001)
N,Q = map(int,input().split())
ab = [ list(map(int,input().split())) for i in range(N-1) ]
px = [ list(map(int,input().split())) for i in range(Q) ]
dic = {}
dic2={}
dic3={}

for i in range(N):
    dic[i+1] = []
    dic2[i+1] = 0
for i in range(N-1):
    tmp1 = ab[i][0]
    tmp2 = ab[i][1]
    dic[tmp1].append(tmp2)
for i in range(Q):
    tmp1 = px[i][0]
    tmp2 = px[i][1]
    dic2[tmp1] += tmp2
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i]) for i in range(1,N+1)]
print(' '.join(res))

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
ab = [ list(map(int,input().split())) for i in range(N-1) ]
px = [ list(map(int,input().split())) for i in range(Q) ]
a = [ab[i][0] for i in range(N-1)]
b = [ab[i][1] for i in range(N-1)]
dic = {}
dic2={}
dic3={}

for i in range(N):
    dic[i+1] = []
    dic2[i+1] = 0
for i in range(N-1):
    tmp1 = a[i]
    tmp2 = b[i]
    dic[tmp1].append(tmp2)
for i in range(Q):
    tmp1 = px[i][0]
    tmp2 = px[i][1]
    dic2[tmp1] += tmp2
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i]) for i in range(1,N+1)]
print(' '.join(res))