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:
from collections import deque
n,q=map(int,input().split())
K=[[] for _ in range(n)]
V=[False]*n
P=[0]*n
for i in range(n-1):
   a,b=map(int,input().split())
   K[a-1].append(b-1)
   K[b-1].append(a-1)
for i in range(q):
   q,x=map(int,input().split())
   P[q-1]=x
que=deque([0])
while que:
   s=que.popleft()
   V[s]=True
   for ni in K[s]:
      if V[ni]==False:
         P[ni]+=P[s]
         que.append(ni)
print(*P)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tree=[[] for _ in range(n)]
cnt=[0]*n

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

def counter(L,p,x):
    for i in L[p]:
        cnt[i] += x

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

for i in range(n):
    x=cnt[i]
    counter(tree,i,x)

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

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tree=[[] for _ in range(n)]
cnt=[0]*n

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

def counter(L,p,x):
    for i in L[p]:
        cnt[i] += x

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

for i in range(n):
    x=cnt[i]
    print(x)
    counter(tree,i,x)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tree=[[] for _ in range(n)]
cnt=[0]*n

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

def counter(L,p,x):
    for i in L[p]:
        cnt[i] += x

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

for i in range(n):
    x=cnt[i]
    counter(tree,i,x)

for c in cnt:
    print(c)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
parent = [-1] * n
c = [0] * n
for _ in range(n - 1):
    a, b = map(int, input().split())
    parent[b - 1] = a
for _ in range(q):
    p, x = map(int, input().split())
    c[p - 1] += x
for i in range(1, n):
    c[i] += c[parent[i] - 1]
print(" ".join(map(str, c)))

Submission status: Wrong Answer

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

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

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

#table
point = [0]*(N+1)
ans = [0]*(N+1)
for i in range(Q):
    p,x = map(int,input().split())
    point[p] += x

# DFS
visited = []
visiting = []
def DFS(n,o):
    ans[n] = ans[o] + point[n]
    visited.append(n)
    for i in tree[n]:
        if i not in visited:
            visiting.append(i)
    if len(visiting) > 0:
        k = visiting.pop()
        return DFS(k,n)

# main
DFS(1,0)

# output
print(" ".join(map(str,ans[1:])))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().rstrip()

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

    ans = [0 for _ in range(N + 1)]
    for p, x in PX:
        ans[p] += x
    def dfs(i):
        for j in ki[i]:
            ans[j] += ans[i]
            dfs(j)
    dfs(1)
    print(*ans[1:])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().rstrip()

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

    ans = [0 for _ in range(N + 1)]
    def dfs(i, n):
        ans[i] += n
        for j in ki[i]:
            dfs(j, n)

    for p, x in PX:
        dfs(p, x)
    print(*ans[1:])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()

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

    ki = [[] for _ in range(N + 1)]
    for a, b in AB:
        ki[a].append(b)

    d = deque()
    ans = [0 for _ in range(N + 1)]
    for p, x in PX:
        d.append(p)
        while d:
            key = d.pop()
            ans[key] += x
            for i in ki[key]:
                d.append(i)
    print(*ans[1:])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
	ta,tb = map(int, input().split())
	E[ta].append(tb)

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

q = [[1,0]]

while q:
	cp, cost = q.pop()
	cnt[cp] += cost

	for np in E[cp]:
		q.append([np, cnt[cp]])

print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from collections import defaultdict
from collections import deque

