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:
import numpy as np
N,Q = list(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)]
v_list = np.zeros(N, dtype=np.int)
tree = [[] for _ in range(N)]

for i in range(N-1):
    # a, b = list(map(int, input().split(" ")))
    a,b = ab[i]
    tree[a-1].append(b-1)
        
for i in range(Q):
    # p, x = list(map(int, input().split(" ")))
    p,x = px[i]
    
    # v_list[dic_tree[p]] += x # Too Heavy...
    v_list[p-1] += x

for i in range(N):
    v_list[tree[i]] += v_list[i]
    if i > 0:
        print(" ", end='') 
    print(v_list[i], end='')

Submission status: Wrong Answer

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

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

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

task_sum = [[] for _ in range(N+1)]
for i in range(N+1):
    task_sum[i].append(sum(task[i]))

print('task_sum:', task_sum)

cnt = [[] for i in range(N+1)]

#最初はdfs(1, -1, task_sum[1])

def dfs(v, p, cost):
        
    for nv in edges[v]:
        if nv == p:
            continue
        
        dfs(nv, v, cost+task_sum[nv])

    cnt[v].append(cost)

dfs(1, -1, task_sum[1])

print(cnt)
for i in range(1, N+1):
    print(int(sum(cnt[i][0])), end=' ')

value = [0] * (N+1)
print(value)

Submission status: Wrong Answer

