Problem p02936 (python)

Fixed submission:
import collections

N,Q=map(int,input().split())
M=[[] for i in range(N+1)]
P=[0 for i in range(N+1)]
for i in range(N-1):
  a,b=map(int,input().split())
  M[a].append(b)
  M[b].append(a)
#print("M:",M)

S=[0 for i in range(N+1)]
for i in range(Q):
  p,x=map(int,input().split())
  S[p]+=x
#print(S)
  
d=collections.deque([1])

while len(d):
  dd=d.popleft()
  if P[dd]==0:
    P[dd]=1
    for i in M[dd]:
      if P[i]==0:
        S[i]+=S[dd]
      d.append(i)
  #print(d,S)
print(*S[1:])

Buggy submission (verdict: Runtime Error):
N,Q=map(int,input().split())
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
p=[0 for i in range(Q)]
q=[0 for i in range(Q)]
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
for i in range(Q):
  p[i],q[i]=map(int,input().split())
#print(N,Q)
#print(a)
#print(b)
#print(p)
#print(q)

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
  K[p[i]-1]+=q[i]
#print(K)

for i in range(N-1):
  K[b[i]-1]+=K[a[i]-1]
#print(K)

print(" ".join(map(str,K)))

Buggy submission (verdict: Runtime Error):
N,Q=map(int,input().split())
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
p=[0 for i in range(N-1)]
q=[0 for i in range(N-1)]
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
for i in range(Q):
  p[i],q[i]=map(int,input().split())
#print(N,Q)
#print(a)
#print(b)
#print(p)
#print(q)

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
#  K[p[i]-1]+=q[i]
#print(K)

for i in range(N-1):
#  K[b[i]-1]+=K[a[i]-1]
#print(K)

print(" ".join(map(str,K)))

Buggy submission (verdict: Runtime Error):
N,Q=map(int,input().split())
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
p=[0 for i in range(N-1)]
q=[0 for i in range(N-1)]
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
for i in range(Q):
  p[i],q[i]=map(int,input().split())
#print(N,Q)
#print(a)
#print(b)
#print(p)
#print(q)

#c=list(zip(a,b))
#c.sort()
#a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
  K[p[i]-1]+=q[i]
#print(K)

#for i in range(N-1):
#  K[b[i]-1]+=K[a[i]-1]
#print(K)

print(" ".join(map(str,K)))

Buggy submission (verdict: Runtime Error):
N,Q=map(int,input().split())
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
p=[0 for i in range(N-1)]
q=[0 for i in range(N-1)]
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
for i in range(Q):
  p[i],q[i]=map(int,input().split())
#print(N,Q)
#print(a)
#print(b)
#print(p)
#print(q)

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
  K[p[i]-1]+=q[i]
#print(K)

#for i in range(N-1):
#  K[b[i]-1]+=K[a[i]-1]
#print(K)

print(" ".join(map(str,K)))

Buggy submission (verdict: Runtime Error):
N,Q=map(int,input().split())
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
p=[0 for i in range(N-1)]
q=[0 for i in range(N-1)]
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
for i in range(Q):
  p[i],q[i]=map(int,input().split())
#print(N,Q)
#print(a)
#print(b)
#print(p)
#print(q)

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
  K[p[i]-1]+=q[i]
#print(K)

for i in range(N-1):
  K[b[i]-1]+=K[a[i]-1]
#print(K)

print(" ".join(map(str,K)))

Buggy submission (verdict: Runtime Error):
n,q=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
ab=[list(map(int,list(input()))) for i in range(n)]
px=[list(map(int,list(input()))) for i in range(q)]
#print(ab)
#print(px)
ans= [[0 for i in range(n)] for i in range(n)]
ans2= [0 for i in range(n)]
for i in range(n):
    ans[i][i] = 1
for i in range(len(ab)):
    ans[ab[i][0]-1][ab[i][1]-1]+=1
for i in range(n):
    for j in range(i+1,n):
        if(ans[i][j]>0):
            for k in range(j,n):
                ans[i][k]+=ans[j][k]
for i in range(q):
    for j in range(n):
        ans2[j]+=px[i][1]* 1 if ans[px[i][0]-1][j] else 0
print(ans2)