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:
class Node:
    def __init__(self, v):
        self.__value = v
        self.counter = 0
        self.children = []


tree_map = {}
n, q = map(int, input().split())
for i in range(1, n + 1):
    tree_map[i] = Node(i)

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

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

operations = [[tree_map[1], 0]]
while len(operations) > 0:
    node, v = operations.pop()
    node.counter += v
    for c in node.children:
        operations.append([c, node.counter])

counters = [str(tree_map[i].counter) for i in range(1, n + 1)]
print(" ".join(counters))

Submission status: Wrong Answer

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

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

value = [0]*(N+1)

for i in range(Q):
  p, x = map(int, input().split())
  value[p] += x
  
def dfs(v, parent):  
  for i in tree[v]:
    if i == parent:
      continue
    value[i] += value[v]
    dfs(i, v)
    
dfs(1, 0)  
print(*value[1:])

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, v):
        self.__value = v
        self.counter = 0
        self.children = []


tree_map = {}
n, q = map(int, input().split())
for i in range(1, n + 1):
    tree_map[i] = Node(i)

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

operations = {}
for i in range(q):
    p, x = map(int, input().split())
    if p not in operations:
        operations[p] = x
    else:
        operations[p] += x

for p, x in operations.items():
    nodes = [tree_map[p]]
    while len(nodes) > 0:
        node = nodes.pop()
        node.counter += x

        for c in node.children:
            nodes.append(c)

counters = [str(tree_map[i].counter) for i in range(1, n + 1)]
print(" ".join(counters))

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]

value = [0]*(N+1)
for i in range(Q):
  p, x = map(int, input().split())
  value[p] += x
  
for root, reaf in tree.items():
  for i in reaf:
    value[i] += value[root]

print(*value[1:])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

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

value = [0]*(N+1)
for i in range(Q):
  p, x = map(int, input().split())
  value[p] += x
  
for root, reaf in tree.items():
  for i in reaf:
    if root == i:
      continue
    value[i] += value[root]

print(*value[1:])

Submission status: Wrong Answer

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

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


ans = [0]*N
for i in range(N):
  if i ==0:
    ans[i] = count_list[i]
  else:
    ans[i] = count_list[i] + ans[tree_list[i]]
ans = [str(i) for i in ans]
print(" ".join(ans))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
parent = [0]*(N+1)
for n in range(N-1):
  a,b = map(int,input().split())
  parent[b] = a  
c = [0]*(N+1)  

for q in range(Q):
  p,x = map(int,input().split())
  c[p] += x
  
for i in range(1,N+1):
  c[i] += c[parent[i]]

c = c[1:]
print(" ".join(map(str,c)))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
parent = [0]*(N+1)
for n in range(N-1):
  a,b = map(int,input().split())
  parent[b] = a  
c = [0]*(N+1)  

for q in range(Q):
  p,x = map(int,input().split())
  c[p] += x
  
for i in range(1,N+1):
  c[i] += c[parent[i]]

print(" ".join(map(str,c[1:])))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ki = [0]*(N+1)
for n in range(N-1):
  a,b = map(int,input().split())
  ki[b] = a  
c = [0]*(N+1)  

for q in range(Q):
  p,x = map(int,input().split())
  c[p] += x
  
for i in range(1,N+1):
  c[i] += c[ki[i]]

print(" ".join(map(str,c[1:])))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ki = [0]*(N+1)
for n in range(N-1):
  a,b = map(int,input().split())
  ki[b] = a
  
c = [0]*(N+1)  

for q in range(Q):
  p,x = map(int,input().split())
  c[p] += x
  
for i in range(1,N+1):
  c[i] += c[ki[i]]

print(" ".join(map(str,c[1:])))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ki = [0]*(N+1)

for n in range(N-1):
  a,b = map(int,input().split())
  ki[b] = a
  
c = [0]*(N+1)  

for q in range(Q):
  p,x = map(int,input().split())
  c[p] += x
  
for i in range(1,N+1):
  c[i] += c[ki[i]]

print(" ".join(map(str,c[1:])))