def main():
  n, q = map(int, input().strip().split())
  tree = defaultdict(lambda :{'child': [], 'counter': 0})
  for _ in range(n-1):
    a, b = map(int, input().strip().split())
    tree[a]['child'].append(b)
    
  for _ in range(q):
    p, x = map(int, input().strip().split())
    tree[p]['counter'] += x
    
  def bfs(root):
    dq = deque()
    dq.append(root)
    while dq:
      node = dq.popleft()
      for ichild in node['child']:
        tree[ichild]['counter'] += node['counter']      
        dq.append(tree[ichild])
    
  bfs(tree[1])
  print(" ".join(map(str, [tree[i]['counter'] for i in range(1, n+1)])))

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from collections import defaultdict
from collections import deque
def main():
  n, q = map(int, input().strip().split())
  tree = defaultdict(lambda :{'child': [], 'counter': 0})
  for _ in range(n-1):
    a, b = map(int, input().strip().split())
    tree[a]['child'].append(b)
    
  for _ in range(q):
    p, x = map(int, input().strip().split())
    tree[p]['counter'] += x
    
  def dfs(node, score=0):
    node['counter'] += score
    for ichild in node['child']:
      dfs(tree[ichild], node['counter'])
      
  def bfs(root):
    dq = deque()
    dq.append(root)
    while dq:
      node = dq.pop()
      for ichild in node['child']:
        tree[ichild]['counter'] += node['counter']      
        dq.append(tree[ichild])
    
#   dfs(tree[1])
  bfs(tree[1])
  print(" ".join(map(str, [tree[i]['counter'] for i in range(1, n+1)])))

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:



N,Q = map(int, input().split())
es = [[] for _ in range(N)]

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

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


for i in range(N):
    for nxt in es[i]:
        ans[nxt] += ans[i]

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500000)

N, Q = map(int, input().split())
G = [[] for _ in range(N)]
cnt = [0]*N


def dfs(start_node, x):
    for i in G[start_node]:
        cnt[i] += x
        dfs(i, x)

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

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

ans = map(str, cnt)
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
import heapq as h

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

stack=[0]
parent=[0]*N
order=[]
h.heapify(stack)
h.heapify(order)

while stack:
    x=h.heappop(stack)
    h.heappush(order,x)
    for i in hen[x]:
        if i==parent[x]:
            continue
        parent[i]=x
        h.heappush(stack,i)

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

for x in order:
    count=ans[x]
    for y in hen[x]:
        if y==parent[x]:
            continue
        ans[y]+=count

print(*ans)

Submission status: Wrong Answer

Solution code:
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.next_node = []

def dfs(root,x):
  tree_stack = [root]
  while len(tree_stack) > 0:
    node = tree_stack.pop(-1)
    node.val += x
    for n in node.next_node:
      tree_stack.append(n)

N,Q = (int(i) for i in input().split())

Nodes = {}
for i in range(N):
  Nodes[i] = TreeNode(0)

AB = []
for i in range(N-1):
  A,B = (int(i) for i in input().split())
  Nodes[A-1].next_node.append(Nodes[B-1])

PX = {}
for i in range(Q):
  P,X = (int(i) for i in input().split())
  PX[P] = PX.get(P,0) + X

for p,x in PX.items():
  dfs(Nodes[p-1],x)

ans = []
for node in Nodes.values():
  ans.append(node.val)

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

Submission status: Wrong Answer

Solution code:
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.next_node = []

def dfs(root,x):
  tree_stack = [root]
  while len(tree_stack) > 0:
    node = tree_stack.pop(-1)
    node.val += x
    for n in node.next_node:
      tree_stack.append(n)

N,Q = (int(i) for i in input().split())

Nodes = []
for i in range(N):
  Nodes.append(TreeNode(0))

AB = []
for i in range(N-1):
  A,B = (int(i) for i in input().split())
  Nodes[A-1].next_node.append(Nodes[B-1])

PX = {}
for i in range(Q):
  P,X = (int(i) for i in input().split())
  PX[P] = PX.get(P,0) + X

for p,x in PX.items():
  dfs(Nodes[p-1],x)

ans = []
for node in Nodes:
  ans.append(node.val)

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

Submission status: Wrong Answer

Solution code:
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.next_node = []

def dfs(root,x):
  tree_queue = [root]
  while len(tree_queue) > 0:
    node = tree_queue.pop(0)
    node.val += x
    for n in node.next_node:
      tree_queue.append(n)

N,Q = (int(i) for i in input().split())

