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: Wrong Answer

Solution code:
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)]
tree = [[] for i in range(N)]
ans = [0]*N
ad = [0]*N
for a,b in ab:
  tree[a-1].append(b-1)
  

for i in range(N-1,-1,-1):
  x = tree[i].copy()
  for j in x:
    tree[i]+=tree[j]

for p,x in px:
  ad[p-1]+=x
  

for i,a in enumerate(ad):
  ans[i]+=a
  x=tree[i].copy()
  for j in x:
    ans[j]+=a
    
print(' '.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
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)]
tree = [[] for _ in range(N)]
ans = [0] * N
for a,b in ab:
  tree[a-1].append(b-1)
  

for i in range(N-1,-1,-1):
  x = tree[i]
  for j in x:
    x+=tree[j]

for p,x in px:
  i = p-1
  ans[i]+=x
  for j in tree[i]:
    ans[j]+=x
    
print(' '.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
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)]
tree = [[] for _ in range(N)]
ans = [0] * N
for a,b in ab:
  tree[a-1].append(b-1)
  
for p,x in px:
  i = p-1
  ans[i]+=x
  judge = tree[i].copy()
  while judge!=[]:
    a=judge.pop(0)
    ans[a]+=x
    judge+=tree[a]
    
print(' '.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
n,q = map(int, input().split())
ki = [0]*n
ans = [0]*n
for i in range(n-1):
    a,b = map(int, input().split())
    a -=1
    b -=1
    ki[b] = a

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

for i in range(1,n):
    ans[i] += ans[ki[i]]
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

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)

counter = [0] * N

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

def process(root):
    for e in edges[root]:
        counter[e] += counter[root]
        process(e)

process(0)

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

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)

counter = [0] * N

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

for i in range(N - 1):
    for e in edges[i]:
        counter[e] += counter[i]

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

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)

counter = [0] * N

def process(root, value):
    counter[root] += value
    for next_root in edges[root]:
        process(next_root, value)

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
inputs = lambda:map(int, input().split())
n, q = inputs()
e = [[] for _ in range(n)]
counter = [0] * n
m = n-1
for _ in range(m):
    v1, v2 = inputs()
    v1 -= 1
    v2 -= 1
    e[v1] += v2,
for _ in range(q):
    p, x = inputs()
    p -= 1
    counter[p] += x
used = [False] * n
s = 0
used[s] = True
stack = [s]
while stack:
    s = stack.pop()
    for c in e[s]:
        if used[c]:
            None
        else:
            used[c] = True
            counter[c] += counter[s]
            stack += c,
print(*counter)

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
#a=[0]*(N-1)
#b=[0]*(N-1)
p=[0]*N
x=[0]*N
tree=[[] for i in range(N)]
cost=[0]*N
for i in range(N-1):
    a,b=map(int,input().split())
    tree[a-1].append(b-1)
for i in range(Q):
    p,x=map(int,input().split())
    cost[p-1] += x

for i in range(N):
    for j in tree[i]:
        cost[j] += cost[i]

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

Submission status: Wrong Answer

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for j in range(1, N)]

query_list = [input().split() for k in range(Q)]
query_list_int = [[int(k) for k in i] for i in query_list]

val_list = [0 for _ in range(N)]

linked_node_list = [[] for _ in range(N)]

# 無向グラフのリンク関係を作成（上流への接続も含んでいる）
for a, b in tree_list:
    a, b = int(a)-1, int(b) -1
    linked_node_list[a].append(b) #子の追加
    # linked_node_list[b].append(a) #親の追加


for index, val in query_list_int:
    val_list[index-1] += val

stack = [0] #ルートノードの値を入れたスタックを生成、巡回するターゲットを格納する
parent = [0] * (N+1) #訪問済みノードを記憶するスタックを格納する

#再帰関数を使うとメモリ制限に引っかかるので、whileで順次実行する
# スタックでLIFOを使う。LIFOになるので、一番若い者をみながら深さ優先探索にできる。
# 深さが行きついたときに、残っているノードのうち親ノードに遡ると想定している巡回にならないから
# 親ノードを覚えておいて弾くようにする
# 有向グラフで定義すれば親を弾く処理はいらない

