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:
import numpy as np

N, Q = map(int, input().split())
Child = [[] for _ in range(N)]
X = np.zeros(N, np.int64)
Y = np.zeros(N, np.int64)
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    Child[a].append(b)
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    X[p] += x


def dfs(u, k):
    Y[u] += k
    for v in Child[u]:
        dfs(v, k + X[v])


dfs(0, X[0])
print(' '.join(str(y) for y in Y))

Submission status: Runtime Error

Solution code:
import numpy as np

N, Q = map(int, input().split())
Child = np.zeros((N, N), np.bool)
X = np.zeros(N, np.int64)
Y = np.zeros(N, np.int64)
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    Child[a, b] = True
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    X[p] += x


def dfs(u, k):
    Y[u] += k
    for v, flag in enumerate(Child[u, :]):
        if flag:
            dfs(v, k + X[v])


dfs(0, X[0])
print(' '.join(str(y) for y in Y))

Submission status: Runtime Error

Solution code:
import numpy as np

N, Q = map(int, input().split())
Child = np.zeros((N, N), np.bool)
X = np.zeros(N, np.int32)
Y = np.zeros(N, np.int32)
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    Child[a, b] = True
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    X[p] += x


def dfs(u, k):
    Y[u] += k
    for v in range(N):
        if Child[u, v]:
            dfs(v, k + X[v])


dfs(0, X[0])
print(' '.join(str(y) for y in Y))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)
def make_tree(node):
    for i in tree_list[node]:
        if i != tree_dic[node]["p"]:
            tree_dic[node]["c"].append(i)
            tree_dic[i]["p"] = node
            make_tree(i)

n, q = map(int, input().split())
tree_list = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree_list[a-1].append(b-1)
    tree_list[b-1].append(a-1)

tree_dic = {} #tree_dic = {i:{p:j, c:[k, l]}}
for i in range(n):
    tree_dic[i] = {"p": None, "c": [], "x": 0}

make_tree(0)

for i in range(q):
    p, x = map(int, input().split())
    tree_dic[p-1]["x"] += x
# print(tree_dic)

x_list = [0 for _ in range(n)]

import queue
next_visit = queue.Queue()
next_visit.put(0)
while not next_visit.empty():
    i = next_visit.get()
    tmp = tree_dic[i]["x"]
    if i != 0:
        tmp += x_list[tree_dic[i]["p"]]
    x_list[i] = tmp
    for j in tree_dic[i]["c"]:
        next_visit.put(j)

for i in x_list:
    print(i, end=" ")

Submission status: Runtime Error

Solution code:
def make_tree(node):
    for i in tree_list[node]:
        if i != tree_dic[node]["p"]:
            tree_dic[node]["c"].append(i)
            tree_dic[i]["p"] = node
            make_tree(i)

n, q = map(int, input().split())
tree_list = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree_list[a-1].append(b-1)
    tree_list[b-1].append(a-1)

tree_dic = {} #tree_dic = {i:{p:j, c:[k, l]}}
for i in range(n):
    tree_dic[i] = {"p": -1, "c": [], "x": 0}

make_tree(0)
# print(tree_dic)
for i in range(q):
    p, x = map(int, input().split())
    tree_dic[p-1]["x"] += x
# print(tree_dic)

x_list = [0 for _ in range(n)]

import queue
next_visit = queue.Queue()
next_visit.put(0)
while not next_visit.empty():
    i = next_visit.get()
    tmp = tree_dic[i]["x"]
    if i != 0:
        tmp += x_list[tree_dic[i]["p"]]
    x_list[i] = tmp
    for j in tree_dic[i]["c"]:
        next_visit.put(j)

for i in x_list:
    print(i, end=" ")

Submission status: Runtime Error

Solution code:
def make_tree(node):
    for i in tree_list[node]:
        if i != tree_dic[node]["p"]:
            tree_dic[node]["c"].append(i)
            tree_dic[i]["p"] = node
            make_tree(i)

n, q = map(int, input().split())
tree_list = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree_list[a-1].append(b-1)
    tree_list[b-1].append(a-1)

