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 i in range(N-1)]
px=[list(map(int,input().split())) for i in range(Q)]

c=[0 for i in range(N)]

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

for i in range(N-1):
    c[ab[i][1]-1]+=c[ab[i][0]-1]

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

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(N-1)]
px=[list(map(int,input().split())) for i in range(Q)]

c=[0 for i in range(N)]

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

ab.sort()

for i in range(N-1):
    c[ab[i][1]-1]+=c[ab[i][0]-1]

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

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)]

vs=[0]*n
for p,x in px:
  vs[p-1] += x
  
for a,b in sorted(ab):
  vs[b-1] += vs[a-1]  

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

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)]

vs=[0]*n
for p,x in px:
  vs[p-1] += x
  
for a,b in ab:
  vs[b-1] += vs[a-1]  

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

Submission status: Wrong Answer

Solution code:
from collections import deque

#n,q=4,3
#ab=[[1,2],[2,3],[2,4]]
#px=[[2,10],[1,100],[3,1]]
n,q=map(int, input().split())
#ab=[list(map(int, input().split())) for i in range(n-1)]
#ab=sorted(ab,key=lambda x:(x[0],x[1]))
#max_n=2*10**5
t=[[] for i in range(n+1)]
ans=[0 for i in range(n+1)]
for i in range(n-1):
    ab=list(map(int, input().split()))
    t[ab[0]].append(ab[1])
d=deque([])
for i in range(q):
    px=list(map(int, input().split()))
    root = px[0]
    x = px[1]
    ans[root]+=x
    while(1):#再帰呼び出しをwhile
        if len(t[root])!=0:
            for j in range(len(t[root])):
                ans[t[root][j]] += x
                d.append(t[root][j])
        if len(d)==0:
            break
        root=d.popleft()

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque

#n,q=4,3
#ab=[[1,2],[2,3],[2,4]]
#px=[[2,10],[1,100],[3,1]]
n,q=map(int, input().split())
ab=[list(map(int, input().split())) for i in range(n-1)]
ab=sorted(ab,key=lambda x:(x[0],x[1]))
#max_n=2*10**5
t=[[] for i in range(n+1)]
ans=[0 for i in range(n+1)]
for i in range(len(ab)):
    t[ab[i][0]].append(ab[i][1])
d=deque([])
for i in range(q):
    px=list(map(int, input().split()))
    root = px[0]
    x = px[1]
    ans[root]+=x
    while(1):#再帰呼び出しをwhile
        if len(t[root])!=0:
            for j in range(len(t[root])):
                ans[t[root][j]] += x
                d.append(t[root][j])
        if len(d)==0:
            break
        root=d.popleft()

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
#Ki -D-
from collections import deque


n,q=map(int,input().split())
data=[deque([]) for i in range(n+1)]

for i in range(n-1):
    a,b=map(int,input().split())
    data[a].append(b)
    
s=deque([0 for i in range(n+1)])

for i in range(q):
    p,x=map(int,input().split())
    s[p]=s[p]+x
    
for i in range(1,n+1):
    while data[i]:
        s[data[i][0]]=s[data[i][0]]+s[i]
        data[i].popleft()

s.popleft()
print(*s)

Submission status: Wrong Answer

Solution code:
#Ki -D-
from collections import deque


n,q=map(int,input().split())
data=[deque([]) for i in range(n+1)]

for i in range(n-1):
    a,b=map(int,input().split())
    data[a].append(b)
    
s=[0]*(n+1)

for i in range(q):
    p,x=map(int,input().split())
    s[p]=s[p]+x
    
for i in range(1,n+1):
    while data[i]:
        s[data[i][0]]=s[data[i][0]]+s[i]
        data[i].popleft()
        
a=""

for i in range(1,n+1):
    a+=str(s[i])+" "
    
print(a)

Submission status: Wrong Answer

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

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

    deq = deque([0])
    visited = [False] * N
    while deq:
        parent = deq.popleft()
        link = links[parent]

        for v in link:
            if not visited[v]:
                ans[v] += ans[parent]
                deq.append(v)

        visited[parent] = True

    print(*ans)

Submission status: Wrong Answer

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

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

    deq = deque([1])
    visited = [False] * (N + 1)
    while deq:
        parent = deq.popleft()
        link = links[parent]

        for v in link:
            if not visited[v]:
                ans[v] += ans[parent]
                deq.append(v)

        visited[parent] = True

    print(*ans[1:])

