Problem p02936 (python)

Fixed submission:
from collections import defaultdict as dd
n, q = map(int, input().split())
Connections = dd(list)
for i in range(n-1):
    a, b = map(int, input().split())
    Connections[a].append(b)
    Connections[b].append(a)

#Tree[node] -> children(list)
Tree = dd(list)
visited = set()
stack = [1]
while stack != []:
    temp = []
    for taregt_node in stack:
        for next_node in Connections[taregt_node]:
            if next_node not in visited:
                Tree[taregt_node].append(next_node)
                visited.add(taregt_node)
                temp.append(next_node)
    stack = temp
#print(Tree)

#Point[node] -> point
Point = dd(int)
for i in range(q):
    p, x = map(int, input().split())
    Point[p] += x
#print(Point)

Counter = dd(int)
#visited = set()
stack = [1]
Counter[1] = Point[1]
while stack != []:
    temp = []
    for target_node in stack:
        for next_node in Tree[target_node]:
            Counter[next_node] = Counter[target_node] + Point[next_node]
            temp.append(next_node)
    stack = temp
#print(Counter)

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

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**5)

from collections import defaultdict as dd
n, q = map(int, input().split())
Tree = dd(lambda: [])
for i in range(n-1):
    a, b = map(int, input().split())
    Tree[a].append(b)

Point = dd(lambda: 0)
for i in range(q):
    p, x = map(int, input().split())
    Point[p] += x

Ans = {}
def write(node, point):
    point += Point[node]
    Ans[node] = point
    for next_node in Tree[node]:
        write(next_node, point)
write(1, 0)

for i in range(1, n+1):
    print(Ans[i])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict as dd
n, q = map(int, input().split())
Tree = dd(lambda: [])
for i in range(n-1):
    a, b = map(int, input().split())
    Tree[a].append(b)

Point = dd(lambda: 0)
for i in range(q):
    p, x = map(int, input().split())
    Point[p] += x

Ans = dd(lambda: 0)
def write(node, point):
    point += Point[node]
    Ans[node] = point
    for next_node in Tree[node]:
        write(next_node, point)
write(1, 0)

for i in range(1, n+1):
    print(Ans[i])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict as dd
n, q = map(int, input().split())
Tree = dd(lambda: [])
for i in range(n-1):
    a, b = map(int, input().split())
    Tree[a].append(b)

Point = dd(lambda: 0)
for i in range(q):
    p, x = map(int, input().split())
    Point[p] += x

Ans = {}
def write(node, point):
    point += Point[node]
    Ans[node] = point
    for next_node in Tree[node]:
        write(next_node, point)
write(1, 0)

for i in range(1, n+1):
    print(Ans[i])

Buggy submission (verdict: Runtime Error):
n, q = input().split(" ")
n = int(n)
q = int(q)

list_ab = []
for i in range(n - 1):
  pair_ab = [int(num) for num in input().split(" ")]
  list_ab.append(pair_ab)

list_px = []
for i in range(q):
  pair_px = [int(num) for num in input().split(" ")]
  list_px.append(pair_px)

def make_dict(list_ab):
  dict = {}
  for num in range(1, n+1):
    dict[num] = []
  for pair_ab in list_ab:
    dict[int(pair_ab[0])].append(pair_ab[1])
  return dict

ab_dict = make_dict(list_ab)

counter = [0]*(n+1)

def calc(position, num_plus):
  counter[position] += num_plus
  list_plus = ab_dict[position]
  for num in list_plus:
    calc(num, num_plus)

for p, x in list_px:
  calc(p, x)

for i in range(1, len(counter)):
  counter[i] = str(counter[i])
counter.pop(0)
str = " ".join(counter)
print(str)

Buggy submission (verdict: Runtime Error):
n, q = input().split(" ")
n = int(n)
q = int(q)

list_ab = []
for i in range(n - 1):
  pair_ab = [int(num) for num in input().split(" ")]
  list_ab.append(pair_ab)

list_px = []
for i in range(q):
  pair_px = [int(num) for num in input().split(" ")]
  list_px.append(pair_px)

def make_dict(list_ab):
  dict = {}
  for num in range(1, n+1):
    dict[num] = []
  for pair_ab in list_ab:
    dict[int(pair_ab[0])].append(pair_ab[1])
  return dict

ab_dict = make_dict(list_ab)

counter = [0]*(n+1)

def calc(position, num_plus):
  counter[position] += num_plus
  list_plus = ab_dict[position]
  for num in list_plus:
    calc(num, num_plus)

for p, x in list_px:
  calc(p, x)

for i in range(len(counter)):
  counter[i] = str(counter[i])
str = " ".join(counter)
print(str)