Nodes = []
for i in range(N):
  Nodes.append(TreeNode(0))

AB = []
for i in range(N-1):
  A,B = (int(i) for i in input().split())
  Nodes[A-1].next_node.append(Nodes[B-1])

PX = {}
for i in range(Q):
  P,X = (int(i) for i in input().split())
  PX[P] = PX.get(P,0) + X

for p,x in PX.items():
  dfs(Nodes[p-1],x)

ans = []
for node in Nodes:
  ans.append(node.val)

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

Submission status: Wrong Answer

Solution code:
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.next_node = []

def dfs(root,x):
  tree_queue = [root]
  while len(tree_queue) > 0:
    node = tree_queue.pop(0)
    node.val += x
    for n in node.next_node:
      tree_queue.append(n)

N,Q = (int(i) for i in input().split())

AB = []
for i in range(N-1):
  AB.append([int(i) for i in input().split()])

PX = []
for i in range(Q):
  PX.append([int(i) for i in input().split()])

Nodes = []
for i in range(N):
  Nodes.append(TreeNode(0))

# Node_dict = {nodenum:Node_point}
Node_dict = {}
for ab in AB:
  A = ab[0]
  B = ab[1]
  Nodes[A-1].next_node.append(Nodes[B-1])

for px in PX:
  P = px[0]
  X = px[1]
  dfs(Nodes[P-1],X)

ans = []
for node in Nodes:
  ans.append(node.val)

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

Submission status: Wrong Answer

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

class TreeNode:
    def __init__(self, num):
        self.num = num
        self.children = []
        self.counter = 0

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

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

tree_dict = {}
for n in range(1, N+1):
    tree_dict[n] = {
        'children': [],
        'counter': 0
    }

# 木構造を辞書で表現
for (a, b) in ab:
    tree_dict[a]['children'].append(b)

px = []
for q in range(Q):
    _px = [int(i) for i in input().split()]
    px.append((_px[0], _px[1]))

# カウンター処理
# 神考案DFS
for (p, x) in px:
    stack = [p]
    while len(stack) > 0:
        now_num = stack.pop(-1)
        tree_dict[now_num]['counter'] += x
        stack.extend(tree_dict[now_num]['children'])

ans = ''
for vertex in tree_dict.values():
    ans += str(vertex['counter'])
    ans += ' '

print(ans)

Submission status: Wrong Answer

Solution code:
N,Q = [int(i) for i in input().split()]
graph = [[] for _ in range(N+1)]
ans = [0] * (N+1)
for i in range(N-1):
    a,b = [int(j) for j in input().split()]
    graph[a].append(b)
for i in range(Q):
    pa,xaa = [int(j) for j in input().split()]
    ans[pa] += xaa

seen = [False] * (N+1)

import collections
from collections import deque
que = deque()
que.append(1)
seen[1] = True
while len(que) != 0:
    x = que.popleft()
    seta = set(graph[x])
    for i in seta:
        if seen[i]:
            continue
        ans[i] += ans[x]
        que.append(i)
        seen[i] = True
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
N,Q = [int(i) for i in input().split()]
graph = [[] for _ in range(N+1)]
ans = [0] * (N+1)
for i in range(N-1):
    a,b = [int(j) for j in input().split()]
    graph[a].append(b)
p = [0] * Q
xa = [0] * Q
for i in range(Q):
    pa,xaa = [int(j) for j in input().split()]
    p[i] = pa
    xa[i] = xaa
for i in range(Q):
    ans[p[i]] += xa[i]
for i in range(1,N+1):
    seta = set(graph[i])
    for j in seta:
        ans[j] += ans[i]
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
N,Q = [int(i) for i in input().split()]
graph = [[] for _ in range(N+1)]
ans = [0] * (N+1)
for i in range(N-1):
    a,b = [int(j) for j in input().split()]
    graph[a].append(b)
p = [0] * Q
xa = [0] * Q
for i in range(Q):
    pa,xaa = [int(j) for j in input().split()]
    p[i] = pa
    xa[i] = xaa