tree_dic = {} #tree_dic = {i:{p:j, c:[k, l]}}
for i in range(n):
    tree_dic[i] = {"p": None, "c": [], "x": 0}

make_tree(0)

for i in range(q):
    p, x = map(int, input().split())
    tree_dic[p-1]["x"] += x
# print(tree_dic)

x_list = [0 for _ in range(n)]

# import queue
# next_visit = queue.Queue()
# next_visit.put(0)
# while not next_visit.empty():
#     i = next_visit.get()
#     tmp = tree_dic[i]["x"]
#     if i != 0:
#         tmp += x_list[tree_dic[i]["p"]]
#     x_list[i] = tmp
#     for j in tree_dic[i]["c"]:
#         next_visit.put(j)

for i in x_list:
    print(i, end=" ")

Submission status: Runtime Error

Solution code:
def make_tree(node):
    for i in tree_list[node]:
        if i != tree_dic[node]["p"]:
            tree_dic[node]["c"].append(i)
            tree_dic[i]["p"] = node
            make_tree(i)

n, q = map(int, input().split())
tree_list = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree_list[a-1].append(b-1)
    tree_list[b-1].append(a-1)

tree_dic = {} #tree_dic = {i:{p:j, c:[k, l]}}
for i in range(n):
    tree_dic[i] = {"p": None, "c": [], "x": 0}

make_tree(0)

for i in range(q):
    p, x = map(int, input().split())
    tree_dic[p-1]["x"] += x
# print(tree_dic)

x_list = [0 for _ in range(n)]

import queue
next_visit = queue.Queue()
next_visit.put(0)
while not next_visit.empty():
    i = next_visit.get()
    tmp = tree_dic[i]["x"]
    if i != 0:
        tmp += x_list[tree_dic[i]["p"]]
    x_list[i] = tmp
    for j in tree_dic[i]["c"]:
        next_visit.put(j)

for i in x_list:
    print(i, end=" ")

Submission status: Runtime Error

Solution code:
def make_tree(node):
    for i in tree_list[node]:
        if i != tree_dic[node]["p"]:
            tree_dic[node]["c"].append(i)
            tree_dic[i]["p"] = node
            make_tree(i)

n, q = map(int, input().split())
tree_list = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree_list[a-1].append(b-1)
    tree_list[b-1].append(a-1)

tree_dic = {} #tree_dic = {i:{p:j, c:[k, l]}}
for i in range(n):
    tree_dic[i] = {"p": None, "c": [], "x": 0}

make_tree(0)

for i in range(q):
    p, x = map(int, input().split())
    tree_dic[p-1]["x"] += x
# print(tree_dic)
x_list = [0 for _ in range(n)]
for i in range(n):
    tmp = tree_dic[i]["x"]
    if i != 0:
        tmp += x_list[tree_dic[i]["p"]]
    x_list[i] = tmp

for i in x_list:
    print(i, end=" ")

Submission status: Runtime Error

Solution code:
def make_tree(node):
    for i in tree_list[node]:
        if i != tree_dic[node]["p"]:
            tree_dic[node]["c"].append(i)
            tree_dic[i]["p"] = node
            make_tree(i)

n, q = map(int, input().split())
tree_list = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree_list[a-1].append(b-1)
    tree_list[b-1].append(a-1)

tree_dic = {} #tree_dic = {i:{p:j, c:[k, l]}}
for i in range(n):
    tree_dic[i] = {"p": None, "c": [], "x": 0}

make_tree(0)

for i in range(q):
    p, x = map(int, input().split())
    tree_dic[p-1]["x"] += x
# print(tree_dic)
x_list = [0 for _ in range(n)]
for i in range(n):
    tmp = 0
    p = i
    while True:
        tmp += tree_dic[p]["x"]
        if p == 0:
            break
        p = tree_dic[p]["p"]
    x_list[i] = tmp

print(x_list)

Submission status: Runtime Error

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

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

point = [0]*(n+1)
for i in range(q):
  p, x = map(int, input().split())
  point[p] += x

seen = [False]*(n+1)
counter = [0]*(n+1)
sum_point = 0