while True:
    #ルートから順に巡回
    v=stack.pop()
    for child in linked_node_list[v]:
        # if child != parent[v]:
            # parent[child] = v # 訪問済みノードを格納
        stack.append(child) #このノードvの接続先ノードをスタックに格納
        val_list[child] += val_list[v] #累積和
    if not stack:
        #スタックがなくなれば巡回終了
        break

print(*val_list)

Submission status: Wrong Answer

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for j in range(1, N)]
query_list = [input().split() for k in range(Q)]


class Node:
    def __init__(self, val):
        self.val = val
        self.child_list = []
        self.cnt = 0

class my_tree:
    def __init__(self, tree_list):
        self.node_list = []

        for i in range(N):
            self.node_list.append(Node(i+1))

        for a, b in tree_list:
            a, b = int(a), int(b)

            child_node = self.node_list[b-1]
            self.node_list[a-1].child_list.append(child_node)
        
    def adding(self, query_list):
        for a, data in query_list:
            a, data = int(a), int(data)
            self.node_list[a-1].cnt += data

        stack = [self.node_list[0]]

        while True:
            v = stack.pop()
            for child in v.child_list:
                child.cnt += v.cnt
                stack.append(child)
            
            if len(stack) == 0:
                break


ins = my_tree(tree_list)
ins.adding(query_list)
print(*[node.cnt for node in ins.node_list])

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for j in range(1, N)]
query_list = [input().split() for k in range(Q)]


class Node:
    def __init__(self, val):
        self.val = val
        self.child_list = []
        self.cnt = 0

class my_tree:
    def __init__(self, tree_list):
        self.node_list = []

        for i in range(N):
            self.node_list.append(Node(i+1))

        for a, b in tree_list:
            a, b = int(a), int(b)

            child_node = self.node_list[b-1]
            self.node_list[a-1].child_list.append(child_node)

    def show_node(self):
        for node in self.node_list:
            print(node.val, ' has ', [child.val for child in node.child_list])

        
    def adding(self, query_list):
        for a, data in query_list:
            a, data = int(a), int(data)
            self.node_list[a-1].cnt += data

        stack = deque([self.node_list[0]])

        while True:
            v = stack.pop()
            for child in v.child_list:
                child.cnt += v.cnt
                stack.append(child)
            
            if len(stack) == 0:
                break