Submission status: Wrong Answer

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

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

l = [0] * (n+1)

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

for i,j in tree.items():
  for k in j:
    l[k] += l[i]

print(*l[1:])

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(n - 1)]
ls.sort(key=lambda x: x[0])
ps = [0] * (n + 1)
for _ in range(q):
    p, v = map(int, input().split())
    ps[p] += v
for a, b in ls:
    ps[b] += ps[a]
print(*ps[1:])

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(n - 1)]
ls.sort(key=lambda x: (x[0], x[1]))
ps = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    ps[p - 1] += x
for a, b in ls:
    ps[b - 1] += ps[a - 1]
print(*ps)

Submission status: Wrong Answer

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

num_list = [0] * (N)
a_b_list = []

for _ in range(N-1):
    a_b_list.append(list(map(int, input().split())))

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

for a_b in a_b_list:
    num_list[a_b[1]-1] += num_list[a_b[0]-1]

print(*num_list)

Submission status: Wrong Answer

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

num_list = [0] * (N+1)
tree_list = [[i] for i in range(N+1)]

for _ in range(N-1):
    a, b = map(int, input().split(' '))
    for i, t in enumerate(tree_list):
        if i == 0:
            continue
        if a in t:
            tree_list[i].append(b)

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

num_list.pop(0)
print(*num_list)

Submission status: Wrong Answer

Solution code:
import math
import sys
import queue
from collections import Counter
from itertools import accumulate
from fractions import gcd
from functools import reduce
 
 
def lcm(a, b):
    return a * b // gcd(a, b)
 
def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
 
def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)
 
sys.setrecursionlimit(10000)
 
mod = 1000000007
 
"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654
 
# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
 
# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())
 
## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""
 
 
class Tree:
    def __init__(self):
        self.child = []
        self.count = 0
 
if __name__ == "__main__":
    N, Q = map(int, sys.stdin.readline().split())
    node_list = [Tree() for _ in range(N + 1)]
 
    for _ in range(N - 1):
        a, b = map(int, sys.stdin.readline().split())
        node_list[a].child.append(b)
 
    for _ in range(Q):
        p, x = map(int, sys.stdin.readline().split())
        node_list[p].count += x
 
    ans = []
    for n in node_list[1::]:
        ans.append(n.count)
        for nc in n.child:
            node_list[nc].count += n.count
    print(*ans)

Submission status: Wrong Answer

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

E = []
for _ in range(N - 1):
    E.append(tuple(map(lambda x: int(x) - 1, input().split())))
E.sort()

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

for i, j in E:
    counter[j] += counter[i]

for i, c in enumerate(counter):
    if i < N - 1:
        print(c,end=' ')
    else:
        print(c)

Submission status: Wrong Answer

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

E = []
for _ in range(N - 1):
    E.append(tuple(map(lambda x: int(x) - 1, input().split())))
E.sort()

root = [{i} for i in range(N)]
for i, j in E:
    root[j] |= root[i]

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

for i, c in enumerate(counter):
    if i < N - 1:
        print(c,end=' ')
    else:
        print(c)

Submission status: Wrong Answer

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

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

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

queue = [[1, 0]]
while len(queue) > 0:
  q = queue.pop(0)
  if q[0] != 0:
    cnt[q[0]] += cnt[q[1]]
  for i in to[q[0]]:
    if i != q[1]:
      queue.append([i, q[0]])

for i in range(1, N):
  print(cnt[i])

Submission status: Wrong Answer

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

N, Q = map(int, sys.stdin.readline().split())
list_ab = [list(map(int,list(sys.stdin.readline().split()))) for i in range(N-1)]
list_px = [list(map(int,list(sys.stdin.readline().split()))) for i in range(Q)]

# print(list_ab)
# print(list_ab)

# print(list_ab.sort(key=lambda x:x[0]))
# print(sorted(list_ab, key=lambda x:x[0]))
list_ab = sorted(list_ab, key=lambda x:x[0])
list_ab = sorted(list_ab, key=lambda x:x[1])
# print(list_ab)
# print(list_px)
count = [0]*N

for i in range(Q):
    p,x = list_px[i]
    count[p-1] += x

for i in range(N-1):
    a,b = list_ab[i]
    count[b-1] += count[a-1]
    # if():


# print(count)
print(*count[0:])