Solution code:
import math
import fractions
import copy
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
"""
#decomment when needed
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""

def addbottom(total,tree,k,value,base):
    #print("current layer",k)
    #print(k,value)
    total[k]+=value
    #print(total)
    if len(tree[k]):
        for i in tree[k]:
            #print(k,i)
            total = addbottom(total, tree, i, value,base)
    return total


n ,m = ips()
tree = [[] for i in range(n+1)]
for i in range(n-1):
    x,y = ips()
    tree[x].append(y)
total = [0 for i in range(n+1)]
for i in range(m):
    x,adder = ips()
    total[x]+=adder
base = copy.copy(total)
for i in range(1,n+1):
    #print("adding up",i,base[i])
    value = total[i]
    if len(tree[i]):
        for k in tree[i]:
            total[k]+=value
for i in range(1,n):
    print(total[i],end=" ")
print(total[n])

Submission status: Wrong Answer

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

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

T = [ [int(i) for i in input().split() ] for j in range(N - 1) ]

S = [ [int(i) for i in input().split() ] for j in range(Q) ]



w = []


# T = [[1,2],[2,3],[3,4],[3,5]]

T = np.array(T)


# nの右隣の数をリストとして返す関数
def return_number(n):
	
	x = []

	# rはタプル
	r = np.where(T[:,0] == n )

	for i in range(len(r[0])):
		x.append(T[r[0][i],1])
		#xにはnの右横の数字が保存される

	return x	
	# リストxを返す、0列目bにnがない時は[]を返す

# リストyの下のノードの数を数える関数
def counter_children(y):
	counter = 0
	for i in range(len(y)):
		counter += len(return_number(y[i]))

	return counter	



def search_subtree(x,y):
	# 再起のたびにzは空になる	
	z = []

	# x,yはリスト xは全体のsubtreeリスト、yは維持的はi番目の深さのnode番号
	# 終了条件(下に子がない時)
	if counter_children(y) == 0:
		return  x
	
	else:
		for i in range(len(y)):
			x += return_number(y[i]) 
			z += return_number(y[i]) 
		
		return search_subtree(x,z)

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

for i in range(Q):
	# S[i][1]がポイント
	# S[i][0]が親node
	 p = search_subtree([S[i][0]],[S[i][0]])

	 for j in range(len(p)):
	 	counter_node[p[j] - 1] += S[i][1]

for i in range(N):
	sys.stdout.write(str(counter_node[i]) + " ")

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
lst = []
cnt = []
ans = [0]*N
N = N - 1
for i in range(N):
    lst.append(list(map(int, input().split())))
lst = sorted(lst)

for j in range(Q):
    cnt.append(list(map(int, input().split())))
               
for k in cnt:
    ans[k[0]-1] += k[1]
for t in lst:
    ans[t[1]-1] += ans[t[0]-1]
for a in ans:
    print(a, end=" ")

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
lst = []
cnt = []
ans = [0]*N
N = N - 1
for i in range(N):
    lst.append(list(map(int, input().split())))
lst = sorted(lst)

for j in range(Q):
    cnt.append(list(map(int, input().split())))
               
for k in range(Q):
    ans[cnt[k][0]-1] += cnt[k][1]
for i in range(N):
    ans[lst[i][1]-1] += ans[lst[i][0]-1]
for a in ans:
    print(a, end=" ")

Submission status: Wrong Answer

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

def qul(d,l,p):
    if p in d:
        for j in d[p]:
            if j in d:
                if j not in l:
                    l.append(j)
                qul(d,l,j)
            else:
                l.append(j)
def sol():
    d={}
    n,q=map(int,input().split())
    for i in range(n-1):
        a,b=map(int,input().split())
        if a in d:
            d[a].append(b)
        else:
            d[a]=[b]
    w=[0]*(n+1)
    for i in d:
        d[i].sort(reverse=True)
    print(0)
    if (True): exit()
    for i in range(q):
        p,x=map(int,input().split())
        w[p]+=x
    for i in range(n,0,-1):
        l=[]
        qul(d,l,i)
        for j in l:
            w[j]+=w[i]
        #print(i,l)
    print(*w[1:])

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
lst = []
cnt = []
ans = [0]*N
N = N - 1
for i in range(N):
    lst.append(sorted(list(map(int, input().split()))))
for j in range(Q):
    cnt.append(list(map(int, input().split())))
for k in range(Q):
    ans[cnt[k][0]-1] += cnt[k][1]
for i in range(N):
    ans[lst[i][1]-1] += ans[lst[i][0]-1]
for a in ans:
    print(a, end=" ")

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)

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

def dfs(i):
    for j in trees[i]:
        values[j] += values[i]
        dfs(j)

dfs(0)
print(*values)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
lst = []
cnt = []
ans = [0]*N
N = N - 1
for i in range(N):
    lst.append(list(map(int, input().split())))
for j in range(Q):
    cnt.append(list(map(int, input().split())))
for k in range(Q):
    ans[cnt[k][0]-1] += cnt[k][1]
for i in range(N):
    ans[lst[i][1]-1] += ans[lst[i][0]-1]
for a in ans:
    print(a, end=" ")

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**6)
es = [[] for i in range(N)]

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


point = [0] * N

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

ans = [0] * N
ans[0] = point[0] # 一番上の根なのでこれだけ
visited = [False] * N
visit_cnt = 1
from collections import deque
nexts = deque([0])
while len(nexts) >= 1:
    now = nexts.popleft()
    visited[now] = True

    for next in es[now]:
        if visited[next] == True:
            continue
        nexts.append(next)
        ans[next] = ans[now] + point[next]


print(*ans)

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**6)
#es = [[] for i in range(N)]
es = {}
for i in range(N-1):
    a,b = map(int, input().split())
    a,b = a-1, b-1
    if a in es:
        es[a].append(b)
    else:
        es[a] = [b]


point = [0] * N

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

ans = [0] * N
ans[0] = point[0] # 一番上の根なのでこれだけ
visited = [False] * N
visit_cnt = 1
from collections import deque
nexts = deque([0])
while len(nexts) >= 1:
    now = nexts.popleft()
    visited[now] = True
    if now in es:
        for next in es[now]:
            if visited[next] == True:
                continue
            nexts.append(next)
            ans[next] = ans[now] + point[next]


print(*ans)

Submission status: Wrong Answer

Solution code:
def tree(p,x,m,v):
    q=[]
    q.append(p)
    while len(q)!=0:
        n=q.pop(0)
        v[n-1]+=x
        for i in range(len(m[n-1])):
            if m[n-1][i]==1:
                q.append(i+1)

N,Q=map(int,input().split())
v={}

for i in range(N):
    v[i]=0

m=[[0 for i in range(N)] for j in range(N)]

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

for _ in range(Q):
    p,x=map(int,input().split())
    tree(p,x,m.copy(),v)

for i in v:
    print(v[i])

Submission status: Wrong Answer

Solution code:
import numpy as np

import sys


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

T = [ [int(i) for i in input().split() ] for j in range(N - 1) ]

S = [ [int(i) for i in input().split() ] for j in range(Q) ]



w = []


# T = [[1,2],[2,3],[3,4],[3,5]]

T = np.array(T)


# nの右隣の数をリストとして返す関数
def return_number(n):
	
	x = []

	# rはタプル
	r = np.where(T[:,0] == n )

	for i in range(len(r[0])):
		x.append(T[r[0][i],1])
		#xにはnの右横の数字が保存される

	return x	
	# リストxを返す、0列目bにnがない時は[]を返す

# リストyの下のノードの数を数える関数
def counter_children(y):
	counter = 0
	for i in range(len(y)):
		counter += len(return_number(y[i]))

	return counter	



def search_subtree(x,y):
	# 再起のたびにzは空になる	
	z = []

	# x,yはリスト xは全体のsubtreeリスト、yは維持的はi番目の深さのnode番号
	# 終了条件(下に子がない時)
	if counter_children(y) == 0:
		return  x
	
	else:
		for i in range(len(y)):
			x += return_number(y[i]) 
			z += return_number(y[i]) 
		
		return search_subtree(x,z)

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

for i in range(Q):
	# S[i][1]がポイント
	# S[i][0]が親node
	 p = search_subtree([S[i][0]],[S[i][0]])

	 for j in range(len(p)):
	 	counter_node[p[j] - 1] += S[i][1]

for i in range(N):
	sys.stdout.write(str(counter_node[i]) + " ")

Submission status: Wrong Answer

Solution code:
def main():
    from operator import itemgetter
    n,q=map(int,input().split())
    ls=[0]*(n+1)
    
    ki_list=[list(map(int,input().split())) for i in range(n-1)]
    ki_list.sort(key=itemgetter(0))
    
    for i in range(q):
        p,x = map(int,input().split())
        ls[p]+=x
    
    for i in range(n-1):
        oya,ko=ki_list[i][0],ki_list[i][1]
        ls[ko]+=ls[oya]
    
    del ls[0]
    print(' '.join(map(str,ls)))

if __name__ == '__main__': 
    main()

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
n,q=map(int,input().split())
ls=[0]*(n+1)

ki_list=[list(map(int,input().split())) for i in range(n-1)]
ki_list.sort(key=itemgetter(0))

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

for i in range(n-1):
    oya,ko=ki_list[i][0],ki_list[i][1]
    ls[ko]+=ls[oya]

del ls[0]
print(' '.join(map(str,ls)))

Submission status: Wrong Answer

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

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


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

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

for i in range(1, N+1):
    point[i] += point[parent[i]]

print(*point[1:])

Submission status: Wrong Answer

Solution code:
def tree(p,x,d,v):
    q=[]
    q.append(p)
    while len(q)!=0:
        i=q.pop(0)
        v[i]+=x
        if i in d:
            for j in d[i]:
                q.append(j)

N,Q=map(int,input().split())
d={}
v={}

for i in range(1,N+1):
    v[i]=0

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


for _ in range(Q):
    p,x=map(int,input().split())
    tree(p,x,d.copy(),v)

for i in v:
    print(v[i])

Submission status: Wrong Answer

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

ct = [0] * (n + 1)
e = [[] for _ in range(n + 1)]

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

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

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**6)
es = {}
for i in range(N-1):
    a,b = map(int, input().split())
    a,b = a-1, b-1
    if a in es:
        es[a].append(b)
    else:
        es[a] = [b]


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

for i in range(N):
    if i in es:
        for j in es[i]:
            point[j] += point[i]

print(*point)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

stack = deque()
stack.append(0)
ans = [0] * N
ans = V

while stack:
    v = stack.popleft()
    for e in E[v]:
        ans[e] += ans[v]
        stack.append(e)

print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n, q = map(int, input().split())
child_dict = defaultdict(list)
value_dict = defaultdict(lambda: 0)
for _ in range(n - 1):
    a, b = map(int, input().split())
    child_dict[a].append(b)
import copy
for _ in range(q):
    c, d = map(int, input().split())
    value_dict[c] += d
    child = child_dict[c]
    while len(child) > 0:
        k = []
        for i in child:
            value_dict[i] += d
            k.extend(child_dict[i])
        child = copy.deepcopy(k)
ans = [value_dict[i] for i in range(1 ,n+1)]
print(*ans)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
n, q = map(int, input().split())
edge = [list(map(int, input().split())) for i in range (n-1)]
edge.sort(key=itemgetter(1))
ope = [list(map(int, input().split())) for j in range(q)]
c = [0] * n
for j in range(q):
    m = ope[j][0] - 1
    c[m] += ope[j][1]
for i in range(n-1):
    k = edge[i][0] - 1
    c[i+1] += c[k]
print(*c)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

n, q = map(int, input().split())
child_dict = defaultdict(list)
value_dict = defaultdict(lambda: 0)
for _ in range(n - 1):
    a, b = map(int, input().split())
    child_dict[a].append(b)

for _ in range(q):
    c, d = map(int, input().split())
    value_dict[c] += d
    

stack = deque()
stack.append(1)

while stack:
    top_ = stack.pop()
    for child in child_dict[top_]:
        value_dict[child] += value_dict[top_]
    stack.extend(child_dict[top_])
ans = [value_dict[i] for i in range(1 ,n+1)]
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline

n,q = map(int,input().split())
score = [0]*(n+1)
tree = [0]*(n+1)
child = [() for i in range(n+1)]

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

que = deque(child[1])
qappend = que.append
qpop = que.popleft

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

while(que):
  n = qpop()
  score[n] += score[tree[n]]
  for i in child[n]:
    qappend(i)

print(*score[1:])

Submission status: Wrong Answer

Solution code:
def get_subgraph(node):
  subgraph = [node]
  for i, j in edges:
    if node == i:
      subgraph.extend(get_subgraph(j))
      break
  return subgraph

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

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

counter = list(map(int, list('0'*n)))

subgraphs = []
for i in range(1, n+1):
  subgraphs.append(get_subgraph(i))

for p, x in tasks:
  for i in subgraphs[p-1]:
    counter[i-1] += x
print(' '.join(map(str, counter)))

Submission status: Wrong Answer

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

tree = {}

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

p_sum = [0]*n

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

res = [0]*n
q = []
checked = [False]*n
q.append(0)
checked[0]=True
res[0]=p_sum[0]

while 1:
    s = q.pop(0)
    print(s)
    for t in tree[s]:
        if not checked[t]:
            q.append(t)
            checked[t] = True
            res[t] = res[s]+p_sum[t]
    if q == []: break
            

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
score = [0]*(n+1)
tree = [0]*(n+1)

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

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

for i in range(1,n+1):
  score[i] += score[tree[i]]

print(*score[1:])

Submission status: Wrong Answer

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

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

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

g = [0] * N
for i in range(N-1):
    link[a[i]-1].append([b[i]-1, cost[i]])

nums = [0] * N
def dfs(v, c):
    for nv in link[v]:
        nums[nv[0]] += c + cost[nv[0]]
        dfs(nv[0], c + cost[nv[0]])
    return

nums[0] = cost[0]
dfs(0, cost[0])

print(*nums)

Submission status: Wrong Answer

Solution code:
N,Q = map(int, input().split())
tree = {i:[0,[]] for i in range(1,N+1)}
for i in range(N-1):
  a,b = map(int, input().split())
  tree[a][1].append(b)
for i in range(Q):
  p,x = map(int, input().split())
  tree[p][0] += x
  target = tree[p][1][:]
  while target:
    p = target.pop()
    tree[p][0] += x
    target.extend(tree[p][1])
print(*[tree[i][0] for i in range(1,N+1)], sep=" ")

Submission status: Wrong Answer

Solution code:
import pprint
import sys
sys.setrecursionlimit(4100000)

def comp2(num, ll, sl, check):
    if check[num] == 1:
        return sl
    else:
        check[num] = 1
        sl[num].add(num)
        for i in ll[num]:
            sl[num] = sl[num] | (comp2(i, ll, sl, check))[i]
            '''print("num ", num, " i ", i)
            print("sl ", sl)'''
        return sl


def solve():
    n, q = map(int, input().split())
    l = [[0] for i in range(n+1)]
    check = [0 for i in range(n+1)]
    group = [set() for i in range(n+1)]
    ans = [0]*(n+1)
    points = [0 for i in range(n+1)]
    check[0] = 1
    for i in range(n-1):
        a, b = map(int, input().split())
        l[a].append(b)
    for i in range(1,n+1):
        group = comp2(i,l,group,check)
    for i in range(q):
        num, point = map(int, input().split())
        points[num] += point
    for i in range(1,n+1):
        li = list(group[num])
        for j in li:
            ans[j] += points[i]
    ans2 = list(map(str, ans))
    ans3 = ' '.join(ans2[1::])
    print(ans3)


if __name__=="__main__":
    solve()

Submission status: Wrong Answer

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

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

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

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

for i, p in enumerate(point):
    for idx in child[i]:
        point[idx] += p

print(*point[1:])

Submission status: Wrong Answer

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

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

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

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


for i, p in enumerate(point):
    for idx in child[i]:
        point[idx] += p

print(*point[1:])

Submission status: Wrong Answer

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

def addc(p,b,total):
  for i in e[p]:
    if(i == b):
      continue
    c[i] += total
    total = c[i]
    addc(i,p,total)

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

e = [ [] for i in range(n + 1)]
c = [0 for i in range(n + 1)]

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

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

addc(1,-1,c[1])

c.pop(0)

print(*c)

Submission status: Wrong Answer

Solution code:

N, Q = map(int,input().split())
G = [[] for i in range(N+ 1)]
ANS = [0] * (N + 1)
for _ in range(N - 1):
    a , b = map(int,input().split())
    G[a].append(b)
for _ in range(Q):
    p, x =map(int,input().split())
    ANS[p] += x
from collections import deque
que=deque([])
que.append(1)
while que:
    node = que.pop()
    ans = ANS[node]
    for child in G[node]:
        ANS[child] += ans
        que.append(child)
for i in range(1, N + 1):
    print(ANS[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
 
n, q = map(int, input().split())
tl = [list() for _ in range(n)]
xl = [0] * n
ans = [0] * n
 
for i in range(n-1):
    a, b = map(int, input().split())
    tl[a-1].append(b-1)
 
for i in range(q):
    p, x = map(int, input().split())
    xl[p-1] += x

def rec(p, x):
    x += xl[p]
    ans[p] = x
    for num in tl[p]:
        rec(num, x)

rec(0, 0)
 
print(*ans)

Submission status: Wrong Answer

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

for i in range(N-1):
    a, b = map(int,input().split())
    nord[b-1].add(a-1)
    
nord_num = [0 for i in range(N)]
for i in range(Q):
    p, x = map(int,input().split())
    nord_num[p-1] = nord_num[p-1] + x
    
for i in range(N):
    for j in nord[i]:
        nord_num[i] = nord_num[j] + nord_num[i]
        
count = 1
for i in nord_num:
    if count == N:
        print(i)
    else:
        print(i, end=" ")
        count += 1

Submission status: Wrong Answer

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

dic = {}

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

cnt = [0] * (N+1)

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

for root, leaf in dic.items():
    for i in leaf:
        cnt[i] += cnt[root]

print(*cnt[1:])

Submission status: Wrong Answer

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

dic = {}

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

cnt = [0] * (N+1)

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

for root, leaf in dic.items():
    for i in leaf:
        cnt[i] += cnt[root]

print(*cnt[1:])

Submission status: Wrong Answer

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

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

dic = {}

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

cnt = [0] * (N+1)

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

for root, leaf in dic.items():
    for i in leaf:
        cnt[i] += cnt[root]

print(*cnt[1:])

Submission status: Wrong Answer

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

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

root = {}
for i in range(n-1):
    a, b = map(int, input().split())
    if a in root:
        root[a] += [b]
    else:
        root.setdefault(a, [b])
    if b in root:
        root[b] += [a]
    else:
        root.setdefault(b, [a])

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q = map(int, input().split())
tree = [[int(i) for i in input().split()] for i in range(n-1)] 
px = [[int(i) for i in input().split()] for i in range(q)] 
calc_tree = [[] for i in range(n)]
ans = [0] * n
ans2 = [0] * n
flag = [1] * n

for i in range(n-1):
  if flag[tree[i][1]-1]:
    calc_tree[tree[i][0]-1].append(tree[i][1]-1)
    flag[tree[i][1]-1] = 0
  else:
    calc_tree[tree[i][1]-1].append(tree[i][0]-1)

for i in px:
  ans[i[0]-1] += i[1]
'''
tmp = flag.index(1)
print(ans[tmp])
'''
yet = deque()
yet.append(flag.index(1))
#for i in calc_tree[tmp]:
 # yet.append(i)
while yet:
  ans2[yet[0]] = ans[yet[0]]
  for i in calc_tree[yet[0]]:
    #print(i)
    ans[i] += ans[yet[0]]
    yet.append(i)
  #print(yet,'pop前')
  yet.popleft()
  #print(yet)
  
for i in ans2:
  print(i)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(500000)

N,Q = map(int, input().split())
AB_list = []
for _ in range(N-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    AB_list.append((a,b))
AB_list.sort(key=lambda x:x[0])

add_list = np.zeros(N, dtype=int)
for _ in range(Q):
    p,x = map(int, input().split())
    add_list[p-1] += x

for a, b in AB_list:
    add_list[b] += add_list[a]
 
result = " ".join(map(str, add_list))
print(result)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

def rec(parent):
    if len(edge[parent])>0: 
        for x in edge[parent]:
            nodes[x]+=nodes[parent]
            rec(x)

rec(0)
print(*nodes)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
child = [() for i in range(n+1)]
parent = [() for i in range(n+1)]
score = [0]*(n+1)
for i in range(n-1):
  a,b = map(int,input().split())
  child[a] += (b,)
  parent[b] += (a,)
  for j in parent[a]:
    child[j] += (b,)
    parent[b] += (j,)
for i in range(q):
  p,x = map(int,input().split())
  score[p] += x
  for j in child[p]:
    score[j] += x
print(*score[1:])

Submission status: Wrong Answer

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

for i in range(n-1):
  if tree[i][0] < tree[i][1]:
    calc_tree[tree[i][0]-1].append(tree[i][1]-1)
  else:
    calc_tree[tree[i][1]-1].append(tree[i][0]-1)

for i in px:
  ans[i[0]-1] += i[1]

for i,a in enumerate(ans):
  print(a)
  for j in calc_tree[i]:
    ans[j] += a

Submission status: Wrong Answer

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

for i in range(n-1):
  calc_tree[tree[i][0]-1].append(tree[i][1]-1)

for i in px:
  ans[i[0]-1] += i[1]

for i,a in enumerate(ans):
  print(a)
  for j in calc_tree[i]:
    ans[j] += a