Problem p02936 (python)

Fixed submission:
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]
 
sys.setrecursionlimit(1000000)
mod = 1000000007
 
#A
def A():
    a = I()
    s = input()
    if a >= 3200:
        print(s)
    else:
        print("red")
    return
 
#B
def B():
    n = I()
    a = LI()
    ans = 0
    for i in a:
        ans += 1/i
    print(1/ans)
    return
 
#C
def C():
    n = I()
    v = LI()
    q = []
    for i in v:
        heappush(q,i)
    while q:
        x = heappop(q)
        if not q:
            print(x)
            return
        y = heappop(q)
        heappush(q,(x+y)/2)
    return
 
#D
def D():
    def dfs(x,d,s):
        for y in v[x]:
            if d[y]:
                s += f[y]
                ans[y] = s
                d[y] = 0
                dfs(y,d,s)
                s -= f[y]
    n,q = LI()
    v = [[] for i in range(n)]
    for i in range(n-1):
        a,b = LI()
        a -= 1
        b -= 1
        v[a].append(b)
        v[b].append(a)
    f = [0]*n
    for i in range(q):
        p,x = LI()
        p -= 1
        f[p] += x
    ans = [0]*n
    d = [1]*n
    d[0] = 0
    ans[0] = f[0]
    s = f[0]
    dfs(0,d,s)
    print(*ans)
    return
 
#E
def E():
    s = input()
    t = input()
 
    return
 
#F
def F():
 
    return
 
#Solve
if __name__ == "__main__":
    D()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
D - Ki
https://atcoder.jp/contests/abc138/tasks/abc138_d

"""
import sys


from functools import lru_cache
from collections import deque

def solve(N, Q):
    @lru_cache(maxsize=None)
    def calc_score(n):
        if n == 1 or nodes[n][3]:
            return nodes[n][0]
        res = calc_score(nodes[n][2]) + nodes[n][0]
        nodes[n][3] = True
        return res

    nodes = [[0, -1, -1, False] for _ in range(N+1)]
    edges = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[a].append(b)
        edges[b].append(a)
    for _ in range(Q):
        p, x = map(int, input().split())
        nodes[p][0] += x

    visited = set()
    q = deque([1])
    while q:
        n = q.popleft()
        if n not in visited:
            visited.add(n)
            for m in (e for e in edges[n] if e not in visited):
                q.appendleft(m)
                nodes[m][2] = n
    ans = [calc_score(i) for i in range(1, N+1)]
    return ans


def main(args):
    N, Q = map(int, input().split())
    ans = solve(N, Q)
    print(*ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
D - Ki
https://atcoder.jp/contests/abc138/tasks/abc138_d

"""
import sys


from functools import lru_cache
from collections import deque

def solve(N, Q):
    @lru_cache(maxsize=None)
    def calc_score(n):
        if n == 1 or nodes[n][3]:
            return nodes[n][0]
        res = calc_score(nodes[n][2]) + nodes[n][0]
        nodes[n][3] = True
        return res

    nodes = [[0, -1, -1, False] for _ in range(N+1)]
    edges = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[a].append(b)
    for _ in range(Q):
        p, x = map(int, input().split())
        nodes[p][0] += x

    visited = set()
    q = deque([1])
    while q:
        n = q.popleft()
        if n not in visited:
            visited.add(n)
            for m in edges[n]:
                q.appendleft(m)
                nodes[m][2] = n
    ans = [calc_score(i) for i in range(1, N+1)]
    return ans


def main(args):
    N, Q = map(int, input().split())
    ans = solve(N, Q)
    print(*ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""
D - Ki
https://atcoder.jp/contests/abc138/tasks/abc138_d

"""
import sys


from functools import lru_cache

def solve(N, Q):
    @lru_cache(maxsize=None)
    def calc_score(n):
        if n == 1 or nodes[n][3] == True:
            return nodes[n][0]
        res = calc_score(nodes[n][2]) + nodes[n][0]
        nodes[n][3] = True
        return res

    nodes = [[0, -1, -1, False] for _ in range(N+1)]

    for _ in range(N-1):
        a, b = map(int, input().split())
        nodes[b][2] = a
    for _ in range(Q):
        p, x = map(int, input().split())
        nodes[p][0] += x
    ans = [calc_score(i) for i in range(1, N+1)]
    return ans


def main(args):
    N, Q = map(int, input().split())
    ans = solve(N, Q)
    print(*ans)


if __name__ == '__main__':
    main(sys.argv[1:])