Problem p02936 (python)

Fixed submission:
#D
       


#XXX=[int(x) for x in Input.split()]

n,q = (int(x) for x in input().split())
#n,q = XXX[0:2]

E = [[] for i in range(n+1)]
for i in range(n-1):
    a,b = (int(x) for x in input().split())
    #a,b = XXX[i*2+2:i*2+4]
    #A.append(a)
    #B.append(b)
    E[a].append(b)
    E[b].append(a)

P = []
X = []
for i in range(q):
    p,x = (int(x) for x in input().split())
    #p,x = XXX[i*2+n*2:i*2+n*2+2]
    P.append(p)
    X.append(x)

c = [0]*(n+1)
for i in range(len(P)):
    c[P[i]]+=X[i]

#sc = [-1]*(n+1)
sc = [0]*(n+1)
checked = [False]*(n+1)
stac = [1]

#dig(1,-1,0)

while stac:
    th = stac.pop()
    sc[th] += c[th]
    checked[th] = True
    
    for i in E[th]:
        if checked[i] == False:
            sc[i] += sc[th]
            stac.append(i)

result = map(str, sc[1:])
print(" ".join(result))

Buggy submission (verdict: Runtime Error):
#D

def dig(n, scn, count):
    global sc
    #global checed

    if scn == -1:
        scn = 0
    else:
        return
    
    scn = count + c[n]
    sc[n] = scn
    #checked.append(n)
    
    for i in E[n]:
        #if not(i in checked):
        dig(i, sc[i], scn)

        


#XXX=[int(x) for x in Input.split()]

n,q = (int(x) for x in input().split())
#n,q = XXX[0:2]

E = [[] for i in range(n+1)]
for i in range(n-1):
    a,b = (int(x) for x in input().split())
    #a,b = XXX[i*2+2:i*2+4]
    #A.append(a)
    #B.append(b)
    E[a].append(b)
    E[b].append(a)

P = []
X = []
for i in range(q):
    p,x = (int(x) for x in input().split())
    #p,x = XXX[i*2+n*2:i*2+n*2+2]
    P.append(p)
    X.append(x)

c = [0]*(n+1)
for i in range(len(P)):
    c[P[i]]+=X[i]

sc = [-1]*(n+1)
#sc = [0]*(n+1)
#checked = []

dig(1,-1,0)
result = map(str, sc[1:])
print(" ".join(result))

Buggy submission (verdict: Runtime Error):
#D

def dig(n, count):
    global sc
    global checed

    #if sc[n] == -1:
    #    sc[n] = 0
    #else:
    #    return
    
    sc[n] = count + c[n]
    checked.append(n)
    
    for i in E[n]:
        if not(i in checked):
            dig(i, sc[n])

        


#XXX=[int(x) for x in Input.split()]

n,q = (int(x) for x in input().split())
#n,q = XXX[0:2]

E = [[] for i in range(n+1)]
for i in range(n-1):
    a,b = (int(x) for x in input().split())
    #a,b = XXX[i*2+2:i*2+4]
    #A.append(a)
    #B.append(b)
    E[a].append(b)
    E[b].append(a)

P = []
X = []
for i in range(q):
    p,x = (int(x) for x in input().split())
    #p,x = XXX[i*2+n*2:i*2+n*2+2]
    P.append(p)
    X.append(x)

c = [0]*(n+1)
for i in range(len(P)):
    c[P[i]]+=X[i]

#sc = [-1]*(n+1)
sc = [0]*(n+1)
checked = []

dig(1,0)
result = map(str, sc[1:])
print(" ".join(result))

Buggy submission (verdict: Runtime Error):
#D

def dig(n, count):
    global sc

    if sc[n] == -1:
        sc[n] = 0
    else:
        return
    
    sc[n] = count + c[n]
    
    for i in E[n]:
        dig(i, sc[n])

        


#XXX=[int(x) for x in Input.split()]

n,q = (int(x) for x in input().split())
#n,q = XXX[0:2]

#A = []
#B = []
E = [[] for i in range(n+1)]
for i in range(n-1):
    a,b = (int(x) for x in input().split())
    #a,b = XXX[i*2+2:i*2+4]
    #A.append(a)
    #B.append(b)
    E[a].append(b)
    E[b].append(a)

P = []
X = []
for i in range(q):
    p,x = (int(x) for x in input().split())
    #p,x = XXX[i*2+n*2:i*2+n*2+2]
    P.append(p)
    X.append(x)

c = [0]*(n+1)
for i in range(len(P)):
    c[P[i]]+=X[i]

#print(c)

sc = [-1]*(n+1)

dig(1,0)
result = map(str, sc[1:])
print(" ".join(result))