for i in range(Q):
    ans[p[i]] += xa[i]
for i in range(1,N+1):
    ans[i] += ans[i-1]
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,q = map(int,readline().split())
ki = [[] for _ in range(n)]

for i in range(n-1):
    a,b = map(int,readline().split())
    if a < b:
        a,b = b,a
    a,b = a-1,b-1
    ki[a].append(b)

count = [0]*n
for j in range(q):
    p,x = map(int,readline().split())
    p -= 1
    count[p] += x

queue = deque([[0,count[0]]])
while queue:
    x,q = queue.popleft()
    for i in ki[x]:
        count[i] += q
        queue.append([i,count[i]])
        


print(*count)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,q = map(int,readline().split())
ki = [[] for _ in range(n)]

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

count = [0]*n
for j in range(q):
    p,x = map(int,readline().split())
    p -= 1
    count[p] += x

queue = deque([[0,count[0]]])
while queue:
    x,q = queue.popleft()
    for i in ki[x]:
        count[i] += q
        queue.append([i,count[i]])
        


print(*count)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,q = map(int,readline().split())
ki = [[] for _ in range(n)]

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

count = [0]*n
for j in range(q):
    p,x = map(int,readline().split())
    p -= 1
    count[p] += x

queue = deque([[0,count[0]]])
while queue:
    x,q = queue.pop()
    for i in ki[x]:
        count[i] += q
        queue.append([i,count[i]])
        


print(*count)

Submission status: Wrong Answer

Solution code:
N,Q = [int(i) for i in input().split()]
graph = [[] for _ in range(N+1)]
ans = [0] * (N+1)
for i in range(N-1):
    a,b = [int(j) for j in input().split()]
    graph[a].append(b)
p = [0] * Q
xa = [0] * Q
for i in range(Q):
    pa,xaa = [int(j) for j in input().split()]
    p[i] = pa
    xa[i] = xaa
def bfs(v,x):
    seen = [False] * (N+1)
    import collections
    from collections import deque
    que = deque()
    que.append(v)
    while len(que) != 0:
        k = que.popleft()
        seen[k] = True
        ans[k] += x
        seta = set(graph[k])
        for i in seta:
            if seen[i]:
                continue
            que.append(i)
for i in range(Q):
    v = p[i]
    x = xa[i]
    bfs(v,x)
print(*ans[1:])

Submission status: Wrong Answer

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

root = [0]*n
for _ in range(n-1):
    a, b = (int(x) for x in input().split())
    root[b-1] = a-1
#print(root)

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

for i in range(1, n):
    dp[i] += dp[root[i]]
    
print(' '.join(map(str,dp)))

Submission status: Wrong Answer

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

root = [0]*n
for _ in range(n-1):
    a, b = (int(x) for x in input().split())
    root[b-1] = a-1
print(root)

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

for i in range(1, n):
    dp[i] += dp[root[i]]
    
print(' '.join(map(str,dp)))

Submission status: Wrong Answer

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

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

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

while len(root):
    a, b = root.pop(0)
    dp[b] = dp[a]+dp[b]
    
print(' '.join(map(str,dp)))

Submission status: Wrong Answer

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce
import statistics


# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

def treesearch(root, data, ans):
    for i in range(len(data)):
        if data[i][0] == root:
            ans[data[i][1]-1] += ans[root-1]
            treesearch(data[i][1], data, ans)

data1 = getInputListInt()
n = data1[0]
q = data1[1]

data2 = getSomeInputListInt(n-1)

data3 = getSomeInputListInt(q)

ans = np.zeros(n)

for i in range(len(data3)):
    ans[data3[i][0]-1] += data3[i][1]

treesearch(1, data2, ans)

for s in ans:
    print(int(s), end =" ")

Submission status: Wrong Answer

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce
import statistics


# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

def treesearch(root, data, ans):
    for i in range(len(data)):
        if data[i][0] == root:
            ans[data[i][1]-1] += ans[root-1]
            treesearch(data[i][1], data, ans)