def dfs(v):
  
  global graph
  global seen
  global sum_point
  global counter
  
  seen[v] = True
  sum_point += point[v]
  
  for next_v in graph[v]:
    
    if seen[next_v]: 
      continue
    dfs(next_v)
  
  counter[v] = sum_point
  sum_point -= point[v]

dfs(1)
print(*counter[1::])

Submission status: Runtime Error

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

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

point = [0]*(n+1)
for i in range(q):
  p, x = map(int, input().split())
  point[p] += x

seen = [False]*(n+1)
counter = [0]*(n+1)
sum_point = 0

def dfs(v):
  
  global graph
  global seen
  global sum_point
  global counter
  
  seen[v] = True
  sum_point += point[v]
  
  for next_v in graph[v]:
    
    if seen[next_v]: 
      continue
    dfs(next_v)
  
  counter[v] = sum_point
  sum_point -= point[v]

dfs(1)
print(counter[1::])

Submission status: Runtime Error

Solution code:
sys.setrecursionlimit(10**6)
edges = {}
p = []

def dfs(target_node, keisyo_point):
    global p
    p[target_node] += keisyo_point
    next_node = edges[target_node]
    if not next_node:
        return
    else:
        for next in next_node:
            dfs(next, p[target_node])

def main():
    global edges, p
    n, q = map(int, input().split())
    edges = {e: [] for e in range(1, n+1)}
    for _ in range(n - 1):
        a, b = tuple(map(int, input().split()))
        edges[a].append(b)
    p = [0] * (n + 1)
    for _ in range(q):
        p1, x1 = map(int, input().split())
        p[p1] += x1

    dfs(1, 0)
    for i1 in range(1, n + 1):
        print(p[i1], end=' ')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

sys.setrecursionlimit(10**6)
edges = {}
p = []

def dfs(target_node, keisyo_point):
    global p
    p[target_node] += keisyo_point

    next_node = edges[target_node]

    if not next_node:
        return
    else:
        for next in next_node:
            dfs(next, p[target_node])

def main():
    global edges, p
    n, q = map(int, input().split())
    #edges = [tuple(map(int, input().split())) for _ in range(n - 1)]
    edges = {e: [] for e in range(1, n+1)}
    for _ in range(n - 1):
        a, b = tuple(map(int, input().split()))
        edges[a].append(b)
    p = [0] * (n + 1)
    for _ in range(q):
        p1, x1 = map(int, input().split())
        p[p1] += x1

    dfs(1,0)
    for i1 in range(1, n + 1):
        print(p[i1], end=' ')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

n, q = map(int, input().split())
dist = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    dist[a - 1].append(b - 1)
value = []
cnt = [0 for i in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    value.append([p - 1, x])

def dfs(pos, addvalue):
    cnt[pos] += addvalue
    for i in dist[pos]:
        dfs(i, addvalue)

for i in value:
    dfs(i[0], i[1])

for i in cnt:
    print(i, end = " ")

Submission status: Runtime Error

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

def solve():
    import sys
    sys.setrecursionlimit(2*10**5)

    N, Q = list(map(int, sys.stdin.readline().split()))

    tree = [[] for _ in range(N)]  # 木構造
    for _ in range(N-1):
        a, b = list(map(int, sys.stdin.readline().split()))
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)

    anss = [0 for _ in range(N)]
    for _ in range(Q):
        p, x = list(map(int, sys.stdin.readline().split()))
        anss[p-1] += x

    def dfs(a, p):
        """
        a:対象番号
        p:親番号
        """
        for b in tree[a]:
            if b == p: continue
            anss[b] += anss[a]
            dfs(b, a)

    dfs(0, -1)

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


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

def solve():
    import sys
    sys.setrecursionlimit(2*10**5)

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

    tree = [[] for _ in range(N)]  # 木構造
    anss = [0 for _ in range(N)]
    for _ in range(N-1):
        a, b = list(map(int, input().split()))
        tree[a-1].append(b-1)

    def dfs(a, x):
        anss[a] += x

        for b in tree[a]:
            dfs(b, x)

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

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


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

