Problem:


Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20



Sample input:
4 3
1 2
2 3
2 4
2 10
1 100
3 1


Sample output:
100 110 111 110

Submission status: Runtime Error

Solution code:
tree = [[] for _ in range(200001)]
res = [0]*200001
 
def dfs(p, pre):
    for child in tree[p]:
        if child == pre: continue
        res[child] += res[p]
        dfs(child, p)
 
def main():
    """ main """
    N, Q = map(int, input().split())
    for _ in range(N-1):
        a,b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    for _ in range(Q):
        p,x = map(int, input().split())
        res[p] += x
    dfs(1, 0)
 
    print(*res[1:N+1])
 
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
tree = [[] for i in range(200001)]
scores = [0 for i in range(200001)]

def dfs(pre, p):
    for child in tree[p]:
        if child == pre: continue
        scores[child] += scores[p]
        dfs(p, child)

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

    for _ in range(Q):
        p, x = map(int, input().split())
        scores[p] += x
    dfs(0, 1)
    print(' '.join(map(str, scores[1:N+1])))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
tree = [[] for i in range(N+1)]
scores = [0 for i in range(N+1)]

def dfs(pre, p):
    for child in tree[p]:
        if child == pre: continue
        scores[child] += scores[p]
        dfs(p, child)

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

    for _ in range(Q):
        p, x = map(int, input().split())
        scores[p] += x
    dfs(0, 1)
    print(' '.join(map(str, scores[1:])))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n,q = map(int,input().split())
a_b_list = [list(map(int,input().split())) for i in range(n-1)]
p_x_list = [list(map(int,input().split())) for i in range(q)]

graph_direct_list = [[] for _ in range(n+1)]
for i in range(n-1):
    a_i = a_b_list[i][0]
    b_i = a_b_list[i][1]
    graph_direct_list[a_i].append(b_i)
    graph_direct_list[b_i].append(a_i)

counter_init_list = [0 for _ in range(n+1)]


for j in range(q):
    p_j = p_x_list[j][0]
    x_j = p_x_list[j][1]
    counter_init_list[p_j] += x_j

counter_list = [0 for _ in range(n+1)]
graph_vertex_checked_list = [0 for _ in range(n+1)]


graph_vertex_stack_que=collections.deque()
graph_vertex_stack_que.append(1) # start
while graph_vertex_stack_que:
    v = graph_vertex_stack_que.pop()
    graph_vertex_checked_list[v] = 1
    counter_list[v] += counter_init_list[v]
    for u in graph_direct_list[v]:
        if graph_vertex_checked_list[u] == 1:
            continue
        counter_list[u] += counter_list[v]
        graph_vertex_stack_que.append(u)
print(*counter_list[1:])

Submission status: Runtime Error

Solution code:
def Operation(V_id, x):
  global V_value
  global V
  global Q_O
  V_value[V_id] = x
  if V[V_id] == [] :
    return 0
  for i in V[V_id]:
    Operation(i, V_value[V_id]+Q_O[i])
  return 0

N, Q = map(int,input().split())
V = [ [] for i in range(N)]
V_value = [ 0 for i in range(N)]
Q_O = [ 0 for i in range(N)]
for i in range(N-1):
  a, b = map(int,input().split())
  V[a-1].append(b-1)

for i in range(Q):
  V_id, x = map(int,input().split())
  Q_O[V_id-1] += x

Operation(0, Q_O[0])

s = ''
for i in range(N-1):
  s += str(V_value[i]) + ' '
s += str(V_value[N-1])
print(s)

Submission status: Runtime Error

Solution code:
def Operation(V, V_id, V_value, x, Q_O):
  V_value[V_id] = x
  if V[V_id] == [] :
    return 0
  for i in V[V_id]:
    Operation(V, i, V_value, V_value[V_id]+Q_O[i], Q_O)
  return 0

N, Q = map(int,input().split())
V = [ [] for i in range(N)]
V_value = [ 0 for i in range(N)]
Q_O = [ 0 for i in range(N)]
for i in range(N-1):
  a, b = map(int,input().split())
  V[a-1].append(b-1)