ins = my_tree(tree_list)
# ins.show_node()
ins.adding(query_list)
print(*[node.cnt for node in ins.node_list])

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tree=[[] for _ in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
p=[0]*q
x=[0]*q
for i in range(q):
    p[i],x[i] = map(int,input().split())
    
px=[[] for i in range(n)]
for pp,xx in zip(p,x):
    px[pp-1].append(xx)
px=[sum(i) for i in px]
c=[0]*n
flag=[True]*n
 
from collections import deque
q = deque([(-1,0,0)])
while q:
    parent,child,point = q.popleft()
    if parent!=-1:
        flag[parent]=False
    point+=px[child]
    c[child]=point
    print(parent,child,point,tree[child],c)
    for y in tree[child]:
        if y == parent:
            continue
        if flag[y]:
            q.append((child,y,point))
print(*c)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tree=[[] for _ in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    tree[a-1].append(b-1)
#    tree[b-1].append(a-1)
p=[0]*q
x=[0]*q
for i in range(q):
    p[i],x[i] = map(int,input().split())
    
px=[[] for i in range(n)]
for pp,xx in zip(p,x):
    px[pp-1].append(xx)
px=[sum(i) for i in px]

c=[0]*n

from collections import deque
q = deque([(-1,0,0)])
while q:
    parent,child,point = q.popleft()
    point+=px[child]
    c[child]=point
    for y in tree[child]:
        if y == parent:
            continue
        q.append((child,y,point))
print(*c)

Submission status: Wrong Answer

Solution code:
n, q = (int(i) for i in input().split())
A = [[int(i) for i in input().split()] for i in range(n-1)]
B = [[int(i) for i in input().split()] for i in range(q)]

count = {i:0 for i in range(1,n+1)}
for k,v in B: count[k] += v

tree = {}
for l,r in A:
    if not l in tree: tree[l] = [r]
    else: tree[l] += [r]

ans = {i:0 for i in range(1,n+1)}
ans[1] = count[1]

for k,v in tree.items():
    for i in v:
        ans[i] += ( ans[k] + count[i] )

for a in ans.values():
    print(a,end=' ')

Submission status: Wrong Answer

Solution code:
n, q = (int(i) for i in input().split())
A = [[int(i) for i in input().split()] for i in range(n-1)]
B = [[int(i) for i in input().split()] for i in range(q)]

count = {i:0 for i in range(1,n+1)}
for k,v in B: count[k] += v

tree = {}
for l,r in A:
    if not l in tree: tree[l] = [r]
    else: tree[l] += [r]

ans = {i:0 for i in range(1,n+1)}
ans[1] = count[1]

for k,v in tree.items():
    for i in v:
        ans[i] += ( ans[k] + count[i] )

for a in ans.values():
    print(a,end=' ')
print()

Submission status: Wrong Answer

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

q = deque([[1,0]])
while len(q)!=0:
    now = q.pop()
    point[now[0]] += point[now[1]]
    for i in range(len(g[now[0]])):
        if g[now[0]][i]==now[1]:
            continue
        q.append([g[now[0]][i],now[0]])
print(1)
"""
for i in range(1, n+1):
    print(point[i], end='')
    if i!=n:
        print(' ', end='')
"""

Submission status: Wrong Answer

Solution code:
import heapq
n, q = map(int, input().split())
g = [[] for i in range(n+1)]
point = [0 for i in range(n+1)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)
for i in range(q):
    p, x = map(int, input().split())
    point[p] += x
print(1)
"""
q = [[1,0]]
heapq.heapify(q)
while len(q)!=0:
    now = heapq.heappop(q)
    point[now[0]] += point[now[1]]
    for i in range(len(g[now[0]])):
        if g[now[0]][i]==now[1]:
            continue
        heapq.heappush(q,[g[now[0]][i],now[0]])

for i in range(1, n+1):
    print(point[i], end='')
    if i!=n:
        print(' ', end='')
"""

Submission status: Wrong Answer

Solution code:
#D
n,q=map(int,input().split())
tree=[[] for _ in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    tree[a].append(b)
#    tree[b-1].append(a-1)
p=[0]*q
x=[0]*q
for i in range(q):
    p[i],x[i] = map(int,input().split())
    
tree.pop(0)
px=[[] for i in range(n+1)]
for pp,xx in zip(p,x):
    px[pp].append(xx)
px.pop(0)
px=[sum(i) for i in px]

c=[0]*n

from collections import deque
q = deque([(0,1,0)])
while q:
    parent,child,point = q.popleft()
    c[parent-1]=point
    if len(tree[child-1])==0:
        c[child-1]=point+px[child-1]
    for y in tree[child-1]:
        if y == parent:
            continue
        q.append((child,y,point+px[parent]))
print(*c)

Submission status: Wrong Answer

Solution code:
N,Q=map(int, input().split())
connections=[[] for _ in range(N+1)]
for n in range(N - 1):
    a, b = map(int, input().split())
    connections[a].append(b)
    connections[b].append(a)
scores = [0]*(N+1)
for _ in range(Q):
    tag, p = map(int, input().split())
    scores[tag] += p
    
stack=[(1,-1)]
while stack:
    kbef=stack.pop()
    k=kbef[0]
    before=kbef[1]
    kids=connections[k]
    if len(kids)==1 and before==kids[0]:
        break
    for kid in kids:
        if before==kid:
            continue
        scores[kid]+=scores[k]
        stack.append((kid,k))
        
print(*scores[1:])

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import queue

MOD = 10**9+7

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

node = [0]*N
edge = [[] for _ in range(N)]

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

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

q = queue.Queue()
q.put(0)
while not q.empty():
    parent = q.get()
    for i in edge[parent]:
        q.put(i)
        node[i]+=node[parent]

for n in node:
    print(n)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import queue

MOD = 10**9+7

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

node = [0]*N
edge = [[] for _ in range(N)]

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

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

q = queue.Queue()
q.put(0)
while not q.empty():
    parent = q.get()
    for i in edge[parent]:
        q.put(i)
        node[i]+=node[parent]

for n in node:
    print(n)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
A=[[] for i in range(n)]
node=[0]*n

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

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

ans=[0]*n
for i,j in enumerate(A):
    ans[i] +=node[i]
    for k in j:
        ans[k] +=ans[i]

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)


def rootedTree(node,parent,root,score):
    B.append([node,parent,root,score])
    for i in root:
        rootedTree(i,node,tree[i],score+C[i])


n,q=map(int,input().split())
tree=[[] for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    tree[a] +=[b]
B=[]
C=[0 for j in range(n+1)]
for i in range(q):
    add,point=map(int,input().split())
    C[add] +=point


rootedTree(1,-1,tree[1],C[1])
ans=[0]*n
for i in B:
    ans[i[0]-1]=i[3]
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for j in range(1, N)]

query_list = [input().split() for k in range(Q)]
query_list_int = [[int(k) for k in i] for i in query_list]

val_list = [0 for _ in range(N)]

linked_node_list = [[] for _ in range(N)]

for a, b in tree_list:
    a, b = int(a)-1, int(b) -1
    linked_node_list[a].append(b)
    linked_node_list[b].append(a)


for index, val in query_list_int:
    val_list[index-1] += val

stack = [0]
parent = [0] * (N+1)

while True:
    v=stack.pop()
    for child in linked_node_list[v]:
        if child != parent[v]:
            parent[child] = v
            stack.append(child)
            val_list[child] += val_list[v]
    print(stack)
    if not stack:
        break

print(*val_list)

Submission status: Wrong Answer

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for j in range(1, N)]

query_list = [input().split() for k in range(Q)]
query_list_int = [[int(k) for k in i] for i in query_list]

val_list = [0 for _ in range(N)]

linked_node_list = [[] for _ in range(N)]

for a, b in tree_list:
    a, b = int(a)-1, int(b) -1
    linked_node_list[a].append(b)
    linked_node_list[b].append(a)


for index, val in query_list_int:
    val_list[index-1] += val

stack = [0]
parent = [0] * (N+1)

while True:
    v=stack.pop()
    for child in linked_node_list[v]:
        if child != parent[v]:
            parent[child] = v
            stack.append(child)
            val_list[child] += val_list[v]
    print(stack)
    if not stack:
        break

print(val_list)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
n,q=map(int,input().split())
l=[[] for i in range(n+1)]
parent=[i for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a].append(b)
  parent[b]=a
lcount=[0 for i in range(n+1)]
for i in range(q):
  p,x=map(int,input().split())
  lcount[p]+=x
ans1=[0 for i in range(n)]
def find(n):
  if parent[n]==n:
    return n
  else:
    return find(parent[n])
oya=find(1)
def dfs(n1,count):
  count+=lcount[n1]
  ans1[n1-1]=count
  if l[n1]==[]:
    return
  else:
    for i in l[n1]:
      num=count
      dfs(i,num)
    return
dfs(oya,0)
ans=[]
for i in range(n):
  ans.append(str(ans1[i]))
print(" ".join(ans))

Submission status: Wrong Answer

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

import queue
n,qq=map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(n-1)]
v=[[] for _ in range(n+1)]
for _ in range(n-1):
    a,b=AB[_]
    v[a].append(b)
    v[b].append(a)