def solve():
    N, Q = list(map(int, input().split()))

    tree = [[] for _ in range(N)]  # 木構造
    anss = [0 for _ in range(N)]
    for _ in range(N-1):
        a, b = list(map(int, input().split()))
        tree[a-1].append(b-1)

    def dfs(a, x):
        anss[a] += x

        for b in tree[a]:
            dfs(b, x)

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

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


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
for _ in range(N - 1):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    tree[x].append(y)
    tree[y].append(x)

cnt = [0 for _ in range(N)]
for _ in range(Q):
    x, y = map(int, input().split())
    x -= 1
    cnt[x] += y

#print(tree)
#print(cnt)
def dfs(cur=0, pre=-1, val=0):
    cnt[cur] += val
    for x in tree[cur]:
        if pre == x:
            continue
        dfs(x, cur, cnt[cur])


dfs()
print(*cnt)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
for _ in range(N - 1):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    tree[x].append(y)

cnt = [0 for _ in range(N)]
for _ in range(Q):
    x, y = map(int, input().split())
    x -= 1
    cnt[x] += y


def dfs(cur=0, val=0):
    cnt[cur] += val
    for x in tree[cur]:
        dfs(x, cnt[cur])

dfs()
print(*cnt)

Submission status: Runtime Error

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

tree={}
tree[1]=-1
cnt=[0]*N

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

for j in range(Q):
    p,x=map(int,input().split())
    p-=1
    cnt[p]+=x

for k in range(1,N):
    cnt[k]+=cnt[tree[k+1]-1]

# def search(root,x):
#     leaf =[]
#     for i,j in tree.items():
#         if j ==root:
#             leaf.append(i)
        
#     if len(leaf)!=0:
#         for k in leaf:
#             search(k,x)
#             cnt[k-1]+=x

# for i in range(N-1):
#     a,b = map(int,input().split())
#     tree[b]=a
# for j in range(Q):
#     p,x=map(int,input().split())
#     if p==1:
#         cnt+=x
#     else:
#         cnt[p-1]+=x
#         search(p,x)


print(*cnt)

Submission status: Runtime Error

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

import numpy as np
cnt = np.array([0]*N)

tree={}
tree[1]=-1

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

for j in range(Q):
    p,x=map(int,input().split())
    p-=1
    cnt[p]+=x

for k in range(1,N):
    cnt[k]+=cnt[tree[k+1]-1]

# def search(root,x):
#     leaf =[]
#     for i,j in tree.items():
#         if j ==root:
#             leaf.append(i)
        
#     if len(leaf)!=0:
#         for k in leaf:
#             search(k,x)
#             cnt[k-1]+=x

# for i in range(N-1):
#     a,b = map(int,input().split())
#     tree[b]=a
# for j in range(Q):
#     p,x=map(int,input().split())
#     if p==1:
#         cnt+=x
#     else:
#         cnt[p-1]+=x
#         search(p,x)


print(*cnt)

Submission status: Runtime Error

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

sys.setrecursionlimit(50000)

def yn(b):
    print("Yes" if b==1 else "No")
    return

class check():
    def __init__(self,n,q):
        readline=sys.stdin.readline
        self.tree=[[] for i in range(n)]
        self.ans=[0]*n
        for i in range(n-1):
            a,b=map(int, readline().rstrip().split())
            a-=1
            b-=1
            self.tree[a].append(b)
            self.tree[b].append(a)
        for i in range(q):
            p,x=map(int, readline().rstrip().split())
            p-=1
            self.ans[p]+=x
    def do(self,root,next):
        if root >= 0:
            self.ans[next]+=self.ans[root]
        for a in self.tree[next]:
            if a==root:
                continue
            self.do(next,a)
    def answer(self):
        print(" ".join(map(str,self.ans)))






def resolve():
    readline=sys.stdin.readline

    n,q=map(int, readline().rstrip().split())
    c=check(n,q)
    c.do(-1,0)
    c.answer()

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)

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

def dfs(frm, cur):
    cnt[cur] += cnt[frm]
    for to in edges[cur]:
        if frm != to:
            dfs(cur, to)