for i in range(Q):
  V_id, x = map(int,input().split())
  Q_O[V_id-1] += x

Operation(V, 0, V_value, Q_O[0], Q_O)

s = ''
for i in range(N-1):
  s += str(V_value[i]) + ' '
s += str(V_value[N-1])
print(s)

Submission status: Runtime Error

Solution code:
def Operation(V, V_id, V_value, x):
  V_value[V_id] += x
  if V[V_id] == [] :
    return 0
  for i in V[V_id]:
    Operation(V, i, V_value, x)
  return 0

N, Q = map(int,input().split())
V = [ [] for i in range(N)]
V_value = [ 0 for i in range(N)]
Q_O = [ 0 for i in range(N)]
for i in range(N-1):
  a, b = map(int,input().split())
  V[a-1].append(b-1)

for i in range(Q):
  V_id, x = map(int,input().split())
  Q_O[V_id-1] += x
for i in range(N):
  if Q_O[i] != 0 :
    Operation(V, i, V_value, Q_O[i])

s = ''
for i in range(N-1):
  s += str(V_value[i]) + ' '
s += str(V_value[N-1])
print(s)

Submission status: Runtime Error

Solution code:
def Operation(V, V_id, V_value, x):
  V_value[V_id] += x
  if V[V_id] == [] :
    return 0
  for i in V[V_id]:
    Operation(V, i, V_value, x)
  return 0

N, Q = map(int,input().split())
V = [ [] for i in range(N)]
V_value = [ 0 for i in range(N)]
for i in range(N-1):
  a, b = map(int,input().split())
  V[a-1].append(b-1)

for i in range(Q):
  V_id, x = map(int,input().split())
  Operation(V, V_id-1, V_value, x)

s = ''
for i in range(N-1):
  s += str(V_value[i]) + ' '
s += str(V_value[N-1])
print(s)

Submission status: Runtime Error

Solution code:
def Operation(V, V_id, V_value, x):
  V_value[V_id] += x
  if V[V_id] == [] :
    return 0
  for i in V[V_id]:
    Operation(V, i, V_value, x)
  return 0

N, Q = map(int,input().split())
V = [ [] for i in range(N)]
V_value = [ 0 for i in range(N)]
for i in range(N-1):
  a, b = map(int,input().split())
  V[a-1].append(b-1)

for i in range(Q):
  V_id, x = map(int,input().split())
  Operation(V, V_id-1, V_value, x)

print(V_value)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(int(1e5))

N, Q = map(int, input().split())
node = [list() for i in range(N+1)]
count = [0] * len(node)

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

for i in range(Q):
    p, x = map(int, input().split())
    count[p] += x

def add_tree(x, i):
    global node, count
    count[i] += x
    x = count[i]
    for j in node[i]:
        add_tree(x, j)

add_tree(0, 1)

#print(N, Q)
#print(node)
#print(count)
print(*count[1:])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(int(1e5))

N, Q = map(int, input().split())
node = [list() for i in range(N+1)]
count = [0] * len(node)

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

def add_tree(x, i):
    global node, count
    count[i] += x
    for j in node[i]:
        add_tree(x, j)

for i in range(Q):
    p, x = map(int, input().split())
    add_tree(x, p)

#print(N, Q)
#print(node)
#print(count)
print(*count[1:])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(int(1e5))

N, Q = map(int, input().split())
node = [list() for i in range(N+1)]
count = [0] * len(node)

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

def add_tree(x, i):
    global node, count
    count[i] += x
    for j in node[i]:
        add_tree(x, j)

for i in range(Q):
    p, x = map(int, input().split())
    add_tree(x, p)

#print(N, Q)
#print(node)
#print(count)
print(*count)

Submission status: Runtime Error

Solution code:
#これは行列を使わないと無理では
import numpy as np
n,q = map(int, input().split())
cond = []
point = np.zeros(n)
roots = np.zeros(n**2).reshape(n,n)
roots[0][0] += 1

for i in range(n-1):
  a,b = map(int, input().split())
  cond.append([a,b])
cond.sort
a = [cond[i][0] for i in range(n-1)]
b = [cond[i][1] for i in range(n-1)]
  