Submission status: Wrong Answer

Solution code:
NQ = input().split(' ')
N = int(NQ[0])
Q = int(NQ[1])
out_put_list = [0] * N
tree_list = {}
for i in range(N):
    tree_list[i] = []

for i in range(N-1):
    ab = input().split(' ')
    a = int(ab[0]) -1
    b = int(ab[1]) -1
    tree_list[a].append(b)

for _ in range(Q):
    px = input().split(' ')
    p = int(px[0])
    x = int(px[1])
    out_put_list[p-1] += x

for i in range(len(tree_list)):
    for num in tree_list[i]:
        out_put_list[num] += out_put_list[i]

print(*out_put_list)

Submission status: Wrong Answer

Solution code:
import collections
 
n,q=map(int,input().split())
a=sorted(list(list(map(int,input().split())) for i in range(n-1)))
p=collections.defaultdict(int)
for i in range(q):
  k,v=map(int,input().split())
  p[k]+=v
 
li=[0]*n
li[0]+=p[1]
for i in range(n-1):
  li[a[i][1]-1]+=li[a[i][0]-1]+p[a[i][1]]
print(*li)

Submission status: Wrong Answer

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

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

	AB.sort()

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

	for ab in AB:
		cost[ab[1]-1] += cost[ab[0]-1]
	print(*cost)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
  N, Q = map(int, input().split())
  child_per = {1:0}
  visited = [False] * (N+1)
  graph = {i:deque() for i in range(N+1)}

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

  visited[1] = True
  q = [1]
  while q:
    per = q.pop()
    for child in graph[per]:
      if not visited[child]:
        visited[child] = True
        child_per[child] = per
        q.append(child)

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

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

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
lis = [[i+1] for i in range(n)]
for i in range(n-1):
  a,b = map(int,input().split())
  for j in lis:
    if a in j:
      j.append(b)

ans = [0]*n
for i in range(q):
  p,x = map(int,input().split())
  for k in lis:
    if p == k[0]:
      for l in k:
        ans[l-1] += x

ans_str = [str(num) for num in ans]
        
print(' '.join(ans_str))

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, v):
        self.__value = v
        self.counter = 0
        self.children = []


tree_map = {}
n, q = map(int, input().split())
for i in range(1, n + 1):
    tree_map[i] = Node(i)

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

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

    nodes = [tree_map[p]]
    while len(nodes) > 0:
        node = nodes.pop()
        node.counter += x

        for c in node.children:
            nodes.append(c)

counters = [str(tree_map[i].counter) for i in range(1, n + 1)]
print(" ".join(counters))

Submission status: Wrong Answer

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

N,Q=map(int,input().split())
A=[list(map(int,input().split())) for _ in range(N-1)]

A.sort()

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

for a in A:
	cost[a[1]-1] += cost[a[0]-1]
print(*cost)

Submission status: Wrong Answer

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

rl = [0]*n
al = [0]*n

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


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

for k in range(n):
    if k == 0:
        pass
    else:
        al[k] += al[rl[k]]
        print(al)
print(*al)

Submission status: Wrong Answer

Solution code:
import sys

N,Q=map(int,input().split())
A=[list(map(int,input().split())) for _ in range(N-1)]

A.sort()

cost=[0]*N
P=[list(map(int,input().split())) for _ in range(Q)]
for q in range(Q):
	cost[P[q][0]-1]+=P[q][1]

for a in A:
	cost[a[1]-1] += cost[a[0]-1]
print(*cost)

Submission status: Wrong Answer

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

A.sort()

cost=[0]*N

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

for a in A:
	cost[a[1]-1] += cost[a[0]-1]
print(*cost)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
tree_list  = {i:[] for i in range(1,n+1)}
answer_list = [0]
for i in range(1,n):
    a,b = map(int,input().split())
    tree_list[a].append(b)
    tree_list[b].append(a)
    answer_list.append(0)
for i in range(m):
    num1,num2 = map(int,input().split())
    answer_list[num1-1] +=  num2
for i in range(1,n+1): 
    for num in tree_list[i]:
        if i < num:
            answer_list[num-1] += answer_list[i-1]