# # list_t = [range(1, N)]
# # print(list_t)
# list_t = []
# for i in range(N):
#     list_t.append([i+1])
# # print(list_t)

# for i in range(N):
#     if(list_t[i]):



# class Node:
#     def __init__(self, label):
#         self.label = label
#         self.left = None
#         self.right = None
#         self.counter = 0

# root = Node(1)

# print(root.label)

Submission status: Wrong Answer

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

N, Q = map(int, sys.stdin.readline().split())
list_ab = [list(map(int,list(sys.stdin.readline().split()))) for i in range(N-1)]
list_px = [list(map(int,list(sys.stdin.readline().split()))) for i in range(Q)]

# print(list_ab)
# print(list_ab)

# print(list_ab.sort(key=lambda x:x[0]))
# print(sorted(list_ab, key=lambda x:x[0]))
list_ab = sorted(list_ab, key=lambda x:x[0])
# print(list_ab)
# print(list_px)
count = [0]*N

for i in range(Q):
    p,x = list_px[i]
    count[p-1] += x

for i in range(N-1):
    a,b = list_ab[i]
    count[b-1] += count[a-1]
    # if():


# print(count)
print(*count[0:])

# # list_t = [range(1, N)]
# # print(list_t)
# list_t = []
# for i in range(N):
#     list_t.append([i+1])
# # print(list_t)

# for i in range(N):
#     if(list_t[i]):



# class Node:
#     def __init__(self, label):
#         self.label = label
#         self.left = None
#         self.right = None
#         self.counter = 0

# root = Node(1)

# print(root.label)

Submission status: Wrong Answer

Solution code:
NQ = input()
NQs = NQ.split(" ")
N = int(NQs[0])
Q = int(NQs[1])
values = []
for i in range(N):
    values.append(0)
ABs = []
PQs = []

def increment(PQs, ABs, values, x):
    for j in ABs:
        if int(j[0]) == x:
            values[(int(j[1])-1)] = values[(int(j[1])-1)] + int(i[1])
            increment(PQs, ABs, values, int(j[1]))
      
            
    
for i in range(N-1):
    temp = input()
    temps = temp.split(" ")
    ABs.append(temps)
    
for i in range(Q):
    temp = input()
    temps = temp.split(" ")
    PQs.append(temps)

for i in PQs:
    x = int(i[0])
    values[x-1] = values[x-1] + int(i[1])
    increment(PQs, ABs, values, x)

output = ""
realOutput = ""
for i in values:
    output = output + str(i) + " "
for i in range(len(output)-1):
    realOutput = realOutput + output[i]
print(realOutput)

Submission status: Wrong Answer

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

N, Q = map(int, sys.stdin.readline().split())
list_ab = [list(map(int,list(sys.stdin.readline().split()))) for i in range(N-1)]
list_px = [list(map(int,list(sys.stdin.readline().split()))) for i in range(Q)]

# print(list_ab)
# print(list_px)
count = [0]*N

for i in range(Q):
    p,x = list_px[i]
    count[p-1] += x

for i in range(N-1):
    a,b = list_ab[i]
    count[b-1] += count[a-1]
    # if():


# print(count)
print(*count[0:])

# # list_t = [range(1, N)]
# # print(list_t)
# list_t = []
# for i in range(N):
#     list_t.append([i+1])
# # print(list_t)

# for i in range(N):
#     if(list_t[i]):



# class Node:
#     def __init__(self, label):
#         self.label = label
#         self.left = None
#         self.right = None
#         self.counter = 0

# root = Node(1)

# print(root.label)

Submission status: Wrong Answer

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

N, Q = map(int, sys.stdin.readline().split())
list_ab = [list(map(int,list(sys.stdin.readline().split()))) for i in range(N-1)]
list_px = [list(map(int,list(sys.stdin.readline().split()))) for i in range(Q)]

print(list_ab)
print(list_px)
count = [0]*N

for i in range(Q):
    p,x = list_px[i]
    count[p-1] += x

for i in range(N-1):
    a,b = list_ab[i]
    count[b-1] += count[a-1]
    # if():


# print(count)
print(*count[0:])

# # list_t = [range(1, N)]
# # print(list_t)
# list_t = []
# for i in range(N):
#     list_t.append([i+1])
# # print(list_t)

# for i in range(N):
#     if(list_t[i]):



# class Node:
#     def __init__(self, label):
#         self.label = label
#         self.left = None
#         self.right = None
#         self.counter = 0