for i in range(q):
  p,x = map(int, input().split())
  point[p-1] += x

for i in range(n):
  if i>=1:
    idx = b.index(i+1)
    root = a[idx]
    roots[i] += roots[root-1]
    roots[i][i] += 1

roo = roots.T
ans = np.dot(point, roo)
ans_list = ans.tolist()

for i in range(n):
  print(int(ans_list[i]), end=" ")

Submission status: Runtime Error

Solution code:
N, Q =  map(int,input().split())
graph = [[] for _ in range(N+1)]
nodes = [0 for _ in range(N+1)]

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

for _ in range(Q):
  p,x = map(int, input().split())
  nodes[p] += x

def add_node(node,cnt):
  nodes[node] += cnt
  for child in graph[node]:
    add_node(child, nodes[node])
  return None

add_node(1,0)

print(*nodes[1:])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int, input().split())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
# px = [ list(map(int,input().split())) for _ in range(q) ]
c = [0]*n
# for i in range(len(px)):
for i range(q):
    p,x = map(int,input().split())
    c[p-1] += x  
visited = [0]*n
visited[0] = 1
while sum(visited)!=n:
    for i in range(1,n):
        if visited[i]==0:
            for j in range(len(tree[i])):
                node_tmp = tree[i][j]
                if visited[node_tmp-1]==1:
                    c[i] += c[node_tmp-1]
                    visited[i] = 1
print(" ".join(map(str,c)))

Submission status: Runtime Error

Solution code:
def bfs(node, score):
  counts[node] += score
  for child in neighb[node]:
    bfs(child, score)

n,q    = [int(i) for i in input().split()]
counts = [0 for i in range(n)]
neighb = [[] for i in range(n)]
  
for j in range(n-1):
  a,b = [int(i) for i in input().split()]
  neighb[a-1].append(b-1)
  
for j in range(q):
  node,score = [int(i) for i in input().split()]
  bfs(node-1, score)
  
for i in range(n):
  print(counts[i], end=" ")

Submission status: Runtime Error

Solution code:
def bfs(node, score):
  counts[node] += score
  for child in neighb[node]:
    bfs(child, score)

n,q    = [int(i) for i in input().split()]
counts = [0 for i in range(n)]
neighb = [[] for i in range(n)]
  
for j in range(n-1):
  a,b = [int(i) for i in input().split()]
  neighb[a-1].insert(b-1)
  
for j in range(q):
  node,score = [int(i) for i in input().split()]
  bfs(node-1, score)
  
for i in range(n):
  print(counts[i], end=" ")

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int, input().split())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
px = [ list(map(int,input().split())) for _ in range(q) ]
# print(tree)
# print(px)
c = [0]*n
for i in range(len(px)):
    c[px[i][0]-1] += px[i][1]
# print(c)
# for i in range(1,n):
#     c[i] += c[min(tree[i])-1]
visited = [0]*n
visited[0] = 1
while sum(visited)=n:
    for i in range(1,n):
        for j in range(len(tree[i])):
            node_tmp = tree[i][j]
            if visited[node_tmp-1]==1 and visited[i]==0:
                c[i] += c[node_tmp-1]
                visited[i] = 1
            else:
                pass
print(" ".join(map(str,c)))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int, input().split())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
px = [ list(map(int,input().split())) for _ in range(q) ]
# print(tree)
# print(px)
c = [0]*n
for i in range(len(px)):
    c[px[i][0]-1] += px[i][1]
# print(c)
# for i in range(1,n):
#     c[i] += c[min(tree[i])-1]
visited = [0]*n
visited[0] = 1
check = sum(visited)
while check!=n:
    for i in range(1,n):
        for j in range(len(tree[i])):
            node_tmp = tree[i][j]
            if visited[node_tmp-1]==1 and visited[i]==0:
                c[i] += c[node_tmp-1]
                visited[i] = 1
            else:
                pass
    check = sume(visited)
print(" ".join(map(str,c)))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int, input().split())
# print(n,q)
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
px = [ list(map(int,input().split())) for _ in range(q) ]