for i in answer_list:
    print(i)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
tree_list  = {i:[] for i in range(1,n+1)}
answer_list = [0]
for i in range(1,n):
    a,b = map(int,input().split())
    tree_list[a].append(b)
    answer_list.append(0)
for i in range(m):
    num1,num2 = map(int,input().split())
    answer_list[num1-1] +=  num2
for i in range(1,n+1): 
    for num in tree_list[i]:
        answer_list[num-1] += answer_list[i-1]
for i in answer_list:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, q = map(int, input().split())
G = [[] for yy in range(n)]
count = [0]*n
flag=[False]*n
stack = deque([0])

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

stack.append(0)
while stack:
  v = stack.pop()
  flag[v] = True
  for e in G[v]:
    if flag[e]:
      continue
    count[e] += count[v]
    stack.append(e)

print(*count)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, Q = map(int, input().split())
graph = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    graph[a].append(b)

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

for i in range(N):
    for v in graph[i]:
        p[v] += p[i]
#print(p)

visit = [str(i) for i in p]
print(' '.join(visit))

Submission status: Wrong Answer

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

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

child = [[] for i in range(N)]
visited = [False for i in range(N)]
def bfs(v):
    Q = []
    visited[v] = True
    Q.append(v)
    while len(Q) > 0:
        u = Q.pop(0)
        for v in graph[u]:
            if visited[v] == False:
                visited[v] = True
                child[u].append(v)
                Q.append(v)
bfs(0)
#print(child)

for i in range(N):
    for v in child[i]:
        p[v] += p[i]
#print(p)

visit = [str(i) for i in p]
print(' '.join(visit))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n-1)]
pq = [list(map(int,input().split())) for i in range(q)]

c = [0]*(n+1)
point = [[] for _ in range(n+1)]

for i in range(q):
  c[pq[i][0]] += pq[i][1]
for i in range(n-1):
  point[ab[i][0]].append(ab[i][1])

done = set()
done.add(1)
lst = [1]
while lst:
  start = lst.pop()
  for i in point[start]:
    if i not in done:
      c[i] += c[start]
      done.add(i)
      lst.append(i)
for i in c[1:]:
  print(i,end=" ")

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
tree_list  = []
answer_list = [0]
for i in range(n-1):
    tree_list.append(list(map(int,input().split())))
    answer_list.append(0)
for i in range(m):
    num1,num2 = map(int,input().split())
    answer_list[num1-1] +=  num2
for i in range(n): 
    true = True
    while true:
        true = False
        for _ in tree_list:
            if i == _[0]-1:
                answer_list[_[1]-1] += answer_list[i]
                tree_list.remove(_)
                true = True
for i in answer_list:
    print(i)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
tree_list  = []
answer_list = [0]
for i in range(n-1):
    tree_list.append(list(map(int,input().split())))
    answer_list.append(0)
for i in range(m):
    num1,num2 = map(int,input().split())
    answer_list[num1-1] +=  num2
for i in range(n): 
    true = True
    while true:
        true = False
        print(i)
        for _ in tree_list:
            if i == _[0]-1:
                answer_list[_[1]-1] += answer_list[i]
                tree_list.remove(_)
                true = True
for i in answer_list:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a=[0]*n
for i in range(q):
    b,c=map(int,input().split())
    a[b-1]+=c
    
d=deque([0])
f=[0]*n
f[0]=1
while d:
    p=d.pop()
    f[p]=1
    for c in tr[p]:
        if f[c]==0:
            continue
        a[c]+=a[p]
        d.append(c)
print(*a)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a=[0]*n
for i in range(q):
    b,c=map(int,input().split())
    a[b-1]+=c
    
d=deque([0])
f=[0]*n
f[0]=1
while len(d)>0:
    p=d.pop()
    f[p]=1
    for c in tr[p]:
        if f[c]==0:
            continue
        a[c]+=a[p]
        d.append(c)
print(*a)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tr=[[] for i in range(n)]
for i in range(n-1):
    b,c=map(int,input().split())
    tr[b-1].append(c-1)