dfs(-1, 0)
print(*cnt)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
G = [[] for _ in [0]*N]
for _ in [0]*(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

cnt = [0]*N
pre = [-1]*N
k = 0


def init(v=0, p=-1):
    global k
    pre[v] = k
    k += 1

    for u in G[v]:
        if u == p:
            continue
        init(u, v)


def dfs(v, x):
    cnt[v] += x
    for u in G[v]:
        if pre[v] > pre[u]:
            continue
        dfs(u, x)
    return


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

print(*cnt)

Submission status: Runtime Error

Solution code:
def main(n,ab,px):
  g=[[]for _ in range(n+1)]
  for a,b in ab:
    g[a].append(b)
    g[b].append(a)
  ans=[0]*n
  for p,x in px:
    ans[p-1]+=x
  
  def dfs(seen,i):
    seen[i-1]=1
    l=g[i]
    for li in [li for li in l if seen[li-1]==0]:
      ans[li-1]+=ans[i-1]
      dfs(seen,li)
  seen=[0]*n
  dfs(seen,1)
  print(' '.join(list(map(str,ans))))

if __name__=='__main__':
  import sys
  input = sys.stdin.readline
  n,q=map(int,input().split())
  ab=[list(map(int,input().split())) for _ in range(n-1)]
  px=[list(map(int,input().split())) for _ in range(q)]
  main(n,ab,px)

Submission status: Runtime Error

Solution code:
def calc_parents(N, adj_nodes, root=0):
    parents = [-1 for _ in range(N)]
    frontier = [root]
    parents[root] = root
    while len(frontier) > 0:
        node = frontier.pop()
        for child in adj_nodes[node]:
            if parents[child] >= 0:
                continue
            frontier.append(child)
            parents[child] = node
    return parents


def sum_counts(N, parents, counter_roots):
    counter = [0 for _ in range(N)]
    for node in range(N):
        current_node = node
        parent_node = parents[current_node]
        s = counter_roots[current_node]
        while current_node != parent_node:
            current_node = parent_node
            parent_node = parents[current_node]
            s += counter_roots[current_node]
        counter[node] += s
    return counter


def main():
    N, Q = list(map(int, input().split(' ')))
    adj_nodes = defaultdict(list)
    for _ in range(N - 1):
        a, b = list(map(int, input().split(' ')))
        a -= 1
        b -= 1
        adj_nodes[a].append(b)
        adj_nodes[b].append(a)
    parents = calc_parents(N, adj_nodes)
    counter_roots = [0 for _ in range(N)]
    for _ in range(Q):
        p, x = list(map(int, input().split(' ')))
        p -= 1
        counter_roots[p] += x
    counter = sum_counts(N, parents, counter_roots)
    print(' '.join(map(str, counter)))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
N, Q = map(int, input().split())
AB = np.zeros([N, N])
for i in range(N):
  AB[i][i] += 1
  if i == N-1:
    continue
  a, b = map(int, input().split())
  AB[:a, b-1] += 1
AB = AB>0
ans = np.zeros([N])
for q in range(Q):
  p, x = map(int, input().split())
  ans[AB[p-1, :]==1] += x
ans = ans.astype(int)
print(*ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N, Q = map(int, input().split())
AB = np.zeros([N, N])
for i in range(N):
  AB[i][i] += 1
  if i == N-1:
    continue
  a, b = map(int, input().split())
  AB[:a][:b] += 1
AB = AB>0
ans = np.zeros([N])
for q in range(Q):
  p, x = map(int, input().split())
  ans[AB[p-1, :]==1] += x
ans = ans.astype(int)
print(*ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N, Q = map(int, input().split())
AB = np.zeros([N, N])
for i in range(N):
  AB[i][i] += 1
  if i == N-1:
    continue
  a, b = map(int, input().split())
  AB[:a][:b] += 1
AB = AB>0
ans = np.zeros([N])
for q in range(Q):
  p, x = map(int, input().split())
  ans[AB[p-1, :]==1] += x
print(ans)

Submission status: Runtime Error

Solution code:
P = [0]*(N+1)  #ポイント

D = [0]+[-1]*(N)
D[1] = 1
P[1] = H[1]
q = deque([1])

while q:
    n = q.pop()
    for d in G[n]:
        if D[d] != -1:
            continue
        else:
            D[d] = 1
            q.appendleft(d)
            P[d] = P[n] + H[d]
del P[0]

print(*P)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
process = [list(map(int, input().split())) for _ in range(Q)]

graph = [[] for _ in range(N+1)]

for edge in edges:
    graph[edge[0]].append(edge[1])

count = [0] * (N+1)

def dfs(p,x):
    count[p] += x
    for i in graph[p]:
        dfs(i,x)

for i in process:
    dfs(i[0], i[1])

del count[0]

print(' '.join(map(str, count)))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a = a-1
    b = b-1
    adjacent[a].append(b)
    adjacent[b].append(a)
points = [0]*N
for i in range(Q):
    p, q = map(int, input().split())
    p = p-1
    points[p] += q 
ans = [0]*N
def dfs(v, v_pre):
    ans[v] = ans[v_pre]+points[v]
    for w in adjacent[v]:
        if w == v_pre:
            continue
        dfs(w, v)
dfs(0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    adjacent[a-1].append(b-1)
    adjacent[b-1].append(a-1)
points = [0]*N
for i in range(Q):
    p, q = map(int, input().split())
    points[p-1] += q 
ans = [0]*N
def dfs(v, v_pre):
    ans[v] = ans[v_pre]+points[v]
    for w in adjacent[v]:
        if w == v_pre:
            continue
        dfs(w, v)
dfs(0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    adjacent[a-1].append(b-1)
    adjacent[b-1].append(a-1)
points = [0]*N
for i in range(Q):
    p, q = map(int, input().split())
    points[p-1] += q 
ans = [0]*N
def dfs(v, v_pre):
    ans[v] = ans[v_pre]+points[v]
    for w in adjacent[v]:
        if w == v_pre:
            continue
        dfs(w, v)
    return
dfs(0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
def dfs(ad, v, v_pre):
    scores[v] = Counter[v]
    if v_prev >= 0:
            scores[v] += scores[v_prev]
    for w in ad[v]:
        if w == v_pre:
            continue
        dfs(ad, w, v)
    return

N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
Counter = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    adjacent[a-1].append(b-1)
    adjacent[b-1].append(a-1)
for i in range(Q):
    p, q = map(int, input().split())
    Counter[p-1] += q 
scores = [0]*N
dfs(0, Counter, -1)
print(*scores)

Submission status: Runtime Error

Solution code:
def dfs(v, C, v_pre):
    for w in adjacent[v]:
        if w == v_pre:
            continue
        C[w] += Counter[v]
        dfs(w, C, v)
    return

N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
Counter = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    adjacent[a-1].append(b-1)
    adjacent[b-1].append(a-1)
for i in range(Q):
    p, q = map(int, input().split())
    Counter[p-1] += q 
dfs(0, Counter, -1)
print(*Counter)

Submission status: Runtime Error

Solution code:
def dfs(v, C, v_pre):
    for w in adjacent[v]:
        if w == v_pre:
            continue
        C[w] += Counter[v]
        dfs(w, C, v)
    return

N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
Counter = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    adjacent[a-1].append(b-1)
    adjacent[b-1].append(a-1)
for i in range(Q):
    p, q = map(int, input().split())
    Counter[p-1] += q 
dfs(0, Counter, -1)

Submission status: Runtime Error

Solution code:
def addScore(Point,Score):
    score[Point]+=Score
    min_index = binary_search(ab,Point)
    while min_index < n-1 and ab[min_index][0] == Point:
        addScore(ab[min_index][1],Score)
        min_index+=1
    

"""
minnum以上で最小の数のindexを返す。無いときはlen(list)を返す
計算量log(len(list))
"""

def binary_search(list, item):
    if list[0][0]>=item:
        return 0
    if list[-1][0]<item:
        return len(list)

    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) //2
        guess = list[mid][0]
        if guess >= item and list[mid-1][0] < item:
            return mid
        if guess >= item:
            high = mid -1
        else:
            low = mid + 1

    return None






n,q=map(int,input().split())

ab = []
for i in range(n-1):
    ab.append(list(map(int,input().split())))

ab.sort()

score=[0]*(n+1)

for _ in range(q):
    p,x=map(int,input().split())
    addScore(p,x)

print(*(score[1:]))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10 ** 6)
# import numpy as np

# 入力
N, Q = map(int, input().split())
g = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    g[a].append(b)
    g[b].append(a)
point = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    point[p] += x
ans = [0] * N
# 探索
def dfs(pre, now):
    # ansを更新
    ans[now] = ans[pre] + point[now]
    # 隣接頂点を探索
    for nxt in g[now]:
        if nxt == pre:
            continue
        dfs(now, nxt)
dfs(0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
def dfs(v, x, v_pre):
  Counter[v] += x
  for w in adjacent[v]:
    if w == v_pre:
      continue
    dfs(w, x, v)
  return

N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
Counter = [0]*N
for i in range(N-1):
  a, b = map(int, input().split())
  adjacent[a-1].append(b-1)
  adjacent[b-1].append(a-1)
Value = []
for i in range(Q):
  p, q = map(int, input().split())
  Value.append([p-1, q])
for i in range(Q):
  dfs(Value[i][0], Value[i][1], -1)

Submission status: Runtime Error

Solution code:
def dfs(v, x):
  Counter[v] += x
  for w in adjacent[v]:
    dfs(w, x)
  return

N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
Counter = [0]*N
for i in range(N-1):
  a, b = map(int, input().split())
  adjacent[a-1].append(b-1)
Value = []
for i in range(Q):
  p, q = map(int, input().split())
  Value.append([p-1, q])
for i in range(Q):
  dfs(Value[i][0], Value[i][1])

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
Counter = [0]*N
for i in range(N-1):
  a, b = map(int, input().split())
  adjacent[a-1].append(b-1)
Value = []
for i in range(Q):
  p, q = map(int, input().split())
  Value.append([p, q])
for i in range(Q):
  dfs(Value[i][0], Value[i][1])
  
def dfs(v, x):
  Counter[v] += x
  for w in adjacent[v]:
    dfs(w, x)
  return
print(*Counter)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
adjacent = [[] for i in range(N)]
Counter = [0]*N
for i in range(N-1):
  a, b = map(int, input().split())
  adjacent[a-1].append(b-1)
Value = []
for i in range(Q):
  p, q = map(int, input().split())
  Value.append([p, q])
for i in range(Q):
  dfs(Value[i][0], Value[i][1])
  
def dfs(v, x):
  Counter[v] += x
  for w in adjacent[v]:
    dfs(w, x)
  return
for x in Counter:
  print(x)

Submission status: Runtime Error

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

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

B=[]
for i in range(Q):
	b=list(map(int,input().split()))
	B.append(b)

    
def dfs(x,col):
    score[x-1]+=col[1]
    for j in A[x-1]:
       dfs(j,col)
        
for i in range(Q):
    dfs(B[i][0],B[i])

print(*score)

Submission status: Runtime Error

Solution code:

#Writer : Rute
import sys
sys.setrecursionlimit(10**5)
N,Q = map(int,input().split())
ls1 = [0]*N
#Graph
g = [[] for i in range(N)]
point = [0]*N
for i in range(N-1):
    a,b = map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for i in range(Q):
    p,x = map(int,input().split())
    point[p-1] += x
def dfs(now, prev = -1):
    for j in g[now]:
        if j == prev:
            continue
        point[j] += point[now]
        
        dfs(j,now)
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
#Writer : Rute
N,Q = map(int,input().split())
ls1 = [0]*N
#Graph
g = [[] for i in range(N)]
point = [0]*N
for i in range(N-1):
    a,b = map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for i in range(Q):
    p,x = map(int,input().split())
    point[p-1] += x
def dfs(now, prev = -1):
    for j in g[now]:
        
        if j == prev:
            continue
        point[j] += point[now]
        
        dfs(j,now)
dfs(0)
print(*point)