print(tree)
print(px)

c = [0]*n

for i in range(len(px)):
    c[px[i][0]-1] += px[i][1]

print(c)

# for i in range(1,n):
#     c[i] += c[min(tree[i])-1]

visited = [False]*n
visited[0] = True
# print(visited)
check = visited.count(False)
# print(check)
while check!=0:
    for i in range(1,n):
        # mi = min(tree[i])-1
        for j in range(len(tree[i])):
            node_tmp = tree[i][j]
            if visited[node_tmp-1]:
                c[i] += c[node_tmp-1]
                visited[i] = True
                # print("OK")
            else:
                # print("NG")
    check = visited.count(False)

print(" ".join(map(str,c)))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)
n, q = map(int, input().split())

trees = [[] for i in range(n)]
for i in range(n-1):
  a, b = map(int, input().split())
  trees[a-1].append(b-1)
  
values = [0 for i in range(n)]

for i in range(q):
  p, x = map(int, input().split())
  values[p-1] += x
  
def dfs(i):
  for j in trees[i]:
    values[j] += values[i]
    dfs(j)
    
dfs(0)
print(*values)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(5000)

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

ki = [[] for _ in range(200005)]
result = [0] * N

for i in range(N - 1):
    p,c = map(int,input().split())
    ki[p - 1].append(c - 1)
    #ki[c - 1].append(p - 1)

for i in range(Q):
    n,x = map(int,input().split())
    result[n - 1] += x

def dfs(start, parent):
    for child in ki[start]:
        # if child == parent:
        #     continue

        result[child] += result[start]
        dfs(child, start)

dfs(0, -1)
print(" ".join(map(str, result)))

Submission status: Runtime Error

Solution code:
N, Q = map(int,input().split())

ki = [[] for _ in range(200005)]
result = [0] * N

for i in range(N - 1):
    p,c = map(int,input().split())
    ki[p - 1].append(c - 1)
    #ki[c - 1].append(p - 1)

for i in range(Q):
    n,x = map(int,input().split())
    result[n - 1] += x

def dfs(start, parent):
    for child in ki[start]:
        # if child == parent:
        #     continue

        result[child] += result[start]
        dfs(child, start)

dfs(0, -1)
print(" ".join(map(str, result)))

Submission status: Runtime Error

Solution code:
class node:
    def __init__(self):
        self.point = 0
        self.child_id = []

    def append_child(self, id):
        self.child_id.append(id)

    def get_child(self):
        return self.child_id

    def add_point(self, point):
        self.point += point

    def get_point(self):
        return self.point

class tree:
    def __init__(self,n, ps):
        self.nodes = [node() for _ in range(n+1)]

        for p in ps:
            self.set_childnode(p[0], p [1])
       
    def set_childnode(self, id, child_id):
        self.nodes[id].append_child(child_id)

    def add_point(self, id, point):
        self.target_ids = [id]

        self.get_target_nodes(id)

        for i in self.target_ids:
            self.nodes[i].add_point(point)

    def get_target_nodes(self, id):
        child_nodes = self.nodes[id].get_child()
        for child_node in child_nodes:
            self.target_ids.append(child_node)
            self.get_target_nodes(child_node)

    def print_points(self):
        points = []
        for i in range(1, len(self.nodes)):
            points.append(self.nodes[i].get_point())
        print(*points)

def main(): 
    n,q = map(int, input().split())

    items = [list(map(int, input().split())) for _ in range(1, n)]

    op = [list(map(int, input().split())) for _ in range(q)]

    trees = tree(n, items)

    for o in op:
        trees.add_point(o[0], o[1])
   
    trees.print_points()

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())

at = [[] for _ in range(n)]
bt = [[] for _ in range(n)]
ans = [0]*n
i = 0
ii = 0

while i < n-1:
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    #print(at)
    #print(a)
    #print(b)
    at[a].append(b)
    bt[b].append(a)
    i += 1
    #print(i)

while ii < q:
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
    ii += 1
    #print(ans)
    #print(ii)

def dfs(i):
    #print(at)
    if at[i]:
        for y in at[i]:
            #print(ans)
            ans[y] += ans[i]
            dfs(y)

dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
import sys

n, q = map(int, sys.stdin.readline().split())
g = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, sys.stdin.readline().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)
xs = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, sys.stdin.readline().split())
    p -= 1
    xs[p] += x

costs = [0 for i in range(n)]
visited = [False for _ in range(n)]


def rec(prev_v, v, value):
    if visited[v]:
        return
    visited[v] = True
    next_value = value + xs[v]
    costs[v] = next_value
    for next_v in g[v]:
        if next_v == prev_v:
            continue
        rec(v, next_v, next_value)


rec(-1, 0, 0)
print(' '.join(map(str, costs)))

Submission status: Runtime Error

Solution code:
import sys

n, q = map(int, sys.stdin)
g = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, sys.stdin)
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)
xs = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, sys.stdin)
    p -= 1
    xs[p] += x

costs = [0 for i in range(n)]
visited = [False for _ in range(n)]


def rec(prev_v, v, value):
    if visited[v]:
        return
    visited[v] = True
    next_value = value + xs[v]
    costs[v] = next_value
    for next_v in g[v]:
        if next_v == prev_v:
            continue
        rec(v, next_v, next_value)


rec(-1, 0, 0)
print(' '.join(map(str, costs)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)
xs = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    xs[p] += x

costs = [0 for i in range(n)]
visited = [False for _ in range(n)]


def rec(prev_v, v, value):
    if visited[v]:
        return
    visited[v] = True
    next_value = value + xs[v]
    costs[v] = next_value
    for next_v in g[v]:
        if next_v == prev_v:
            continue
        rec(v, next_v, next_value)


rec(-1, 0, 0)
print(' '.join(map(str, costs)))

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(30000)

n, q = map(int, input().split())
g = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)
xs = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    xs[p] += x

costs = [0 for i in range(n)]
visited = [False for _ in range(n)]


def rec(prev_v, v, value):
    if visited[v]:
        return
    visited[v] = True
    next_value = value + xs[v]
    costs[v] = next_value
    for next_v in g[v]:
        if next_v == prev_v:
            continue
        rec(v, next_v, next_value)


rec(-1, 0, 0)
print(' '.join(map(str, costs)))

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(30000)

n, q = map(int, input().split())
g = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)
xs = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    xs[p] += x

costs = [0 for i in range(n)]


def rec(prev_v, v, value):
    next_value = value + xs[v]
    costs[v] = next_value
    for next_v in g[v]:
        if next_v == prev_v:
            continue
        rec(v, next_v, next_value)