a=[0]*n
for i in range(q):
    b,c=map(int,input().split())
    a[b-1]+=c
    
d=[0]
while len(d)>0:
    p=d.pop(0)
    for c in tr[p]:
        a[c]+=a[p]
        d.append(c)
print(*a)

Submission status: Wrong Answer

Solution code:
#親ー子の順に書かれてるか否かめっちゃ重要なのになあ
n,q = map(int,input().split())
peer = [[] for _ in range(n)]
for i in range(n-1):
  a,b = (map(int,input().split()))
  a -= 1
  b -= 1
  peer[a].append(b)
  peer[b].append(a)
mom = [-1 for _ in range(n)]
mom[0] = 'luca'
child = [[] for _ in range(n)]
now = [0]
while now != []:
  last = now
  now = []
  for x in last:
    for y in peer[x]:
      if mom[y] == -1:
        now.append(y)
        mom[y] = x
        child[x].append(y)
print(peer)
print(mom)
print(child)
pq = [0 for _ in range(n)]
for i in range(q):
  p,x = map(int,input().split())
  p -= 1
  pq[p] += x
now = [0]
ans = pq
while now != []:
  last = now
  now = []
  for x in last:
    for y in child[x]:
      ans[y] += ans[x]
    now += child[x]
print(' '.join(list(map(str,ans))))

Submission status: Wrong Answer

Solution code:
#実はa<bだったらしい
n,q = map(int,input().split())
child = [[] for _ in range(n)]
for i in range(n-1):
  a,b = (map(int,input().split()))
  a -= 1
  b -= 1
  child[a].append(b)
#print(child)
pq = [0 for _ in range(n)]
for i in range(q):
  p,x = map(int,input().split())
  p -= 1
  pq[p] += x
now = [0]
ans = pq
while now != []:
  last = now
  now = []
  for x in last:
    now += child[x]
    for y in child[x]:
      ans[y] += ans[x]
print(' '.join(list(map(str,ans))))

Submission status: Wrong Answer

Solution code:
#実はa<bだったらしい
n,q = map(int,input().split())
child = [[] for _ in range(n)]
for i in range(n-1):
  a,b = (map(int,input().split()))
  a -= 1
  b -= 1
  child[a].append(b)
#print(child)
pq = [0 for _ in range(n)]
for i in range(q):
  p,x = map(int,input().split())
  p -= 1
  pq[p] += x
now = [0]
ans = pq
while now != []:
  last = now
  now = []
  for x in last:
    now += child[x]
    for y in child[x]:
      ans[y] += ans[x]
print(ans)

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]

    score = [0] * n

    for qq in q_data:
        index = qq[0]
        if index > 1:
            score[index - 1] += qq[1]
            add_score(score, qq, n_data, index)
        else:
            for i in range(n):
                score[i] += qq[1]

    score = [str(s) for s in score]
    s = " ".join(score)

    return s


def add_score(score, qq, n_data, index):
    for nn in n_data:
        if nn[0] == index:
            score[nn[1] - 1] += qq[1]
            add_score(score, qq, n_data, nn[1])


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)

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

class Node:
    def __init__(self):
        self.children = set()
        self.point = 0
    
    def get_children(self):
        if not self.children:
            return set([self])
        ret = set([self])
        for child in self.children:
            c = child.get_children()
            ret = ret.union(c)
        return ret

nodes = [Node() for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    nodes[a-1].children.add(nodes[b-1])

queries = []
for i in range(q):
    p, x = map(int, input().split(' '))
    queries.append((p, x))

for query in queries:
    ps = nodes[query[0]-1].get_children()
    for p in ps:
        p.point += query[1]

print(*[str(n.point) for n in nodes])

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]

    score = [0] * n

    for qq in q_data:
        index = qq[0]
        score[index - 1] += qq[1]
        add_score(score, qq, n_data, index)

    score = [str(s) for s in score]
    s = " ".join(score)

    return s


def add_score(score, qq, n_data, index):
    for nn in n_data:
        if nn[0] == index:
            score[nn[1] - 1] += qq[1]
            add_score(score, qq, n_data, nn[1])


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