p=[-1]*(n+1)
p[1]=0
chi=[[] for _ in range(n+1)]
q=queue.Queue()
q.put(1)
"""
#while not q.empty():
for _ in range(n-1):
    t=q.get()
    for x in v[t]:
        if(p[x]==-1):
            chi[t].append(x)
            p[x]=t
            q.put(x)

s=[0]*(n+1)
for i in range(qq):
    ver,poi=map(int,input().split())
    s[ver]+=poi

q.put(1)
#while not q.empty():
for _ in range(n):
    t=q.get()
    for x in chi[t]:
        s[x]+=s[t]
        q.put(x)
for i in range(1,n+1):
    print(s[i],end=" ")

"""

Submission status: Wrong Answer

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [[int(k) for k in i.split()] for i in tree_list]
query_list = [input() for _ in range(Q)]
query_list_int = [[int(k) for k in i.split()]  for i in query_list]

class Node:
    def __init__(self, data):
        self.data = data
        self.cnt = 0
        self.child = []


class tree:
    def __init__(self, N, tree_list_int):
        self.node_list = [Node(i) for i in range(1,N+1)]

        for i, j in tree_list_int:
            self.node_list[i-1].child.append(self.node_list[j-1])


    def dfs(self, adder_list):
        cumulative_sum_list = [adder_list[0]]

        for val in adder_list[1:]:
            cumulative_sum_list.append(val + cumulative_sum_list[-1])

        return cumulative_sum_list


        
