Problem p02936 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)    
from collections import deque
input = sys.stdin.readline

seen=[]
#seen=dict()
path=[]

def dfs(g, v,add):
    xx[v]+=add
#    path.append(v)
    seen[v]=True
    for next_v in g[v]:
        if seen[next_v]:
            continue
        dfs(g, next_v,xx[v])
#    path.append(v)

    return

icase=-1
if icase==-1:
    n,q=map(int,input().split())
#    g = [deque([]) for _ in range(n)]
    ab=[[int(x) for x in input().split()] for  _ in range(n-1)]
    px=[[int(x) for x in input().split()] for  _ in range(q)]

    g = [set() for _ in range(n)]
    for ai,bi in ab:
        g[ai-1].add(bi-1)
        g[bi-1].add(ai-1)
    g=tuple(g)

    xx=[0]*n
    for pi,xi in px:
        xx[pi-1]=xx[pi-1]+xi
if icase==0:
    n,q=map(int,input().split())
#    g = [deque([]) for _ in range(n)]
    g = [set() for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
#        g[ai-1].append(bi-1)
#        g[bi-1].append(ai-1)
        g[ai-1].add(bi-1)
        g[bi-1].add(ai-1)
    g=tuple(g)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
elif icase==3:
    n,q=6,2
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1,2])
    g[1]=deque([0,3,4])
    g[2]=deque([0,5])
    g[3]=deque([1])
    g[4]=deque([1])
    g[5]=deque([2])
    p=[0]*q
    x=[0]*q
    p[0]=0
    x[0]=10    
    p[1]=0
    x[1]=10    
    xx=[0]*n
    for i in range(q):
#        pi,xi=map(int,input().split())
        xx[p[i]]=xx[p[i]]+x[i]
elif icase==1:
    n,q=3,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([2])
    g[1]=deque([2])
    g[2]=deque([0,1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=2
    x[1]=100
    p[2]=0
    x[2]=1    
    xx=[0]*n
    for i in range(q):
#        pi,xi=map(int,input().split())
        xx[p[i]]=xx[p[i]]+x[i]
#ans:1 111 101
elif icase==2:
    n,q=4,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1])
    g[1]=deque([0,2,3])
    g[2]=deque([1])
    g[3]=deque([1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=0
    x[1]=100
    p[2]=2
    x[2]=1    
    xx=[0]*n
    for i in range(q):
#        pi,xi=map(int,input().split())
        xx[p[i]]=xx[p[i]]+x[i]
#ans:100 110 111 110

#c=[0]*n

seen=[False]*n
#for i in range(n):
#    seen[i]=False
dfs(g,0,0)

#print(path)

'''
seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()
'''

ans=" ".join(map(str,xx[0:]))
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
input = sys.stdin.readline

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [set() for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].add(bi-1)
        g[bi-1].add(ai-1)
    g=tuple(g)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
elif icase==2:
    n,q=4,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1])
    g[1]=deque([0,2,3])
    g[2]=deque([1])
    g[3]=deque([1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=0
    x[1]=100
    p[2]=2
    x[2]=1    
    xx=[0]*n
    for i in range(q):
        xx[p[i]]=xx[p[i]]+x[i]
#ans:100 110 111 110
elif icase==3:
    n,q=6,2
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1,2])
    g[1]=deque([0,3,4])
    g[2]=deque([0,5])
    g[3]=deque([1])
    g[4]=deque([1])
    g[5]=deque([2])
    p=[0]*q
    x=[0]*q
    p[0]=0
    x[0]=10    
    p[1]=0
    x[1]=10    
    xx=[0]*n
    for i in range(q):
#        pi,xi=map(int,input().split())
        xx[p[i]]=xx[p[i]]+x[i]
        
#q = deque([0])
stack=[0]
parent=[-1]*n
while stack:
#    print(q)
    state = stack.pop()
#    print("------------- stack:",stack,"state:",state,"g:",g[state])
    for v in g[state]:
#        print("state:",state,"v:",v,"q:",q)
        if parent[state] == v:
            continue
        else:
            parent[v]=state
            stack.append(v)
            xx[v]+=xx[state]
#            print("parent:",parent,"xx:",xx,"stack",stack)

print(" ".join(map(str,xx[0: ])))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)    
from collections import deque

#seen=[]
seen=dict()
path=[]

def dfs(g, v):
    path.append(v)
    seen[v]=True
    for next_v in g[v]:
        if seen[next_v]:
            continue
        dfs(g, next_v)
    path.append(v)

    return

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
    
c=[0]*n

#seen=[False]*n
for i in range(n):
    seen[i]=False
dfs(g,0)

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)    
from collections import deque

seen=[]
path=[]

def dfs(g, v):
    path.append(v)
    seen[v]=True
    for next_v in g[v]:
        if seen[next_v]:
            continue
        dfs(g, next_v)
    path.append(v)

    return

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
    
c=[0]*n

seen=[False]*n
dfs(g,0)

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)    
from collections import deque

seen=[]
path=[]

def dfs(g, v):
    path.append(v)
    seen[v]=True
    for next_v in g[v]:
        if seen[next_v]:
            continue
        dfs(g, next_v)
    path.append(v)

    return

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
    
c=[0]*n

seen=[False]*n
dfs(g,0)

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(20000)    
from collections import deque

seen=[]
first_order =[]
last_order = []
path=[]