Solution code:
import math
import numpy as np
import sys
input = sys.stdin.readline

#横並び
#a, b, c, x = map(int, [input() for i in range(4)])
#z = list(map(int, input().split()))
#n, a, b = map(int, input().split())

#print(len(z))
#for i in z:
#    print(i)

#a = sorted(a,reverse = True)
#print(sum(a[::2]) - sum(a[1::2]))
#z = map(int, [input() for i in range(int(n))])
#data_list = [ list(map(int,input().split(" "))) for i in range(3)]


n,q = map(int, input().split())
a = [ list(map(int,input().split())) for i in range(n-1)]
x = [ list(map(int,input().split())) for i in range(q)]
a.sort()
x.sort()
y = [0] * n
for i in x:
    y[i[0]-1] += i[1]

for i in a:
    y[i[1]-1] += y[i[0]-1]


print(*y)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(pow(10, 6))
input = sys.stdin.readline
 
def dfs(i, d):
  visited[i] = True
  ans[i] = d + pp[i]
  for j in edges[i]:
    if not visited[j]:
      dfs(j, d + pp[i])
 
n, q = map(int, input().split())
ans = [0 for _ in range(n)]
visited = [False for _ in range(n)]
edges = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  edges[a-1].append(b-1)
pp = [0 for _ in range(n)]
for _ in range(q):
  p, x = map(int, input().split())
  pp[p-1] += x
dfs(0, 0)
print(*ans)

Submission status: Wrong Answer

Solution code:
class Node:
  def __init__(self, i):
    self.counter = 0
    self.childs = []
    self.num = i
  def add_child(self, node):
    self.childs.append(node)
 
N, Q = [int(i) for i in input().split()]
ndic = {}
for i in range(1, N+1):
  ndic[i] = Node(i)
for i in range(N-1):
  a, b = [int(i) for i in input().split()]
  ndic[a].add_child(ndic[b])
 
from collections import defaultdict
cdic = defaultdict(int)
for i in range(Q):
  p, x = [int(i) for i in input().split()]
  cdic[p] += x
  
stack = []
stack.append((ndic[1], 0))
ans = [0] * N
while(len(stack) != 0):
  node, sum = stack.pop()
  sum += cdic[node.num]
  ans[node.num-1] = sum
  for c in ndic[node.num].childs:
    stack.append((c, sum))

anst = ""
for a in ans:
  anst += str(a)+ " "
print(anst[:-1])

Submission status: Wrong Answer

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

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

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

ans = [0]*n
for p, x in cnt:
    tmp = [p]
    while tmp:
        for i in tmp:
            ans[i-1] += x
        tmp2 = tmp
        tmp = []
        for aaa in tmp2:
            tmp.extend(tree[aaa])
    print(ans)

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

Submission status: Wrong Answer

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

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

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

ans = [0]*n
for p, x in cnt:
    tmp = [p]
    while len(tmp) != 0:
        for i in tmp:
            ans[i-1] += x
        tmp2 = tmp
        tmp = []
        for aaa in tmp2:
            tmp.extend(tree[aaa])

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

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

trees = [[] for i in range(n)]

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

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

#print("values: ", values)

def dfs(i):
    for j in trees[i]:
        values[j] += values[i]
        #print("values",i , j, values)
        dfs(j)

dfs(0)
print(*values)

Submission status: Wrong Answer

Solution code:
import math
import numpy as np


#横並び
#a, b, c, x = map(int, [input() for i in range(4)])
#z = list(map(int, input().split()))
#n, a, b = map(int, input().split())

#print(len(z))
#for i in z:
#    print(i)

#a = sorted(a,reverse = True)
#print(sum(a[::2]) - sum(a[1::2]))
#z = map(int, [input() for i in range(int(n))])
#data_list = [ list(map(int,input().split(" "))) for i in range(3)]


n,q = map(int, input().split())
a = [ list(map(int,input().split())) for i in range(n-1)]
x = [ list(map(int,input().split())) for i in range(q)]
a.sort()
x.sort()
y = [0] * n
for i in x:
    y[i[0]-1] += i[1]