# root = Node(1)

# print(root.label)

Submission status: Wrong Answer

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

N, Q = map(int, sys.stdin.readline().split())
list_ab = [list(map(int,list(sys.stdin.readline().split()))) for i in range(N-1)]
list_px = [list(map(int,list(sys.stdin.readline().split()))) for i in range(Q)]

print(list_ab)
print(list_px)
count = [0]*N

for i in range(Q):
    p,x = list_px[i]
    count[p-1] += x

for i in range(N-1):
    a,b = list_ab[i]
    count[b-1] += count[a-1]
    # if():


print(count)

# # list_t = [range(1, N)]
# # print(list_t)
# list_t = []
# for i in range(N):
#     list_t.append([i+1])
# # print(list_t)

# for i in range(N):
#     if(list_t[i]):



# class Node:
#     def __init__(self, label):
#         self.label = label
#         self.left = None
#         self.right = None
#         self.counter = 0

# root = Node(1)

# print(root.label)

Submission status: Wrong Answer

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

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

EDGES = []

for i in range(N-1):
    parent,child = list(map(int, input().split()))
    EDGES.append((parent,child))

EDGES.sort(key=lambda e: e[0])

COUNTER= {}
for i in range(1,N+1):
    COUNTER[i] = 0

for _ in range(Q):
    i,value = list(map(int, input().split()))
    COUNTER[i] += value

for e in EDGES:
    parent, child = e
    COUNTER[child] += COUNTER[parent]