def dfs(g, v, first_ptr, last_ptr,xi):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    first_order[v] = first_ptr
    path.append(v)
    seen[v]=True
#    c[v]+=xi
#    print(v,c,g[v])          
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr,xi)
    last_ptr=last_ptr+1
    first_ptr=last_ptr
    last_order[v] = last_ptr
    path.append(v)

    return first_ptr, last_ptr

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        pi,xi=map(int,input().split())
        p[i]=pi-1
        x[i]=xi
elif icase==1:
    n,q=3,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([2])
    g[1]=deque([2])
    g[2]=deque([0,1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=2
    x[1]=100
    p[2]=0
    x[2]=1    
#ans:1 111 101
elif icase==2:
    n,q=4,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1])
    g[1]=deque([0,2,3])
    g[2]=deque([1])
    g[3]=deque([1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=0
    x[1]=100
    p[2]=2
    x[2]=1    
#ans:100 110 111 110
elif icase==3:
    n,q=6,2
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1,2])
    g[1]=deque([0,3,4])
    g[2]=deque([0,5])
    g[3]=deque([1])
    g[4]=deque([1])
    g[5]=deque([2])
    p=[0]*q
    x=[0]*q
    p[0]=0
    x[0]=10    
    p[1]=0
    x[1]=10
#ans:100 110 111 110

xx=[0]*n
for i in range(q):
    xx[p[i]]=xx[p[i]]+x[i]
    
c=[0]*n

#for i in range(q):
#    seen=[False]*n
#    first_ptr=0
#    last_ptr=0
#    first_order =[-1]*n
#    last_order = [-1]*n
#    first_ptr, last_ptr=dfs(g,p[i],first_ptr, last_ptr,x[i])

seen=[False]*n
first_ptr=0
last_ptr=0
first_order =[-1]*n
last_order = [-1]*n
first_ptr, last_ptr=dfs(g,0,first_ptr, last_ptr,x[0])

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)    
from collections import deque

seen=[]
first_order =[]
last_order = []
path=[]

def dfs(g, v, first_ptr, last_ptr,xi):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    first_order[v] = first_ptr
    path.append(v)
    seen[v]=True
#    c[v]+=xi
#    print(v,c,g[v])          
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr,xi)
    last_ptr=last_ptr+1
    first_ptr=last_ptr
    last_order[v] = last_ptr
    path.append(v)

    return first_ptr, last_ptr

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        pi,xi=map(int,input().split())
        p[i]=pi-1
        x[i]=xi
elif icase==1:
    n,q=3,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([2])
    g[1]=deque([2])
    g[2]=deque([0,1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=2
    x[1]=100
    p[2]=0
    x[2]=1    
#ans:1 111 101
elif icase==2:
    n,q=4,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1])
    g[1]=deque([0,2,3])
    g[2]=deque([1])
    g[3]=deque([1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=0
    x[1]=100
    p[2]=2
    x[2]=1    
#ans:100 110 111 110
elif icase==3:
    n,q=6,2
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1,2])
    g[1]=deque([0,3,4])
    g[2]=deque([0,5])
    g[3]=deque([1])
    g[4]=deque([1])
    g[5]=deque([2])
    p=[0]*q
    x=[0]*q
    p[0]=0
    x[0]=10    
    p[1]=0
    x[1]=10
#ans:100 110 111 110

xx=[0]*n
for i in range(q):
    xx[p[i]]=xx[p[i]]+x[i]
    
c=[0]*n

#for i in range(q):
#    seen=[False]*n
#    first_ptr=0
#    last_ptr=0
#    first_order =[-1]*n
#    last_order = [-1]*n
#    first_ptr, last_ptr=dfs(g,p[i],first_ptr, last_ptr,x[i])

seen=[False]*n
first_ptr=0
last_ptr=0
first_order =[-1]*n
last_order = [-1]*n
first_ptr, last_ptr=dfs(g,0,first_ptr, last_ptr,x[0])

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Buggy submission (verdict: Runtime Error):

import sys
sys.setrecursionlimit(10000)    
from collections import deque

seen=[]
first_order =[]
last_order = []

def dfs(g, v, first_ptr, last_ptr,xi):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True
    c[v]+=xi
#    print(v,c,g[v])          
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr,xi)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr


icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        pi,xi=map(int,input().split())
        p[i]=pi-1
        x[i]=xi

c=[0]*n

for i in range(q):
    seen=[False]*n
    first_ptr=0
    last_ptr=0
    first_order =[-1]*n
    last_order = [-1]*n
    first_ptr, last_ptr=dfs(g,p[i],first_ptr, last_ptr,x[i])

for i in range(n):
    print(c[i],end=" ")
print()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)    
from collections import deque

seen=[]
first_order =[]
last_order = []

def dfs(g, v, first_ptr, last_ptr,xi):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True
    c[v]+=xi
#    print(v,c,g[v])          
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr,xi)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr


icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        pi,xi=map(int,input().split())
        p[i]=pi-1
        x[i]=xi

c=[0]*n

for i in range(q):
    seen=[False]*n
    first_ptr=0
    last_ptr=0
    first_order =[-1]*n
    last_order = [-1]*n
    first_ptr, last_ptr=dfs(g,p[i],first_ptr, last_ptr,x[i])

for i in range(n):
    print(c[i],end="")
print()