Problem p02936 (python)

Fixed submission:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

#n = int(input())
n,q = [int(i) for i in readline().split()]

g = [[] for _ in range(n)]
for i in range(n-1):
    a,b = [int(i) for i in readline().split()]
    g[a-1].append(b-1)
    g[b-1].append(a-1)

add = [0]*n
for _ in range(q):
    p,x = [int(i) for i in readline().split()]
    add[p-1] += x

ans = [0]*n
#ans[0] = add[0]

def dfs(v,p,value): #p: parent of v
    value += add[v]
    ans[v] = value
    for c in g[v]:
        if c == p: continue
        dfs(c,v,value)

dfs(0,-1,0)

#print(add)

print(*ans)

Buggy submission (verdict: Runtime Error):
#%%
N, Q = [int(i) for i in input().split()]
AB = [[] for _ in range(N - 1)]
PX = [[] for _ in range(Q)]
for i in range(N - 1):
    AB[i] = [int(i) - 1 for i in input().split()]
for i in range(Q):
    PX[i] = [int(i) for i in input().split()]
    PX[i][0] -= 1
#%%

class tree:
    def __init__(self, id):
        self.id = id
        self.parent = -1
        self.children = []
        self.value = 0

def DFS(node_id, value, list_tree):
    list_tree[node_id].value += value
    for i in list_tree[node_id].children:
        DFS(i, value, list_tree)

#%%     
list_node = [tree(i) for i in range(N)]
for i, j in AB:
    list_node[i].children.append(j)
    list_node[j].parent = i

for i, j in PX:
    DFS(i, j, list_node)
    
for i in list_node:
    print(i.value, end=" ")

Buggy submission (verdict: Runtime Error):
#%%
import sys
sys.setrecursionlimit(10 ** 9)
from collections import deque

class Node:
      
    def __init__(self):
        self.id = -1
        self.parent = -1
        self.children = []    
        self.value = 0
        

def main():
    global instance_list
    N, Q = [int(i) for i in input().split()]
    instance_list = [Node() for _ in range(N)]

    for i in range(N - 1):
        temp1, temp2 = [int(i) - 1 for i in input().split()]
        instance_list[temp1].children.append(temp2)
        instance_list[temp2].parent = temp1

    for i in range(Q):
        start, value = [int(i) for i in input().split()]
        start -= 1
        instance_list[start].value += value
        
    que = deque()
    que.append([instance_list[0], 0])
    while len(que) > 0 :
        node, cost = que.popleft()
        instance_list[node].value += cost
        for i in instance_list[node].children:
            que.append([i, cost])
    

    ans = [instance_list[i].value for i in range(N)]
    print(" ".join(map(str, ans)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#%%
class Node():
    
    def __init__(self):
        self.parent = -1
        self.left = -1
        self.right = -1
        self.value = 0
        
    @classmethod
    def get_parent_children(cls, node_data):
        
        if instance_list[node_data[0]].left == -1:
            instance_list[node_data[0]].left = node_data[1]
        elif instance_list[node_data[0]].right == -1:
            instance_list[node_data[0]].right = node_data[1]
            
        instance_list[node_data[1]].parent = node_data[0]
        
def DFS_138(u, value):
    if u == -1:
        return 
    
    instance_list[u].value = instance_list[u].value + value
    DFS_138(instance_list[u].left, value)
    DFS_138(instance_list[u].right, value)
    
#%%
N, Q = [int(i) for i in input().split()]
node_data = [[] for _ in range(N - 1)]
for i in range(N - 1):
    node_data[i] = [int(i) - 1 for i in input().split()]
    
value_data = [[] for _ in range(Q)]
for i in range(Q):
    value_data[i] = [int(i) for i in input().split()]
    value_data[i][0] -= 1
#%%
instance_list = [Node() for _ in range(N)]

for i in node_data:
    Node.get_parent_children(i)
    
for i in value_data:
    DFS_138(i[0], i[1])
#%%
ans = [instance_list[i].value for i in range(N)]
print(" ".join(map(str, ans)))