Problem p02936 (python)

Fixed submission:
n,q=map(int,input().split())
d = {}
ans = [0]*(n+1)
for i in range(n-1):
    sta,gol = map(int,input().split())
    d[gol] = sta

for i in range(q):
    sta,val = map(int,input().split())
    ans[sta]+=val
    

for i in range(n):
    gol = i+1
    if gol in d:
        ans[gol]+=ans[d[gol]]
    

for i in range(n):
    print(ans[i+1], " ",end="")

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
d = {}
ans = [0]*(n+1)
for i in range(n-1):
    sta,gol = map(int,input().split())
    if sta not in d:
        d[sta] = set() 
    d[sta].add(gol)

dv = {}
for i in range(q):
    sta,val = map(int,input().split())
    if sta not in dv:
        dv[sta] = val 
    else:
        dv[sta]+=val
    

#print(d)
#print(dv)  

checked = set()

def cal_(ans_,sta_,flow,checked_):
    val_ = flow
    checked_.add(sta_)
    if sta_ in dv:
        val_ +=dv[sta_]
    
    ans_[sta_]+=val_
    
    if sta_ in d:
        targetset = d[sta_]
        for t in targetset:
            if t not in checked_:
                ans_=cal_(ans_,t,val_,checked_)
    return ans_

ans = cal_(ans,1,0,checked)

anss = ""
for i in range(n):
    anss = anss+str(ans[i+1])+" "
print(anss)

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
d = {}
ans = [0]*(n+1)
for i in range(n-1):
    sta,gol = map(int,input().split())
    if sta not in d:
        d[sta] = set() 
    d[sta].add(gol)

dv = {}
for i in range(q):
    sta,val = map(int,input().split())
    if sta not in dv:
        dv[sta] = val 
    else:
        dv[sta]+=val
    

#print(d)
#print(dv)  

checked = set()

def cal_(ans_,sta_,flow,checked_):
    val_ = flow
    checked_.add(sta_)
    if sta_ in dv:
        val_ +=dv[sta_]
    
    ans_[sta_]+=val_
    
    if sta_ in d:
        targetset = d[sta_]
        for t in targetset:
            if t not in checked_:
                ans_=cal_(ans_,t,val_,checked_)
    return ans_

ans = cal_(ans,1,0,checked)

anss = ""
for i in range(n):
    anss = anss+str(ans[i+1])+" "
print(anss)

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
d = {}
ans = [0]*(n+1)
for i in range(n-1):
    sta,gol = map(int,input().split())
    if sta not in d:
        d[sta] = set() 
    d[sta].add(gol)

dv = {}
for i in range(q):
    sta,val = map(int,input().split())
    if sta not in dv:
        dv[sta] = val 
    else:
        dv[sta]+=val
    

#print(d)
#print(dv)  

def cal_(ans_,sta_,flow):
    val_ = flow
    if sta_ in dv:
        val_ +=dv[sta_]
    
    ans_[sta_]+=val_
    
    if sta_ in d:
        targetset = d[sta_]
        for t in targetset:
            ans_=cal_(ans_,t,val_)
    return ans_

ans = cal_(ans,1,0)

anss = ""
for i in range(n):
    anss = anss+str(ans[i+1])+" "
print(anss)

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
d = {}
ans = [0]*(n+1)
for i in range(n-1):
    sta,gol = map(int,input().split())
    if sta not in d:
        d[sta] = set() 
    d[sta].add(gol)

dv = {}
for i in range(q):
    sta,val = map(int,input().split())
    if sta not in dv:
        dv[sta] = val 
    else:
        dv[sta]+=val
    

#print(d)
#print(dv)  

def cal_(ans_,sta_,flow):
    val_ = flow
    if sta_ in dv:
        val_ +=dv[sta_]
    
    ans_[sta_]+=val_
    
    if sta_ in d:
        targetset = d[sta_]
        for t in targetset:
            ans_=cal_(ans_,t,val_)
    return ans_

ans = cal_(ans,1,0)
    
for i in range(n):
    print(ans[i+1], " ",end="")

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
d = {}
ans = [0]*(n+1)
for i in range(n-1):
    sta,gol = map(int,input().split())
    if sta not in d:
        d[sta] = set() 
    d[sta].add(gol)

dv = {}
for i in range(q):
    sta,val = map(int,input().split())
    if sta not in dv:
        dv[sta] = val 
    else:
        dv[sta]+=val
    

#print(d)
#print(dv)  

def cal_(ans_,sta_,flow):
    val_ = flow
    if sta_ in dv:
        val_ +=dv[sta_]
    
    ans_[sta_]+=val_
    
    if sta_ in d:
        targetset = d[sta_]
        for t in targetset:
            ans_=cal_(ans_,t,val_)
    return ans_

ans = cal_(ans,1,0)
    
for i in range(n):
    print(ans[i+1], " ",end="")

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
d = {}
ans = [0]*(n+1)
for i in range(n-1):
    sta,gol = map(int,input().split())
    if sta not in d:
        d[sta] = set() 
    d[sta].add(gol)

def dig(d_,sta_):
    ansset = set()
    if sta_ in d_:
        targetset = d_[sta_]
        for t in targetset:
            ansset.add(t)
            tmpset = dig(d_,t)
            ansset= ansset|tmpset
    return ansset
  
for sta in d:
    d[sta] = dig(d,sta)
#print(d)
    
def cal_(ans_,sta_,val_):
    ans_[sta_]+=val_
    if sta_ in d:
        targetset = d[sta_]
        for t in targetset:
            ans_[t]+=val_
    return ans_

for i in range(q):
    sta,val = map(int,input().split())
    ans = cal_(ans,sta,val)
    
for i in range(n):
    print(ans[i+1], " ",end="")

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
d = {}
ans = [0]*(n+1)
for i in range(n-1):
    sta,gol = map(int,input().split())
    if sta not in d:
        d[sta] = set() 
    d[sta].add(gol)

#print(d)
    
def cal_(ans_,sta_,val_):
    ans_[sta_]+=val_
    if sta_ in d:
        targetset = d[sta_]
        for t in targetset:
            ans_ = cal_(ans_,t,val_)
    return ans_

for i in range(q):
    sta,val = map(int,input().split())
    ans = cal_(ans,sta,val)
    
for i in range(n):
    print(ans[i+1], " ",end="")