Problem p02936 (python)

Fixed submission:
from sys import stdin, setrecursionlimit

setrecursionlimit(1000000)


def main():
    def read_tree(n):
        parents = [0 for _ in range(n + 1)]
        for _ in range(n - 1):
            a, b = [int(x) for x in input().split()]
            parents[b] = a
        return parents

    n, q = [int(x) for x in input().split()]

    parents = read_tree(n)
    cnt = [0] * (n + 1)

    for _ in range(q):
        p, x = [int(x) for x in input().split()]
        cnt[p] += x
    
    for i in range(1, n + 1):
        cnt[i] += cnt[parents[i]]

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


main()

Buggy submission (verdict: Runtime Error):
import sys


def main():
    
    def walk(p, c):
        if visited[p]:
            return
        visited[p] = True
        cnt[p] += c
        for n in tree[p]:
            walk(n, cnt[p])

    sys.setrecursionlimit(200000)
    rl = sys.stdin.readline

    n, q = [int(x) for x in rl().split()]

    tree = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        a, b = [int(x) for x in rl().split()]
        tree[a].append(b)
        tree[b].append(a)

    cnt = [0] * (n + 1)
    for _ in range(q):
        p, x = [int(x) for x in rl().split()]
        cnt[p] += x

    visited = [False] * (n + 1)
    walk(1, 0)

    print(*cnt[1:])


main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(200000)


def main():
    def walk(p, c):
        if visited[p]:
            return
        visited[p] = True
        cnt[p] += c
        for n in tree[p]:
            walk(n, cnt[p])

    n, q = [int(x) for x in sys.stdin.readline().split()]

    tree = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        tree[a].append(b)
        tree[b].append(a)

    cnt = [0] * (n + 1)
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        cnt[p] += x

    visited = [False] * (n + 1)
    walk(1, 0)

    print(*cnt[1:])


main()

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

setrecursionlimit(200000)


def main():
    def walk(p, c):
        if visited[p]:
            return
        visited[p] = True
        cnt[p] += c
        for n in tree[p]:
            walk(n, cnt[p])

    n, q = [int(x) for x in stdin.readline().split()]

    tree = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        a, b = [int(x) for x in stdin.readline().split()]
        tree[a].append(b)
        tree[b].append(a)

    cnt = [0] * (n + 1)
    for _ in range(q):
        p, x = [int(x) for x in stdin.readline().split()]
        cnt[p] += x

    visited = [False] * (n + 1)
    walk(1, 0)

    print(*cnt[1:])


main()

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

setrecursionlimit(200000)


def main():
    def read_tree(n):
        tree = [[] for _ in range(n + 1)]
        for _ in range(n - 1):
            a, b = [int(x) for x in stdin.readline().split()]
            tree[a].append(b)
        return tree

    def walk(p, x):
        cnt[p] += x
        for n in tree[p]:
            walk(n, x)

    n, q = [int(x) for x in input().split()]
    
    tree = read_tree(n)
    cnt = [0] * (n + 1)

    for _ in range(q):
        p, x = [int(x) for x in stdin.readline().split()]
        walk(p, x)

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


main()

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

setrecursionlimit(200000)

n, q = [int(x) for x in input().split()]

tree = [[] for _ in range(n + 1)]
cnt = [0] * (n + 1)

for _ in range(n - 1):
    a, b = [int(x) for x in stdin.readline().split()]
    tree[a].append(b)

# TLE/RE

def walk(p, x):
    cnt[p] += x
    for n in tree[p]:
        walk(n, x)


for _ in range(q):
    p, x = [int(x) for x in stdin.readline().split()]
    walk(p, x)

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

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

setrecursionlimit(20000)

n, q = [int(x) for x in input().split()]

tree = [[] for _ in range(n + 1)]
cnt = [0] * (n + 1)

for _ in range(n - 1):
    a, b = [int(x) for x in stdin.readline().split()]
    tree[a].append(b)

def walk(p, x):
    cnt[p] += x
    for n in tree[p]:
        walk(n, x)

for _ in range(q):
    p, x = [int(x) for x in stdin.readline().split()]
    walk(p, x)

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

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(20000)

n, q = [int(x) for x in input().split()]

tree = [[] for _ in range(n + 1)]
cnt = [0] * (n + 1)

for _ in range(n - 1):
    a, b = [int(x) for x in input().split()]
    tree[a].append(b)

def walk(p, x):
    cnt[p] += x
    for n in tree[p]:
        walk(n, x)

for _ in range(q):
    p, x = [int(x) for x in input().split()]
    walk(p, x)

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

Buggy submission (verdict: Runtime Error):
n, q = [int(x) for x in input().split()]

tree = [[] for _ in range(n + 1)]
cnt = [0] * (n + 1)

for _ in range(n - 1):
    a, b = [int(x) for x in input().split()]
    tree[a].append(b)

def walk(p, x):
    cnt[p] += x
    for n in tree[p]:
        walk(n, x)

for _ in range(q):
    p, x = [int(x) for x in input().split()]
    walk(p, x)

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

Buggy submission (verdict: Runtime Error):
n, q = [int(x) for x in input().split()]

tree = [[] for _ in range(n + 1)]
cnt = [0] * (n + 1)

for _ in range(n - 1):
    a, b = [int(x) for x in input().split()]
    tree[a].append(b)

def walk(p, x):
    cnt[p] += x
    for n in tree[p]:
        walk(n, x)

for _ in range(q):
    p, x = [int(x) for x in input().split()]
    walk(p, x)

print(*cnt[1:])