rec(-1, 0, 0)
print(' '.join(map(str, costs)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)
xs = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    xs[p] += x

costs = [0 for i in range(n)]


def rec(prev_v, v, value):
    next_value = value + xs[v]
    costs[v] = next_value
    for next_v in g[v]:
        if next_v == prev_v:
            continue
        rec(v, next_v, next_value)


rec(-1, 0, 0)
print(' '.join(map(str, costs)))

Submission status: Runtime Error

Solution code:
import numpy as np

N, Q=map(int, input().split())
matrix=np.zeros((N, N))
point=np.zeros(N)

for i in range(N-1):
  a, b=map(int, input().split())
  matrix[int(a-1), int(b-1)]=1
  
for i in range(Q):
  p, x=map(int, input().split())
  point[int(p-1)]+=x
  
for i in range(N-1):
  point+=matrix[int(i), :]*point[int(i)]
  
print(' '.join(map(str, map(int, point.tolist()))))

Submission status: Runtime Error

Solution code:
import numpy as np

N, Q=map(int, input().split())
matrix=np.zeros((N, N))
point=np.zeros(N)

for i in range(N-1):
  a, b=map(int, input().split())
  matrix[a-1, b-1]=1
  
for i in range(Q):
  p, x=map(int, input().split())
  point[p-1]+=x
  
for i in range(N-1):
  point+=matrix[i, :]*point[i]
  
print(' '.join(map(str, map(int, point.tolist()))))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

class Node():
  def __init__(self):
    self.score = 0
    self.child = set()
  def add_score(self, point):
    self.score += point
    for i in self.child:
      nodes[i].add_score(point)

num_nodes, num_actions = [int(i) for i in input().split()]
nodes = []

for i in range(num_nodes):
  nodes.append(Node())

for i in range(num_nodes-1):
  tmp_str = input()
  parent_id, child_id = [int(val)-1 for val in tmp_str.split()]
  nodes[parent_id].child.add(child_id)

for i in range(num_actions):
  tmp_str = input()
  target_id, point = [int(val) for val in tmp_str.split()]
  nodes[target_id-1].add_score(point)

point_list = [node.score for node in nodes]
print(' '.join([str(i) for i in point_list]))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

class Node():
  def __init__(self):
    self.score = 0
    self.child = set()
  def getChildren(self):
    rtn = self.child
    for i in self.child:
      rtn = rtn | nodes[i].getChildren()
    return rtn

num_nodes, num_actions = [int(i) for i in input().split()]
nodes = []

for i in range(num_nodes):
  nodes.append(Node())

for i in range(num_nodes-1):
  tmp_str = input()
  parent_id, child_id = [int(val)-1 for val in tmp_str.split()]
  nodes[parent_id].child.add(child_id)

for i in range(num_actions):
  tmp_str = input()
  target_id, point = [int(val) for val in tmp_str.split()]
  nodes[target_id-1].score += point
  for c in nodes[target_id-1].getChildren():
    nodes[c].score += point

point_list = [node.score for node in nodes]
print(' '.join([str(i) for i in point_list]))

Submission status: Runtime Error

Solution code:
import sys

def search(a,b,c,no,num):
	c[no-1] += num
	for k in range(len(a)):
		if a[k] == no:
			search(a,b,c,b[k],num)

n,q = map(int,input().split())
a = []
b = []
c = [0,0,0,0]

for i in range(n-1):
	a_dash, b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)
	#c.append(0)

for i in range(q):
	no,num = map(int,input().split())
	search(a,b,c,no,num)