V = [str(COUNTER[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

import sys
#sys.setrecursionlimit(200000)

input = sys.stdin.readline

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

# 2147483647
#  123456789
#P = array('L', [0]*(N+1))
P= {}
for i in range(1,N+1):
    P[i] = 0

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

# accumulate

#def accumulate(i):
#    children = A[i]
#    value = P[i]
#    for c in children:
#        P[c] += value
#        accumulate(c)
#
#accumulate(1)

for i in range(1,N+1):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
    
V = [str(P[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

import sys
#sys.setrecursionlimit(200000)

input = sys.stdin.readline

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

# 2147483647
#  123456789
P = array('L', [0]*(N+1))
for i in range(Q):
    p,x = list(map(int, input().split()))
    P[p] += x

# accumulate

#def accumulate(i):
#    children = A[i]
#    value = P[i]
#    for c in children:
#        P[c] += value
#        accumulate(c)
#
#accumulate(1)

for i in range(1,N+1):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
    
V = [str(P[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Wrong Answer

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

N,Q = map(int,input().split())
a = []
b = []
p = []
x = []
tmp_a = 0
tmp_b = 0
tmp_p = 0
tmp_x = 0


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

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

nodes = []

def search_node(p):
    for k in range(len(a)):
        if p == a[k]:
            nodes.append(b[k])
            search_node(b[k])
    
    return nodes

counter = [0]*N

for i in range(Q):
    nodes = []
    nodes = search_node(p[i])
    nodes.append(p[i])
    for j in nodes:
        counter[j-1] = counter[j-1] + x[i]

out = ""
for term in counter:
    out = out + str(term) + " "

print(out.strip())

Submission status: Wrong Answer

Solution code:
import numpy as np

N,Q=input().split()

N=int(N)
Q=int(Q)

z=[0]*(N-1)

for i in range(N-1):
    A,B=input().split()
    A=int(A)
    B=int(B)
    z[B-2]=A

w=[0]*N


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

i=0
for t in z:
    i+=1
    w[i]+=w[t-1]
    
print(' '.join(map(str, w)))

Submission status: Wrong Answer

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

input_node_list=[[i] for i in range(N)]

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

#print(input_node_list)

#node_listをすべての子を含む形式に書き換える
#node_listすべてに対して、子ども側から処理
for i in range(N):
    #print("########################################")
    #print(input_node_list[N-1-i])
    
    #子どもの中身すべてに対して実行
    for j in range(len(input_node_list[N-1-i])):
        #print(input_node_list[N-1-i][j])
        
        #子どもにさらに子供がいる場合の処理
        if N-1-i!=input_node_list[N-1-i][j]:
            #print(input_node_list[input_node_list[N-1-i][j]])
            
            for k in range(len(input_node_list[input_node_list[N-1-i][j]])):
                if input_node_list[N-1-i][j]!=input_node_list[input_node_list[N-1-i][j]][k]:
                    input_node_list[N-1-i].append(input_node_list[input_node_list[N-1-i][j]][k])
                
ans = [0]*N

for i in range(Q):
    P,X = list(map(int, input().split()))
    
    for j in range(len(input_node_list[P-1])):
        ans[input_node_list[P-1][j]]+=X
        
print(' '.join(map(str, ans)))

Submission status: Wrong Answer

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

for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a].append(b)
    edges[b].append(a)
for _ in range(Q):
    p, x = map(int, input().split())
    count[p-1] += x

while(q):
    k = q.popleft()
    count[k[0]-1] += count[k[1]-1]
    for i in edges[k[0]]:
        if not i == k[1]:
            q.append((i, k[0]))

print(*count)

Submission status: Wrong Answer

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

for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a].append(b)
for _ in range(Q):
    p, x = map(int, input().split())
    counter[p] += x

while(q):
    k = q.popleft()
    counter[k[0]] += counter[k[1]]
    for i in edges[k[0]]:
        if not i == k[1]:
            q.append((i, k[0]))

for i in range(1, n+1):
    print(counter[i], end=" ")

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

import sys
sys.setrecursionlimit(200000)

input = sys.stdin.readline

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

P = array('L', [0]*(N+1))
for i in range(Q):
    p,x = list(map(int, input().split()))
    P[p] += x

# accumulate

def accumulate(i):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
        accumulate(c)

accumulate(1)
V = [str(P[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

import sys
sys.setrecursionlimit(100000)

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

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

# accumulate

def accumulate(i):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
        accumulate(c)

accumulate(1)
V = [str(P[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict
n, Q = map(int, input().split())
edges = defaultdict(list)
plus = defaultdict(int)
counter = [0]*(n+1)
q = deque()
q.append((1, 0))

def dfs(q, counter):
    k = q.popleft()
    counter[k[0]] = counter[k[1]]
    if k[0] in plus.keys():
        counter[k[0]] += plus[k[0]]
    for i in edges[k[0]]:
        if not i == k[1]:
            q.append((i, k[0]))


for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a].append(b)
for _ in range(Q):
    p, x = map(int, input().split())
    plus[p] += x

while(q):
    dfs(q, counter)

for i in range(1, n+1):
    print(counter[i], end=" ")

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n,q = inpl()
ki = [[] for i in range(n)]
ans = [0] * n
for i in range(n-1):
    a,b = inpl()
    ki[a-1].append(b)
for i in range(q):
    p,x = inpl()
    ans[p-1] += x

st = [1]
while st:
    tmp = st.pop()
    for j in ki[tmp-1]:
        ans[j-1] += ans[tmp-1]
    st += ki[tmp-1]
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n,q = inpl()
ki = [[] for i in range(n)]
ans = [0] * n
for i in range(n-1):
    a,b = inpl()
    ki[a-1].append(b)
for i in range(q):
    p,x = inpl()
    ans[p-1] += x

st = [1]
while st:
    tmp = st.pop()
    for j in ki[tmp-1]:
        ans[j-1] += ans[tmp-1]
    st += ki[tmp-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N , Q = map(int,input().split())
counter = [0 for _  in range( Q + 10)]
tree = []
for _ in range(N - 1):
    tree.append( tuple(map(int,input().split())))
tree = sorted(tree)
for _ in range(Q):
    a , b = map(int,input().split())
    counter[a] += b

for x in tree:
    counter[x[1]] += counter[x[0]]
for i in range(1,N+1):
    print(counter[i],end = ' ')

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

import sys
sys.setrecursionlimit(100000)

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

P = array('l', [0]*(N+1))
for i in range(Q):
    p,x = list(map(int, input().split()))
    P[p] += x

# accumulate

def accumulate(i):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
        accumulate(c)

accumulate(1)
V = [str(P[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, Q=map(int, input().split())
 
edge=[list(map(int, input().split())) for i in range(N-1)]
adds=[list(map(int, input().split())) for i in range(Q)]

point=[0]*(N+1)
ans=[0]*(N+1)

for i in range(Q):
  point[adds[i][0]]+=adds[i][1]
  
edge=np.array(edge)
edge=edge[np.argsort(edge[:, 0])]
for i in range(N-1):
  point[edge[i, 1]]+=point[edge[i, 0]]
  
print(' '.join(map(str, map(int, point[1:]))))