ins=tree(N, tree_list_int)

adder_list = [0 for _ in range(N)]
for i, val in query_list_int:
    adder_list[i-1] = val

res = ins.dfs(adder_list)

for j in res:
    print(j)

Submission status: Wrong Answer

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [[int(k) for k in i.split()] for i in tree_list]
query_list = [input() for _ in range(Q)]
query_list_int = [[int(k) for k in i.split()]  for i in query_list]

class Node:
    def __init__(self, data):
        self.data = data
        self.cnt = 0
        self.child = []


class tree:
    def __init__(self, N, tree_list_int):
        self.node_list = [Node(i) for i in range(1,N+1)]

        for i, j in tree_list_int:
            self.node_list[i-1].child.append(self.node_list[j-1])


    def dfs(self, adder_list):
        cumulative_sum_list = [adder_list[0]]

        for val in adder_list[1:]:
            cumulative_sum_list.append(val + cumulative_sum_list[-1])

        return cumulative_sum_list


        
ins=tree(N, tree_list_int)

adder_list = [0 for _ in range(N)]
for i, val in query_list_int:
    adder_list[i-1] = val

res = ins.dfs(adder_list)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import queue
n,qq=map(int,input().split())
v=[[] for _ in range(n+1)]
for _ in range(n-1):
    a,b=map(int,input().split())
    v[a].append(b)
    v[b].append(a)

p=[-1]*(n+1)
p[1]=0
chi=[[] for _ in range(n+1)]
q=queue.Queue()
q.put(1)
"""
while not q.empty():
    t=q.get()
    for x in v[t]:
        if(p[x]==-1):
            chi[t].append(x)
            p[x]=t
            q.put(x)
            
s=[0]*(n+1)
for i in range(qq):
    ver,poi=map(int,input().split())
    s[ver]+=poi
    
q.put(1)
while not q.empty():
    t=q.get()
    for x in chi[t]:
        s[x]+=s[t]
        q.put(x)
for i in range(1,n+1):
    print(s[i],end=" ")

"""

Submission status: Wrong Answer

Solution code:
import queue
n,qq=map(int,input().split())
v=[[] for _ in range(n+1)]
for _ in range(n-1):
    a,b=map(int,input().split())
    v[a].append(b)
    v[b].append(a)

p=[-1]*(n+1)
p[1]=0
chi=[[] for _ in range(n+1)]
q=queue.Queue()
q.put(1)

while not q.empty():
    t=q.get()
    for x in v[t]:
        if(p[x]==-1):
            chi[t].append(x)
            p[x]=t
            q.put(x)
"""
s=[0]*(n+1)
for i in range(qq):
    ver,poi=map(int,input().split())
    s[ver]+=poi
    
q.put(1)
while not q.empty():
    t=q.get()
    for x in chi[t]:
        s[x]+=s[t]
        q.put(x)
for i in range(1,n+1):
    print(s[i],end=" ")

"""

Submission status: Wrong Answer

Solution code:
import queue
n,qq=map(int,input().split())
v=[[] for _ in range(n+1)]
for _ in range(n-1):
    a,b=map(int,input().split())
    v[a].append(b)
    v[b].append(a)

p=[-1]*(n+1)
p[1]=0
chi=[[] for _ in range(n+1)]
q=queue.Queue()
q.put(1)
"""
while not q.empty():
    t=q.get()
    for x in v[t]:
        if(p[x]==-1):
            chi[t].append(x)
            p[x]=t
            q.put(x)
            
s=[0]*(n+1)
for i in range(qq):
    ver,poi=map(int,input().split())
    s[ver]+=poi
    
q.put(1)
while not q.empty():
    t=q.get()
    for x in chi[t]:
        s[x]+=s[t]
        q.put(x)
for i in range(1,n+1):
    print(s[i],end=" ")

"""

