Problem p02936 (python)

Fixed submission:
n,q = (int(num) for num in input().split())
par = [n] * n
visited = [0] * n
val = [0] * n
ab = []
for i in range(n-1):
    a,b = (int(num)-1 for num in input().split())
    par[b] = a
    
for _ in range(q):
    p,x = (int(num) for num in input().split())
    p -= 1
    val[p] += x

for i in range(1,n):
    val[i] += val[par[i]]
    
ans = ""
for num in val:
    ans += str(num) + " "
print(ans)

Buggy submission (verdict: Runtime Error):

import sys

sys.setrecursionlimit(200000)

n,q = (int(num) for num in input().split())
neighbor = [[] for _ in range(n)]
visited = [0] * n
val = [0] * n
ab = []
for i in range(n-1):
    a,b = (int(num)-1 for num in input().split())
    ab.append((a,b))

    
for item in ab:
    a = item[0]
    b = item[1]
    neighbor[a].append(b)
    neighbor[b].append(a)
    
        
#print(par)
#print(neighbor)
#print(rank)

for _ in range(q):
    p,x = (int(num) for num in input().split())
    p -= 1
    val[p] += x
    
que = [0]
visited[0] = 1
last = 0
while que != []: 
    idx = que.pop(0)
    val[idx] += last
    last = val[idx]
    for i in neighbor[idx]:
        if visited[i] == 0:
            visited[i] = 1
            que.append(i)
            
for num in val:
    ans += str(num) + " "
print(ans)

Buggy submission (verdict: Runtime Error):


n,q = (int(num) for num in input().split())
neighbor = [[] for _ in range(n)]
visited = [0] * n
val = [0] * n
ab = []
for i in range(n-1):
    a,b = (int(num)-1 for num in input().split())
    ab.append((a,b))

    
for item in ab:
    a = item[0]
    b = item[1]
    neighbor[a].append(b)
    neighbor[b].append(a)
    
        
#print(par)
#print(neighbor)
#print(rank)

for _ in range(q):
    p,x = (int(num) for num in input().split())
    p -= 1
    val[p] += x
    
def calc(idx,neighbor,val,bias):
    val[idx] += bias
    for i in neighbor[idx]:
        if visited[i] == 0:
            visited[i] = 1
            calc(i,neighbor,val,val[idx])

visited[0] = 1
calc(0,neighbor,val,0)    
ans = ""    
for num in val:
    ans += str(num) + " "
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect

n,q = (int(num) for num in input().split())
par = [i for i in range(n)]
nex = [[] for _ in range(n)]
ch = [0] * n
rank = [1] * n
val = [0] * n
ab = []
for i in range(n-1):
    a,b = (int(num)-1 for num in input().split())
    if a > b:
        a,b = b,a
    ab.append((a,b))
ab.sort(key = lambda x: x[0])
    
for item in ab:
    a = item[0]
    b = item[1]
    if rank[a] > rank[b]:
        par[b] = a
        nex[a].append(b)
    elif rank[a] < rank[b]:
        par[a] = b
        nex[b].append(a)
    elif a < b:
        par[b] = a
        nex[a].append(b)
        rank[a] += 1
    else:
        par[a] = b
        nex[b].append(a)
        rank[b] += 1
        
#print(par)
#print(nex)
#print(rank)

for _ in range(q):
    p,x = (int(num) for num in input().split())
    p -= 1
    val[p] += x
    
def calc(idx,nex,val,bias):
    val[idx] += bias
    if nex[idx] == []:
        return
    else:
        for i in nex[idx]:
            calc(i,nex,val,val[idx])
        
calc(0,nex,val,0)    
ans = ""    
for num in val:
    ans += str(num) + " "
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect

n,q = (int(num) for num in input().split())
par = [i for i in range(n)]
nex = [[] for _ in range(n)]
ch = [0] * n
rank = [1] * n
val = [0] * n
ab = []
for i in range(n-1):
    a,b = (int(num)-1 for num in input().split())
    if a > b:
        a,b = b,a
    ab.append((a,b))
ab.sort(key = lambda x: x[0])
    
for item in ab:
    a = item[0]
    b = item[1]
    if rank[a] > rank[b]:
        par[b] = a
        nex[a].append(b)
    elif rank[a] < rank[b]:
        par[a] = b
        nex[b].append(a)
    elif a < b:
        par[b] = a
        nex[a].append(b)
        rank[a] += 1
    else:
        par[a] = b
        nex[b].append(a)
        rank[b] += 1
        
#print(par)
#print(nex)
#print(rank)

for _ in range(q):
    p,x = (int(num) for num in input().split())
    p -= 1
    val[p] += x
    
def calc(idx,nex,val,bias):
    val[idx] += bias
    if nex[idx] == []:
        return
    else:
        for i in nex[idx]:
            calc(i,nex,val,val[idx])
        
calc(0,nex,val,0)    
    
print(val)