Problem p02936 (python)

Fixed submission:
from collections import deque
 
if __name__ == "__main__":
    N, q = map(int, input().split())
    links = [[] for _ in range(N)]
    
    for i in range(N - 1):
        a, b = map(int, input().split())
        links[a - 1].append(b - 1)
        links[b - 1].append(a - 1)
    
    ans = [0] * N
    for i in range(q):
        p, x = map(int, input().split())
        ans[p - 1] += x

    deq = deque([0])
    visited = [False] * N
    while deq:
        parent = deq.popleft()
        link = links[parent]
        for v in link:
            if not visited[v]:
                ans[v] += ans[parent]
                deq.append(v)
        visited[parent] = True
    
    print(*ans)

Buggy submission (verdict: Runtime Error):
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))

        if A not in tree:
            tree[A] = []

        if B not in tree:
            tree[B] = [A]
        else:
            tree[B].append(A)

    point = [0] * (N + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] += B
        
    pointList = [0] * (N + 1)
    for idx, val in enumerate(tree):
        for v in tree[val]:
            pointList[val] += pointList[v]
        pointList[val] += point[val]
            
    print(*pointList[1:])

Buggy submission (verdict: Runtime Error):
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = point[index] + prev
    pointTree.append(sumPoint)

    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)    
    return pointTree

if __name__ == "__main__":
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]

        if B not in tree:
            tree[B] = []

    point = [0] * (int(list(tree.keys())[len(tree)-1]) + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] = point[A] + B

    firstKey = list(tree.keys())[0]
    pointTree = sumTree(firstKey, tree[firstKey], 0)
    print(*pointTree)

Buggy submission (verdict: Runtime Error):
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = point[index] + prev
    pointTree.append(sumPoint)

    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)    
    return pointTree

if __name__ == "__main__":
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]

        if B not in tree:
            tree[B] = []

    point = [0] * (N + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] = point[A] + B

    firstKey = list(tree.keys())[0]
    pointTree = sumTree(firstKey, tree[firstKey], 0)
    print(*pointTree)

Buggy submission (verdict: Runtime Error):
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = point[index] + prev
    pointTree.append(sumPoint)

    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)    
    return pointTree

if __name__ == "__main__":
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]

        if B not in tree:
            tree[B] = []

    point = [0] * (N + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] = point[A] + B

    firstKey = list(tree.keys())[0]
    pointTree = sumTree(firstKey, tree[firstKey], 0)
    print(' '.join(map(str, pointTree)))

Buggy submission (verdict: Runtime Error):
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = (point[index] if index in point else 0) + prev
    pointTree.append(sumPoint)

    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)    
    return pointTree

if __name__ == "__main__":
    tree = {}
    point = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]

        if B not in tree:
            tree[B] = []

    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        if A in point:
            point[A] += B
        else:
            point[A] = B

    firstKey = list(tree.keys())[0]
    pointTree = sumTree(firstKey, tree[firstKey], 0)
    print(' '.join(map(str, pointTree)))

Buggy submission (verdict: Runtime Error):
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = (point[index] if index in point else 0) + prev
    pointTree.append(sumPoint)
    
    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)
    return pointTree

if __name__ == "__main__":
    tree = {}
    point = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]
        if B not in tree:
            tree[B] = []

    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        if A in point:
            point[A] += B
        else:
            point[A] = B

    pointTree = sumTree(1, tree[1], 0)
    print(' '.join(map(str, pointTree)))

Buggy submission (verdict: Runtime Error):
pointTree = []

def sumTree(index, children, prev):
    sumPoint = (point[index] if index in point else 0) + prev
    pointTree.append(sumPoint)
    
    for key in children:
        sumTree(key, tree[key], sumPoint)    

if __name__ == "__main__":
    tree = {}
    point = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]
        if B not in tree:
            tree[B] = []

    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        if A in point:
            point[A] += B
        else:
            point[A] = B

    sumTree(1, tree[1], 0)
    print(' '.join(map(str, pointTree)))