for i in range(len(c)):
	sys.stdout.write(str(c[i]))
	sys.stdout.write(" ")

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
table = [[] for i in range(N)]
for i in range(N-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  table[a].append(b)
  table[b].append(a)
task = [0]*N
point = [-1]*N
point[0] = 0
for i in range(Q):
  p,x = map(int, input().split())
  p -= 1
  task[p] += x
H = deque()
H.append(0)

while H:
  p = H.popleft()
  point[p] += task[p]
  for np in table[p]:
    if point[np] == -1:
      H.append(np)
      point[np] = point[p]

print(" ".join(map(str, point[i])))

Submission status: Runtime Error

Solution code:
class Tree():
  def __init__(self,id,depth = None):
    self.count = 0
    self.link = []
    self.id = id
    self.depth = None
    if(id == 1):
      self.depth = 0
      
  def add(self,x):
    self.count += x
    for l in self.link:
      if(self.depth < G[l-1].depth): 
        G[l-1].add(x)
        
  def detdepth(self):
    if((len(self.link) == 1) and (self.id != 1)):
      self.depth = G[self.link[0]-1].depth + 1
    else:
      for l in self.link:
        if(G[l-1].depth == None):
          G[l-1].detdepth()
        elif(self.depth == None):
          self.depth = G[l-1].depth + 1

N, Q = [int(x) for x in input().split()]
G = [Tree(i) for i in range(1,N+1)]

for i in range(N-1):
  a,b = [int(x) for x in input().split()]
  G[a-1].link.append(b)
  G[b-1].link.append(a)
  
G[0].detdepth()

for i in range(Q):
  p,x =[int(x) for x in input().split()]
  G[p-1].add(x)

for i in range(N):
  if (i < N-1):  	
    print(G[i].count,end = " ")
  else:
    print(G[i].count)

Submission status: Runtime Error

Solution code:


N,Q = map(int,input().split())
aLst = [[int(i) for i in input().split()] for _ in range(N-1)]
pLst = [[int(i) for i in input().split()] for _ in range(Q)]
to = [[]for _ in range(N+1)]

for a,b in aLst:
    to[a].append(b)
    to[b].append(a)

ps = [0]*(N+1)
for a,b in pLst:
    ps[a] += b

def dfs(v, i):
    for j in to[v]:
        if j == i:
            continue
        ps[j] += ps[v]
        dfs(j,v)

dfs(1,0)
print(' '.join(map(str, ps[1:])))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

N,Q = map(int,input().split())
aLst = [[int(i) for i in input().split()] for _ in range(N-1)]
pLst = [[int(i) for i in input().split()] for _ in range(Q)]
to = [[]for _ in range(N+1)]

for a,b in aLst:
    to[a].append(b)
    to[b].append(a)

ps = [0]*(N+1)
for a,b in pLst:
    ps[a] += b

def dfs(v, i):
    for j in to[v]:
        if j == i:
            continue
        ps[j] += ps[v]
        dfs(j,v)

dfs(1,0)
print(' '.join(map(str, ps[1:])))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

N,Q = map(int,input().split())
aLst = [[int(i) for i in input().split()] for _ in range(N-1)]
pLst = [[int(i) for i in input().split()] for _ in range(Q)]
to = [[]for _ in range(N+1)]

for a,b in aLst:
    to[a].append(b)
    to[b].append(a)

ps = [0]*(N+1)
for a,b in pLst:
    ps[a] += b

def dfs(v, i):
    for j in to[v]:
        if i == j:
            continue
        ps[j] += ps[v]
        dfs(j,v)

dfs(1,0)
print(' '.join(map(str, ps[1:])))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

N,Q = map(int,input().split())
aLst = [[int(i) for i in input().split()] for _ in range(N-1)]
pLst = [[int(i) for i in input().split()] for _ in range(Q)]
to = [[]for _ in range(N)]

for a,b in aLst:
    to[a-1].append(b-1)
    to[b-1].append(a-1)

ps = [0]*N
for a,b in pLst:
    ps[a-1] += b

def dfs(v, i):
    for j in to[v]:
        if i == j:
            continue
        ps[j] += ps[v]
        dfs(j,v)

dfs(0,-1)
print(' '.join(map(str, ps)))

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
aLst = [[int(i) for i in input().split()] for _ in range(N-1)]
pLst = [[int(i) for i in input().split()] for _ in range(Q)]
to = [[]for _ in range(N)]

for i in aLst:
    a = i[0] -1
    b = i[1] -1
    to[a].append(b)
    to[b].append(a)

ps = [0]*N
for i in pLst:
    a = i[0] -1
    b = i[1]
    ps[a] += b

def dfs(v, i):
    for j in to[v]:
        if i == j:
            continue
        ps[j] += ps[v]
        dfs(j,v)

dfs(0,-1)
print(' '.join(map(str, ps)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
n, q = map(int, input().split())
edges = [list(map(lambda x:int(x)-1, input().split())) for _ in range(n-1)]
adj = [[] for _ in range(n)]
for a, b in edges:
    adj[a].append(b)
    adj[b].append(a)

query = [0]*n
for _ in range(q):
    a, v = map(int, input().split())
    query[a-1] += v

q = [0]
ans = [0]*n
visited = [false]*n
visited[0] = true
while q:
    cur = q.pop(0)
    visited[cur] = true
    ans[cur] += query[cur]
    for nex in adj[cur]:
        if not visited[nex]:
            ans[nex] += ans[cur]
            q.append(nex)
print(*ans)

Submission status: Runtime Error

Solution code:
#import sys
#input = sys.stdin.readline
N,Q = [int(i) for i in input().split()]
#a_all, b_all = [[int(j) for j in input().split()] for i in range(N-1)]
#x_all, x_all = [[int(j) for j in input().split()] for i in range(Q)]

graph = [[] for i in range(N)]
for i in range(N-1):
    a, b = [int(j) for j in input().split()]
    graph[a-1].append(b)

counter = [0 for i in range(N)]
def count(p, x):
    counter[p-1] += x
    for child in graph[p-1]:
        count(child, x)

for i in range(Q):
    p, x = [int(j) for j in input().split()]
    count(p, x)
    
print(*counter)