Problem p02936 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

 
N,Q = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
 
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
 
value = [0] * (N+1)
for p,x in PX:
    value[p] += x
 
def dfs(v,parent,add):
    value[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])
 
dfs(1,0,0)
 
answer = ' '.join(map(str,value[1:]))
print(answer)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline  # これを使わないとTLEしてしまう
sys.setrecursionlimit(10**7)

N, Q = map(int, input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

# 木構造をリストで表現する
graph = [[] for _ in range(N+1)]

for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)

# 各操作で与えられる値を、各頂点ごとに集計しておく
P = [0] * (N+1)
for p, x in PX:
    P[p] += x

Count = [0] * (N+1)


def dfs(v, parent, point):
    # 現在いる頂点vの親までの値をpointとする
    Count[v] = point + p[v]
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x, v, Count[v])


dfs(1, 0, 0)

res = ' '.join(map(str, Count[1:]))
print(res)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)

N, Q = map(int, input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

# 木構造をリストで表現する
graph = [[] for _ in range(N+1)]

for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)

# 各操作で与えられる値を、各頂点ごとに集計しておく
value = [0] * (N+1)
for p, x in PX:
    value[p] += x


def dfs(v, parent, point):
    # 現在いる頂点vの親までの値をpointとする
    value[v] += point
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x, v, value[v])


dfs(1, 0, 0)

res = ' '.join(map(str, P[1:]))
print(res)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)

N, Q = map(int, input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

# 木構造をリストで表現する
graph = [[] for _ in range(N+1)]

for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)

# 各操作で与えられる値を、各頂点ごとに集計しておく
value = [0] * (N+1)
for p, x in PX:
    value[p] += x


def dfs(v, parent, point):
    # 現在いる頂点vの親までの値をpointとする
    value[v] += point
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x, v, value[v])


dfs(1, 0, 0)

res = ' '.join(map(str, P[1:]))
print(res)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)

N, Q = map(int, input().split())

# 木構造をリストで表現する
G = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a = a - 1
    b = b - 1
    G[a].append(b)

# 各操作で与えられる値を、各頂点ごとに集計しておく
P = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    p = p - 1
    P[p] += x

# 各頂点のカウンター
Count = [0] * N


def dfs(v, point):
    # 現在いる頂点vの親までの値をpointとし、
    new_point = point + P[v]
    Count[v] = str(new_point)

    for next_v in G[v]:
        dfs(next_v, new_point)


dfs(0, 0)

res = " ".join(Count)

print(res)

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

# 木構造をリストで表現する
G = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a = a - 1
    b = b - 1
    G[a].append(b)

# 各操作で与えられる値を、各頂点ごとに集計しておく
P = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    p = p - 1
    P[p] += x

# 各頂点のカウンター
Count = [0] * N


def dfs(v, point):
    # 現在いる頂点vの親までの値をpointとし、
    new_point = point + P[v]
    Count[v] = str(new_point)

    for next_v in G[v]:
        dfs(next_v, new_point)


dfs(0, 0)

res = " ".join(Count)

print(res)

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


class Tree(object):
    def __init__(self, num):
        self.num = num
        self.childs = []
        self.count = 0


def dfs(p, x):
    """
    add x to all elements which have p as parent
    :param p: root of subtree, Tree class
    :param x: numbers to add, int
    """
    # add x to p.count
    p.count += x

    # recurrsive call for direct children of p
    for child in p.childs:
        dfs(child, x)


trees = [Tree(num) for num in range(N)]

for _ in range(N-1):
    a, b = map(int, input().split())
    a = a - 1
    b = b - 1
    trees[a].childs.append(trees[b])


for _ in range(Q):
    p, x = map(int, input().split())
    p = p - 1
    dfs(trees[p], x)


res = ""
for n, tree in enumerate(trees):
    if n != len(trees):
        res += str(tree.count) + " "
    else:
        res += str(tree.count)

print(res)

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


class Tree(object):
    def __init__(self, num):
        self.num = num
        self.childs = []
        self.count = 0


def dfs(p, x):
    """
    add x to all elements which have p as parent
    :param p: root of subtree, Tree class
    :param x: numbers to add, int
    """
    # add x to p.count
    p.count += x

    # recurrsive call for direct children of p
    for child in p.childs:
        dfs(child, x)


trees = [Tree(num) for num in range(N)]

for _ in range(N-1):
    a, b = map(int, input().split())
    a = a - 1
    b = b - 1
    trees[a].childs.append(trees[b])


for _ in range(Q):
    p, x = map(int, input().split())
    p = p - 1
    dfs(trees[p], x)


for tree in trees:
    print(tree.count, end="")

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


class Tree(object):
    def __init__(self, num):
        self.num = num
        self.childs = []
        self.count = 0


def dfs(p, x):
    """
    add x to all elements which have p as parent
    :param p: root of subtree, Tree class
    :param x: numbers to add, int
    """
    # add x to p.count
    p.count += x

    # recurrsive call for direct children of p
    for child in p.childs:
        dfs(child, x)


trees = [Tree(num) for num in range(N)]
print(trees)

for _ in range(N-1):
    a, b = map(int, input().split())
    a = a - 1
    b = b - 1
    trees[a].childs.append(trees[b])


for _ in range(Q):
    p, x = map(int, input().split())
    p = p - 1
    dfs(trees[p], x)


for tree in trees:
    print(tree.count)