for i in a:
    y[i[1]-1] += y[i[0]-1]


print(*y)

Submission status: Wrong Answer

Solution code:
import math
import numpy as np


#横並び
#a, b, c, x = map(int, [input() for i in range(4)])
#z = list(map(int, input().split()))
#n, a, b = map(int, input().split())

#print(len(z))
#for i in z:
#    print(i)

#a = sorted(a,reverse = True)
#print(sum(a[::2]) - sum(a[1::2]))
#z = map(int, [input() for i in range(int(n))])
#data_list = [ list(map(int,input().split(" "))) for i in range(3)]


n,q = map(int, input().split())
a = [ list(map(int,input().split())) for i in range(n-1)]
x = [ list(map(int,input().split())) for i in range(q)]

x.sort()
y = [0] * n
for i in x:
    y[i[0]-1] += i[1]

for i in a:
    y[i[1]-1] += y[i[0]-1]


print(*y)

Submission status: Wrong Answer

Solution code:
import collections

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

g = [[] 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)

count = [0] * (n+1)
check = [0] * (n+1)

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

q = collections.deque()  
q.append(1)

while q:
    v = q.pop()
    check[v] = 1
    for u in g[v]:
        if check[u] == 1:
            continue
        count[u] += count[v]
        q.append(u)
        
print(count[1:])

Submission status: Wrong Answer

Solution code:
import math
import numpy as np


#横並び
#a, b, c, x = map(int, [input() for i in range(4)])
#z = list(map(int, input().split()))
#n, a, b = map(int, input().split())

#print(len(z))
#for i in z:
#    print(i)

#a = sorted(a,reverse = True)
#print(sum(a[::2]) - sum(a[1::2]))
#z = map(int, [input() for i in range(int(n))])
#data_list = [ list(map(int,input().split(" "))) for i in range(3)]


n,q = map(int, input().split())
a = [ list(map(int,input().split())) for i in range(n-1)]
x = [ list(map(int,input().split())) for i in range(q)]

x.sort()
y = [0] * n
for i in x:
    y[i[0]-1] += i[1]

for i in a:
    y[i[1]-1] += y[i[0]-1]

print(y)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

to = [[] for i in range(n)]

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

point = [0] * n

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

node = [0] * n
node[0] = point[0]

d = deque([0])

while len(d) > 0:
    n_now = d.popleft()

    for n_next in to[n_now]:
        d.append(n_next)
        node[n_next] = node[n_now] + point[n_next]

for i in range(n):
    print(node[i], end="")
    if i != n - 1:
        print(" ", end="")

print("")

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
counter = [0]*N
E =[tuple(map(int,input().split())) for _ in range(N-1)]

for i in range(Q):
    p,x = map(int,input().split())
    for e in E:
        if e[0] >= p:
            counter[e[1]-1] += x #list(map(lambda p:p[1]-1, list(filter(lambda e:e[0]>=p,E))))
    counter[p-1] += x
    # print(mask)
    # print(counter)
print("%d "*N % tuple(counter))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,Q = map(int,input().split())
counter = np.zeros(N,dtype=int)
E =[tuple(map(int,input().split())) for _ in range(N-1)]

for i in range(Q):
    p,x = map(int,input().split())
    mask = list(map(lambda p:p[1]-1, list(filter(lambda e:e[0]>=p,E))))
    mask.append(p-1)
    counter[mask] += x
    # print(mask)
    # print(counter)
print("%d "*N % tuple(counter))

Submission status: Wrong Answer

Solution code:
import math
import sys
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)


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

operations = []
for i in range(q):
	operations.append(list(map(int,input().split())))
for i,e in enumerate(operations):
	operations[i][0] -= 1


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

for edge in edges:
	parents[edge[1]] = edge[0]


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

for operation in operations:
	values[operation[0]] += operation[1]

for i in range(1,n):
	values[i] +=  values[parents[i]]

ans = ''

for e in values:
	ans += str(e) + ' '
print(ans[:-1])