Submission status: Wrong Answer

Solution code:
import sys
def add1(x,p):
    add = Result[x]
    Ans[x] = add + p

    for c in Data[x]:
        add1(c,add+p)

sys.setrecursionlimit(10**9)
N,Q = map(int,input().split())
Data = [[] for _ in range(N+1)]
Result = [0] *(N+1)
Ans    = [0] *(N+1)
for _ in range(N-1):
    a,b = map(int,input().split())
    Data[a].append(b)

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

for i in range(1,N+1):
    print(Ans[i], end=' ')
print('')

Submission status: Wrong Answer

Solution code:
import sys
def add1(x,p):
    Result[x] += p
    for c in Data[x]:
        add1(c,p)

sys.setrecursionlimit(10**9)
N,Q = map(int,input().split())
Data = [[] for _ in range(N+1)]
Result = [0] *(N+1)
for _ in range(N-1):
    a,b = map(int,input().split())
    Data[a].append(b)

for _ in range(Q):
    x,p = map(int,input().split())
    add1(x,p)
for i in range(1,N+1):
    print(Result[i], end=' ')
print('')

Submission status: Wrong Answer

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [list(map(int,i.split())) for i in tree_list]
action_list = [input() for _ in range(Q)]
action_list_int = [list(map(int, i.split())) for i in action_list]

print('tree', tree_list_int)
print('act', action_list_int)

class Node:
    def __init__(self, data):
        self.data = data
        self.cnt = 0
        self.child = []


class tree:
    def __init__(self, N, tree_list_int):
        self.node_list = [Node(i) for i in range(1,N+1)]

        for i, j in tree_list_int:
            self.node_list[i-1].child.append(self.node_list[j-1])


    def adder(self, node, val):
        node.cnt += val

        if not node.child:
            return node.cnt

        for next_child in node.child:
            self.adder(next_child, val)

                

        
ins=tree(N, tree_list_int)

for pos, val in action_list_int:
    ins.adder(ins.node_list[pos-1], val)


for i in range(N):
    print(i+1, ins.node_list[i].cnt)

Submission status: Wrong Answer

Solution code:
# Nがノードの数, Qが操作回数
N, Q = map(int, input().split())
# 頂点毎のポイント
c = [0] * N
# 頂点の親
parents = [0] * N
for _ in range(N - 1):
  # 頂点bの親が頂点a
  a, b = map(int, input().split())
  parents[b - 1] = a - 1
for _ in range(Q):
  # 頂点p, ポイントx
    p, x = map(int, input().split())
    c[p - 1] += x

# print(parents)
# print(c)
for i in range(1, N):
  c[i] += c[parents[i]]
print(' '.join(map(str, c)))

Submission status: Wrong Answer

Solution code:
# Nがノードの数, Qが操作回数
N, Q = map(int, input().split())
# 頂点毎のポイント
c = [0] * N
# 頂点の親
parents = [0] * N
for _ in range(N - 1):
  # 頂点bの親が頂点a
  a, b = map(int, input().split())
  parents[b - 1] = a - 1
for _ in range(Q):
  # 頂点p, ポイントx
    p, x = map(int, input().split())
    c[p - 1] += x

# print(parents)
# print(c)
for i in range(1, N):
  c[i] += c[parents[i]]
print(c)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
A=[]
B=[]
K=[0]*(N+1)
for i in range(N-1):
    C, D = map(int, input().split())
    A.append([C, D])

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

for i in range(Q):
    K[B[i][0]-1]+=B[i][1]

for i in range(N-1):
    K[A[i][1]-1]+=K[A[i][0]-1]

for i in range(N):
    print(K[i], end=" ")

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,q=map(int,input().split())
a=[0]*(n-1)
b=[0]*(n-1)
for i in range(n-1):
    a[i],b[i]=map(int,input().split())
ab=[]
for i in range(q):
    ab.append(list(map(int,input().split())))
ab.sort()
cnt=[0]*n
ans=[0]*n
ans1=0
ansans=[0]*n
for i in range(n-1):
    cnt[b[i]-1]=a[i]