data1 = getInputListInt()
n = data1[0]
q = data1[1]

data2 = getSomeInputListInt(n-1)

data3 = getSomeInputListInt(q)

ans = np.zeros(n)



data3.sort(reverse=True)

for i in range(len(data3)):
    ans[data3[i][0]-1] += data3[i][1]
    treesearch(data3[i][0], data2, ans)

for s in ans:
    print(int(s), end =" ")

Submission status: Wrong Answer

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce
import statistics


# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

def treesearch(root, cost, data, ans):
    for i in range(len(data)):
        if data[i][0] == root:
            ans[data[i][1]-1] += cost
            treesearch(data[i][1], cost, data, ans)

data1 = getInputListInt()
n = data1[0]
q = data1[1]

data2 = getSomeInputListInt(n-1)

data3 = getSomeInputListInt(q)

ans = np.zeros(n)

for i in range(len(data3)):
    ans[data3[i][0]-1] += data3[i][1]
    treesearch(data3[i][0], data3[i][1], data2, ans)

for s in ans:
    print(int(s), end =" ")

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
l = [[0]*n for i in range(n)]
sc = [0]*n

for i in range(n):
  l[i][i] = 1
  l[0][i] = 1
for i in range(n-1):
  x,y = map(int, input().split())
  l[x-1][y-1] = 1
  
for j in range(m):
  x,y = map(int, input().split())
  for i in range(n):
    if l[x-1][i] == 1:
      sc[i] += y
print(*sc)

Submission status: Wrong Answer

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

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

que=deque([0])
f=[0]*n
f[0]=1
while que:
  v=que.popleft()
  c=s[v]
  for w in g[v]:
    if f[w]==1:
      continue
    f[w]=1
    s[w]+=c
    que.append(w)
  
print(*s)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
# def dfs(node_i):
#     for bi in tree[node_i]

