Problem p02936 (python)

Fixed submission:
N,Q = [int(x) for x in input().split()]
ki = [0]*N
tr = [[] for _ in range(N)]

for i in range(N-1):
    a,b = [int(x)-1 for x in input().split()]
    tr[a].append(b)
    tr[b].append(a)

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1] += val

stack =[0]
visited = set()
visited.add(0)

while stack:
    i = stack.pop()
    for j in tr[i]:
        if j in visited:
            continue
        visited.add(j)
        ki[j] += ki[i]
        stack.append(j)

for i in ki:
    print(i,end=" ")
print()

Buggy submission (verdict: Runtime Error):
class Tree:
    value=0
    def __init__(self,position):
        self.position = position
        self.kid = set()
        self.value = 0

    def add_kid(self,it):
        self.kid.add(it)

    def add_val(self,val):
        self.value += val

    def calc(self,val=0):
        self.add_val(val)
        # print("called",self.position)
        for i in self.kid:
            if(i.position in visited)):
                continue
            visited.add(i.position)
            i.calc(self.value)

    def print_val(self):
        print(self.value,end=" ")

N,Q = [int(x) for x in input().split()]
ki = [Tree(i) for i in range(N)]

for i in range(N-1):
    a,b = [int(x) for x in input().split()]
    ki[a-1].add_kid(ki[b-1])
    ki[b-1].add_kid(ki[a-1])

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1].add_val(val)

visited = set()
visited.add(0)
ki[0].calc()

for i in ki:
    i.print_val()
print()

Buggy submission (verdict: Runtime Error):
N,Q = [int(x) for x in input().split()]
ki = [0]*N
tr = [[] for _ in range(N)]

for i in range(N-1):
    a,b = [int(x)-1 for x in input().split()]
    tr[a].add(b)
    tr[b].add(a)

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1] += val

stack =[1]
vivisted = set()

while stack:
    i = stack.pop()
    for j in tr[i]:
        if j in visited:
            continue
        visited.add(j)
        ki[j] += ki[i]
        stack.append(j)

for i in ki:
    print(i,end=" ")
print()

Buggy submission (verdict: Runtime Error):
class Tree:
    value=0
    def __init__(self):
        self.kid = []
        self.value = 0

    def add_kid(self,it):
        self.kid.append(it)

    def add_val(self,val):
        self.value += val

    def calc(self,val=0):
        self.add_val(val)
        # print("called",len(self.kid))
        for i in self.kid:
            i.calc(self.value)

    def print_val(self):
        print(self.value,end=" ")

N,Q = [int(x) for x in input().split()]
ki = [Tree() for _ in range(N)]

for i in range(N-1):
    parent,kid = [int(x) for x in input().split()]
    ki[parent-1].add_kid(ki[kid-1])

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1].add_val(val)

ki[0].calc()

for i in ki:
    i.print_val()
print()

Buggy submission (verdict: Runtime Error):
class ki:
    value=0
    def __init__(self):
        self.kid=[]

    def add_kid(self,it):
        self.kid.append(it)

    def calc(self,list_k,val=0):
        #print(self.kid)
        self.value+=val
        for i in self.kid:
            list_k[i].calc(list_k,self.value)


N,Q=input().split()
N,Q=int(N),int(Q)
ki_list=[ki()]
for i in range(1,N+1):
    ki_list.append(ki())

for i in range(N-1):
    a,b=input().split()
    ki_list[int(a)].add_kid(int(b))

for i in range(Q):
    p,x=input().split()
    ki_list[int(p)].value+=int(x)

ki_list[1].calc(ki_list)

print(ki_list[1].value ,end="")
for i in ki_list[2:]:
    print(" " + str(i.value),end="")