Problem p02936 (python)

Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(100000)
from collections import deque

S = input().split(" ")
N = int(S[0])
Q = int(S[1])

arr = []
prr = []
for i in range(N-1):
    S = input().split(" ")
    ar = [int(S[0]),int(S[1])]
    arr.append(ar)

for i in range(Q):
    S = input().split(" ")
    pr = [int(S[0]), int(S[1])]
    prr.append(pr)



def prepare(n, q, arr, prr):
    links = [[] for _ in range(n)]
    values = [0 for _ in range(n)]
    finalValues = [0 for _ in range(n)]
    for ar in arr:
        start = ar[0] - 1
        end = ar[1] - 1
        links[start].append(end)
        links[end].append(start)

    for pr in prr:
        i = pr[0] - 1
        x = pr[1]
        values[i] += x

    return links, values, finalValues


links, values, finalValues = prepare(N, Q, arr, prr)


def bfs():
    q = deque()

    q.append((0,-1,0))

    while len(q) > 0:
        currentNode,parentNode,parentAccumulate = q.popleft()
        parentAccumulate += values[currentNode]
        finalValues[currentNode] += parentAccumulate
        childNodes = links[currentNode]
        for ch in childNodes:
            if ch == parentNode:
                continue
            q.append((ch,currentNode,finalValues[currentNode]))

bfs()
print(*finalValues)

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())

ARR = []

for i in range(N-1):
    ARR.append(list(map(int, input().split())))

BRR = []

for i in range(Q):
    BRR.append(list(map(int, input().split())))


def prepare(n, q, arr, brr):
    nodePoints = [0 for i in range(n)]
    for i in range(q):
        nodeIndex = brr[i][0] - 1
        nodePoints[nodeIndex] += brr[i][1]

    nodes = [[] for i in range(n)]

    for i in range(n - 1):
        startNode = arr[i][0] - 1
        endNode = arr[i][1] - 1

        nodes[startNode].append(endNode)
        nodes[endNode].append(startNode)

    return nodes, nodePoints


nodes, nodePoints = prepare(N, Q, ARR, BRR)


def dfs(currentNode, nodes, parentNode=-1, parentPoint=0):
    nodePoints[currentNode] = nodePoints[currentNode] + parentPoint
    childNodes = nodes[currentNode]
    for childNode in childNodes:
        if childNode == parentNode:
            continue
        dfs(childNode, nodes, currentNode, nodePoints[currentNode])


dfs(0, nodes)

print(" ".join([str(nodePoints[i]) for i in range(N)]))

Buggy submission (verdict: Runtime Error):
from sys import setrecursionlimit
setrecursionlimit(100000)

S = input().split(" ")
N = int(S[0])
Q = int(S[1])

arr = []
prr = []
for i in range(N-1):
    S = input().split(" ")
    ar = [int(S[0]),int(S[1])]
    arr.append(ar)

for i in range(Q):
    S = input().split(" ")
    pr = [int(S[0]), int(S[1])]
    prr.append(pr)



def prepare(n, q, arr, prr):
    links = [[] for _ in range(n)]
    values = [0 for _ in range(n)]
    lineStates = [-1 for _ in range(n)]
    finalValues = [0 for _ in range(n)]
    for ar in arr:
        start = ar[0] - 1
        end = ar[1] - 1
        links[start].append(end)
        links[end].append(start)

    for pr in prr:
        i = pr[0] - 1
        x = pr[1]
        values[i] += x

    return links, values, lineStates, finalValues


links, values, lineStates, finalValues = prepare(N, Q, arr, prr)


def dfs(currentNode, arr, parrentAccumulate):
    childNodes = links[currentNode]
    parrentAccumulate += values[currentNode]
    finalValues[currentNode] = parrentAccumulate
    lineStates[currentNode] = 1
    for ch in childNodes:
        if lineStates[ch] == -1:
            dfs(ch, arr, parrentAccumulate)


dfs(0, links, 0)
print(*finalValues)

Buggy submission (verdict: Runtime Error):
from sys import setrecursionlimit
setrecursionlimit(100000)

S = input().split(" ")
N = int(S[0])
Q = int(S[1])

arr = []
prr = []
for i in range(N-1):
    S = input().split(" ")
    ar = [int(S[0]),int(S[1])]
    arr.append(ar)

for i in range(Q):
    S = input().split(" ")
    pr = [int(S[0]), int(S[1])]
    prr.append(pr)



def prepare(n, q, arr, prr):
    links = [[] for _ in range(n)]
    values = [0 for _ in range(n)]
    lineStates = [-1 for _ in range(n)]
    finalValues = [0 for _ in range(n)]
    for ar in arr:
        start = ar[0] - 1
        end = ar[1] - 1
        links[start].append(end)
        links[end].append(start)

    for pr in prr:
        i = pr[0] - 1
        x = pr[1]
        values[i] += x

    return links, values, lineStates, finalValues


links, values, lineStates, finalValues = prepare(N, Q, arr, prr)


def dfs(currentNode, arr, parrentAccumulate):
    childNodes = links[currentNode]
    parrentAccumulate += values[currentNode]
    finalValues[currentNode] = parrentAccumulate
    lineStates[currentNode] = 1
    for ch in childNodes:
        if lineStates[ch] == -1:
            dfs(ch, arr, parrentAccumulate)


dfs(0, links, 0)
print(" ".join([str(s) for s in finalValues]))

Buggy submission (verdict: Runtime Error):
S = input().split(" ")
N = int(S[0])
Q = int(S[1])

arr = []
prr = []
for i in range(N-1):
    S = input().split(" ")
    ar = [int(S[0]),int(S[1])]
    arr.append(ar)

for i in range(Q):
    S = input().split(" ")
    pr = [int(S[0]), int(S[1])]
    prr.append(pr)



def prepare(n, q, arr, prr):
    links = [[] for _ in range(n)]
    values = [0 for _ in range(n)]
    lineStates = [-1 for _ in range(n)]
    finalValues = [0 for _ in range(n)]
    for ar in arr:
        start = ar[0] - 1
        end = ar[1] - 1
        links[start].append(end)
        links[end].append(start)

    for pr in prr:
        i = pr[0] - 1
        x = pr[1]
        values[i] += x

    return links, values, lineStates, finalValues


links, values, lineStates, finalValues = prepare(N, Q, arr, prr)


def dfs(currentNode, arr, parrentAccumulate):
    childNodes = links[currentNode]
    parrentAccumulate += values[currentNode]
    finalValues[currentNode] = parrentAccumulate
    lineStates[currentNode] = 1
    for ch in childNodes:
        if lineStates[ch] == -1:
            dfs(ch, arr, parrentAccumulate)


dfs(0, links, 0)
print(" ".join([str(s) for s in finalValues]))