N, Q = map(int, input().split())
# tree = [[] for i in range(N)]
tree = defaultdict(list)
tree[0] = []
tree_x = [0 for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    am1 = a-1
    if tree.get(am1) == -1:
        tree[b-1].append(am1)
    else:
        tree[am1].append(b-1)
        
# print(tree)
for q in range(Q):
    p, x = map(int, input().split())
    tree_x[p-1] += x

for i in range(N):
    for bi in tree[i]:
        tree_x[bi] += tree_x[i]


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

Submission status: Wrong Answer

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

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

for i in range(1, N+1):
    for bi in tree[i]:
        tree_x[bi] += tree_x[i]
print(' '.join(map(str, tree_x[1:])))

Submission status: Wrong Answer

Solution code:
import queue
N, Q = map(int, input().split())
tree = [[] for i in range(N+1)]
tree_x = [0 for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    tree[a].append(b)

for q in range(Q):
    p, x = map(int, input().split())
    tree_x[p] += x
q = queue.Queue()
q.put((1, tree[1]))

while not q.empty():
    a, b = q.get()
    add = tree_x[a]
    for bi in b:
        print(bi)
        tree_x[bi] += add
        q.put((bi, tree[bi]))
for i in range(1, N+1):
    print(tree_x[i], end=' ')

Submission status: Wrong Answer

Solution code:
n,q = map(int, input().split())
a=[]
for i in range(n-1):
    tmp = list(map(int,input().split()))
    a.append(tmp)

p=[]
for i in range(q):
    tmp = list(map(int,input().split()))
    p.append(tmp)

link = [[] for _ in range(n)]

for tmp in a:
    link[tmp[0]-1].append(tmp[1]-1)

from collections import defaultdict
d = defaultdict(int)

for tmp in p:
    d[tmp[0]-1]+=tmp[1]

from collections import deque
ans =[0]*n

Q = deque()
Q.append([0,0])

while Q:
    node= Q.popleft()

    val = node[1] + d[node[0]]
    ans[node[0]]+=val

    for nxt in link[node[0]]:
        Q.append([nxt,val])

print(*ans)

Submission status: Wrong Answer

Solution code:
n,q = map(int, input().split())
a=[]
for i in range(n-1):
    tmp = list(map(int,input().split()))
    a.append(tmp)

p=[]
for i in range(q):
    tmp = list(map(int,input().split()))
    p.append(tmp)

link = [[] for _ in range(n)]

for tmp in a:
    link[tmp[0]-1].append(tmp[1]-1)

from collections import defaultdict
d = defaultdict(int)

for tmp in p:
    d[tmp[0]-1]+=tmp[1]

from collections import deque
ans =[0]*n
for k,v in d.items():
    Q = deque()
    Q.append(k)

    while Q:
        node = Q.popleft()
        ans[node]+=v

        for nxt in link[node]:
            Q.append(nxt)
for a in ans:
    print(a,end=" ")

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
e=[list(map(int,input().split())) for _ in range(n-1)]
px=[list(map(int,input().split())) for _ in range(q)]
g=[[] for _ in range(n+1)]
for i in range(n-1):
  g[e[i][0]].append(e[i][1])

for i in range(1,n+1):
  for j in range(len(g[i])):
    g[i]+=g[g[i][j]]

ans=[0]*(n+1)
for i in range(q):
  ans[px[i][0]]+=px[i][1]

for i in range(q):
  score=px[i][1]
  for j in range(len(g[px[i][0]])):
    ans[g[px[i][0]][j]]+=score
    
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque
n, q = map(int, input().split())
tree = [[] for _ in range(n+1)]
cnt = [0] * (n+1)

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

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

queue = deque([1])
visited = {1}
while queue:
    now = queue.popleft()
    visited.add(now)
    for t in tree[now]:
        if t in visited:
            pass
        else:
            queue.append(t)
            cnt[t] += cnt[now]

print(*cnt[1:])

Submission status: Wrong Answer

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

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

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

Counter = [0] * N

import sys
sys.setrecursionlimit(2 * (10 ** 5))

def add_counter(p, x):
  global Tree, Counter
  Counter[p] += x
  if len(Tree[p]) == 0: return
  for child in Tree[p]:
    add_counter(child, x)

for act in Action:
  add_counter(act[0], act[1])

ans = ' '.join([str(n) for n in Counter])
print(ans)

Submission status: Wrong Answer

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

for _ in range(q):
    p,x = map(int,input().split())
    arr[p-1] += x
    for i in range(2*p,len(arr)+1):
        arr[i-1] += x

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

Submission status: Wrong Answer

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

for _ in range(q):
    p,x = map(int,input().split())
    arr[p-1] += x
    for i in range(2*p,len(arr)+1):
        arr[i-1] += x

print(arr)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ab_list=[list(map(int, input().split())) for _ in range(n-1)]
c_list=[0]*n
px_list=[list(map(int, input().split())) for _ in range(q)]

for px in px_list:
  c_list[px[0]-1] += px[1]

for ab in ab_list:
  c_list[ab[1]-1] += c_list[ab[0]-1]

print(c_list)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
Graph = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    Graph[a].append(b)
    Graph[b].append(a)

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

# 各頂点に counter を持たせて，幅優先探索で足していく
dist = [-1 for _ in range(N)]
que = deque()
que.append(0)
while que:
    v = que[0]
    que.popleft()
    for new_v in Graph[v]:
        if dist[new_v] != -1:
            continue
        dist[new_v] = 1
        que.append(new_v)
        counter_list[new_v] += counter_list[v]

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

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
Graph = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    Graph[a].append(b)

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

# 各頂点に counter を持たせて，幅優先探索で足していく
dist = [-1 for _ in range(N)]
que = deque()
que.append(0)
while que:
    v = que[0]
    que.popleft()
    for new_v in Graph[v]:
        if dist[new_v] != -1:
            continue
        dist[new_v] = 1
        que.append(new_v)
        counter_list[new_v] += counter_list[v]

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