#print(cnt)
for j in range(q):
    ansans[ab[j][0]-1]+=ab[j][1]
#print(ansans)
#ans[0]+=ansans[0]
for k in range(n):
    ans[k]=ansans[k]+ans[cnt[k]-1]
#print(ans)
for i in range(n):
    ans[i]=str(ans[i])
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ab=[]
#print(ab)
p=[0]*q
x=[0]*q
for i in range(n-1):
    ab.append(list(map(int,input().split())))
#print(ab)
for i in range(q):
    p[i],x[i]=map(int,input().split())
ans=[0]*n
cnt=[0]*n
ans1=0
ansans=[0]*n
ab.sort()
for i in range(n-1):
    cnt[ab[i][1]-1]=ab[i][0]
#print(cnt)
for j in range(q):
    ansans[p[j]-1]+=x[j]
#print(ansans)
#ans[0]+=ansans[0]
for k in range(n):
    ans[k]=ansans[k]+ans[cnt[k]-1]
#print(ans)
for i in range(n):
    ans[i]=str(ans[i])
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ab=[]
#print(ab)
p=[0]*q
x=[0]*q
for i in range(n-1):
    ab.append(list(map(int,input().split())))
#print(ab)
for i in range(q):
    p[i],x[i]=map(int,input().split())
ans=[0]*n
cnt=[0]*n
ans1=0
ansans=[0]*n
for i in range(n-1):
    cnt[ab[i][1]-1]=ab[i][0]
#print(cnt)
for j in range(q):
    ansans[p[j]-1]+=x[j]
#print(ansans)
#ans[0]+=ansans[0]
for k in range(n):
    ans[k]=ansans[k]+ans[cnt[k]-1]
#print(ans)
for i in range(n):
    ans[i]=str(ans[i])
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
a=[0]*(n-1)
b=[0]*(n-1)
p=[0]*q
x=[0]*q
for i in range(n-1):
    a[i],b[i]=map(int,input().split())
for i in range(q):
    p[i],x[i]=map(int,input().split())
a.sort()
b.sort()
ans=[0]*n
cnt=[0]*n
ans1=0
ansans=[0]*n
for i in range(n-1):
    cnt[b[i]-1]=a[i]
#print(cnt)
for j in range(q):
    ansans[p[j]-1]+=x[j]
#print(ansans)
#ans[0]+=ansans[0]
for k in range(n):
    ans[k]=ansans[k]+ans[cnt[k]-1]
#print(ans)
for i in range(n):
    ans[i]=str(ans[i])
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
a=[0]*(n-1)
b=[0]*(n-1)
p=[0]*q
x=[0]*q
for i in range(n-1):
    a[i],b[i]=map(int,input().split())
for i in range(q):
    p[i],x[i]=map(int,input().split())
ans=[0]*n
cnt=[0]*n
ans1=0
ansans=[0]*n
for i in range(n-1):
    cnt[b[i]-1]=a[i]
#print(cnt)
for j in range(q):
    ansans[p[j]-1]+=x[j]
#print(ansans)
#ans[0]+=ansans[0]
for k in range(n):
    ans[k]=ansans[k]+ans[cnt[k]-1]
#print(ans)
for i in range(n):
    ans[i]=str(ans[i])
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
N, Q = list(map(int, input().split()))
E = []

for i in range(N-1):
  E.append(list(map(int, input().split())))
  
E.sort()

P = [0] * N
for i in range(Q):
  a,b= list(map(int, input().split()))
  P[a-1] += b
  
#print(E)
#print(P)

for edge in E:
  P[edge[1] - 1] += P[edge[0] - 1]
  
print(" ".join(list(map(str,P))))

Submission status: Wrong Answer

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

ki=[[0]*N for i in range(N)]

counter=[0]*N
"""for i in range(N):
    ki[i][i]=1"""

a=[0]*(N-1)
b=[0]*(N-1)
for i in range(N-1):
    a[i], b[i]=map(int, input().split())
    ki[a[i]-1][b[i]-1]=1
#print(ki)
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i], x[i]=map(int, input().split())
    counter[p[i]-1]=x[i]


for i in range(Q):
    for j in range(N):
        counter[j]+=counter[i]*ki[i][j]
print(" ".join(map(str, counter)))