Problem p02936 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6) #for deep recursion

N, Q = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

counts = [0] * N
for op in operations:
    p, x = op
    counts[p-1] += x
    
all_connected_vertices = {i: [] for i in range(1, 1+N)}
for a, b in edges:
    all_connected_vertices[a].append(b)
    all_connected_vertices[b].append(a)
    
def dfs(point, pre):
    connected_vertices = all_connected_vertices[point]
    for c in connected_vertices:
        if c == pre:
            continue
        counts[c-1] += counts[point-1]
        dfs(c, point)
    
dfs(1, -1)
    
print(*counts)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6) #for deep recursion

N, Q = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
counts = [0] * N
operations = [list(map(int, input().split())) for _ in range(Q)]

counts = [0] * N
for op in operations:
    p, x = op
    counts[p-1] += x
    
all_connected_vertices = {i: [] for i in range(1, 1+N)}
for a, b in edges:
    all_connected_vertices[a].append(b)
    all_connected_vertices[b].append(a)
    
def dfs(point, pre):
    connected_vertices = all_connected_vertices[point]
    for c in children:
        if c == pre:
            continue
        counts[c-1] += counts[point-1]
        dfs(c, point)
    
dfs(1, -1)
    
print(*counts)

Buggy submission (verdict: Runtime Error):
def get_verteces_in_subtree():
    mem = {}
    
    def func(vertex):
        if vertex in mem.keys():
            return mem[vertex]
        
        children = tree_dictionary[vertex]
        if len(children) == 0:
            answer = [vertex]
        else:
            answer = [vertex] + sum([func(child) for child in children], [])
        mem[vertex] = answer
        return answer
    return func
  
get_verteces_in_subtree = get_verteces_in_subtree()

N, Q = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

tree_dictionary = {i: [] for i in range(1, N+1)}
for edge in edges:
    tree_dictionary[edge[0]].append(edge[1])

counts = [0] * N
for op in operations:
    top_vertex, value = op
    added_verteces = get_verteces_in_subtree(top_vertex)
    for v in added_verteces:
        counts[v-1] += value

output = ""
for c in counts:
    output += (str(c) + " ")
output = output[:-1]
print(output)

Buggy submission (verdict: Runtime Error):
def get_verteces_in_subtree():
    mem = {}
    
    def func(vertex):
        if vertex in mem.keys():
            return mem[vertex]
        
        children = tree_dictionary[vertex]
        if len(children) == 0:
            answer = [vertex]
        else:
            answer = [vertex] + sum([func(child) for child in children], [])
        mem[vertex] = answer
        return answer
    return func
  
  get_verteces_in_subtree = get_verteces_in_subtree()

N, Q = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

tree_dictionary = {i: [] for i in range(1, N+1)}
for edge in edges:
    tree_dictionary[edge[0]].append(edge[1])

counts = [0] * N
for op in operations:
    top_vertex, value = op
    added_verteces = get_verteces_in_subtree(top_vertex)
    for v in added_verteces:
        counts[v-1] += value

output = ""
for c in counts:
    output += (str(c) + " ")
output = output[:-1]
print(output)

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

@lru_cache(maxsize=4092)
def get_verteces_in_subtree(vertex):
    children = tree_dictionary[vertex]
    if len(children) == 0:
        return [vertex]
    return [vertex] + sum([get_verteces_in_subtree(child) for child in children], [])

N, Q = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

tree_dictionary = {i: [] for i in range(1, N+1)}
for edge in edges:
    tree_dictionary[edge[0]].append(edge[1])

counts = [0] * N
for op in operations:
    top_vertex, value = op
    added_verteces = get_verteces_in_subtree(top_vertex)
    for v in added_verteces:
        counts[v-1] += value

output = ""
for c in counts:
    output += (str(c) + " ")
output = output[:-1]
print(output)

Buggy submission (verdict: Runtime Error):
def get_verteces_in_subtree(vertex):
    children = tree_dictionary[vertex]
    if len(children) == 0:
        return [vertex]
    return [vertex] + sum([get_verteces_in_subtree(child) for child in children], [])

N, Q = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

tree_dictionary = {i: [] for i in range(1, N+1)}
for edge in edges:
    tree_dictionary[edge[0]].append(edge[1])

counts = [0] * N
for op in operations:
    top_vertex, value = op
    added_verteces = get_verteces_in_subtree(top_vertex)
    for v in added_verteces:
        counts[v-1] += value

output = ""
for c in counts:
    output += (str(c) + " ")
output = output[:-1]
print(output)

Buggy submission (verdict: Runtime Error):
def get_verteces_in_subtree(vertex):
    children = tree_dictionary[vertex]
    if len(children) == 0:
        return [vertex]
    return [vertex] + sum([get_verteces_in_subtree(child) for child in children], [])
  
N, Q = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

tree_dictionary = {i: [] for i in range(1, N+1)}
for edge in edges:
    tree_dictionary[edge[0]].append(edge[1])
    
counts = [0] * N
for op in operations:
    top_vertex, value = op
    added_verteces = get_verteces_in_subtree(top_vertex)
    print(added_verteces)
    for v in added_verteces:
        counts[v-1] += value
       
output = ""
for c in counts:
    output += (str(c) + " ")
output = output[:-1]
print(output)