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())
a = []
b = []
p = []
x = []
dic = {}
for i in range(N-1):
    t1,t2 = map(int,input().split())
    a.append(t1)
    b.append(t2)
    dic.update({t2:t1})
ans = [0]*N
for i in range(Q):
    t1,t2 = map(int,input().split())
    p.append(t1)
    x.append(t2)
    ans[t1-1] += t2
# print(a,b,p,x,dic,ans)
dic2 = sorted(dic.items(), key=lambda x:x[0])
dic3 = {}
for i,j in dic2:
    dic3.update({i:j})
# print(dic3)
for item in dic3:
#     print(ans[item-1], ans[dic3[item]])
    ans[item-1] += ans[dic3[item]-1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
a = []
b = []
p = []
x = []
dic = {}
for i in range(N-1):
    t1,t2 = map(int,input().split())
    a.append(t1)
    b.append(t2)
    dic.update({t2:t1})
ans = [0]*N
for i in range(Q):
    t1,t2 = map(int,input().split())
    p.append(t1)
    x.append(t2)
    ans[t1-1] += t2
# print(a,b,p,x,dic,ans)
for item in dic:
#     print(ans[item-1], ans[dic[item]])
    ans[item-1] += ans[dic[item]-1]
print(*ans)

Submission status: Wrong Answer

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

a=sorted(a)
for i in range(n-1):
    m[a[i][1]-1]+=m[a[i][0]-1]

print(*m)

Submission status: Wrong Answer

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

a=sorted(a)
for i in range(n-1):
    m[a[i][1]-1]+=m[a[i][0]-1]

print(m)

Submission status: Wrong Answer

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

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

for i in range(q):
	p, x = map(int, input().split())
	vals[p - 1] += x
	
for i in sorted(range(n), key=lambda k : depths[k]):
	child_idxes = children_idxes[i]
	for child_idx in child_idxes:
		vals[child_idx] += vals[i]

print(*vals)

Submission status: Wrong Answer

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

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

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

for i in range(n - 1):
	children_idx = children_idxes[i]
	for child_idx in children_idx:
		vals[child_idx] += vals[i]

print(*vals)

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]*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]
c=[str(i) for i in c]
print(' '.join(c))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

N, Q = map(int, input().split())
tree = [list(map(int, input().split())) for _ in range(N - 1)]
value = [list(map(int, input().split())) for _ in range(Q)]
count = [0] * N

for i in range(N - 1):
    if tree[i][0] > tree[i][1]:
        tree[i][0], tree[i][1] = tree[i][1], tree[i][0]

tree_sort = sorted(tree, key = itemgetter(0, 1))

# 各頂点の純粋なカウントを求める
for p, v in value:
    count[p - 1] = count[p - 1] + v

# 木の頂点から、下に値を加えていく
for a, b in tree_sort:
    count[b - 1] = count[b - 1] + count[a - 1]

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

N, Q = map(int, input().split())
tree = [list(map(int, input().split())) for _ in range(N - 1)]
value = [list(map(int, input().split())) for _ in range(Q)]
count = [0] * N

for i in range(N - 1):
    if tree[i][0] > tree[i][1]:
        tree[i][0], tree[i][1] = tree[i][1], tree[0]

tree_sort = sorted(tree, key = itemgetter(0, 1))

# 各頂点の純粋なカウントを求める
for p, v in value:
    count[p - 1] = count[p - 1] + v

# 木の頂点から、下に値を加えていく
for a, b in tree_sort:
    count[b - 1] = count[b - 1] + count[a - 1]

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

N, Q = map(int, input().split())
tree = [list(map(int, input().split())) for _ in range(N - 1)]
value = [list(map(int, input().split())) for _ in range(Q)]
count = [0] * N
tree_sort = sorted(tree, key = itemgetter(0, 1))

# 各頂点の純粋なカウントを求める
for p, v in value:
    count[p - 1] = count[p - 1] + v

# 木の頂点から、下に値を加えていく
for a, b in tree_sort:
    count[b - 1] = count[b - 1] + count[a - 1]

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

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

g=[0]*N
ab.sort(key=lambda x:x[0])
for i in range(Q):
  g[px[i][0]-1]+=px[i][1]
for i in range(N-1):
  g[ab[i][1]-1]+=g[ab[i][0]-1]
print(" ".join(map(str,g)))

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

n,q = [int(x) for x in stdin.readline().rstrip().split()]
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n-1)]
lin = [list(map(int,stdin.readline().rstrip().split())) for _ in range(q)]

kazu = [[]for _ in range(n)]
point = [0]*n

for i in range(len(li)):
    kazu[li[i][0]-1].append(li[i][1]-1)
    kazu[li[i][1]-1].append(li[i][0]-1)

for i in range(q):
    point[lin[i][0]-1] += lin[i][1]

#深さ優先探索を実装したい(liが各ノードを入れたリスト、li2が)
def dfs(li,li2,n):
    for i in range(n):
        for j in range(len(li[i])):
            if li[i][j] > i:
                li2[li[i][j]] += li2[i]
    return li2

print(*dfs(kazu,point,n))

Submission status: Wrong Answer

Solution code:
N,Q = map(int, input().split())
ki = [[] for i in range(N+1)]
val = [0 for _ in range(N+1)]
 
for _ in range(N-1):
    a,b = map(int, input().split())
    ki[a].append(b)
for _ in range(Q):
    p,x = map(int, input().split())
    val[p] += x
for i in range(N):
  v = val[i]
  if v:
    for k in ki[i]:
      val[k] += v
print(' '.join(map(str, val[1:])))

Submission status: Wrong Answer

Solution code:
n, q = [int(i) for i in input().split()]
ab = [0]*n
for _ in range(n - 1):
    i, j = [int(i) for i in input().split()]
    ab[j-1] = i-1
p = [0] * n
for _ in range(q):
    i, j = [int(i) for i in input().split()]
    p[i - 1] += j
c = []
d = {}

for i in range(n):
    k = i
    j = 0
    while True:
        if i in d:
            j += d[i]
            break
        j += p[i]
        if i == 0:
            break
        i = ab[i]
    d[k] = j
    c.append(str(j))
print(" ".join(c))

Submission status: Wrong Answer

Solution code:
n, q = [int(i) for i in input().split()]
ab = [0]*n
for _ in range(n - 1):
    i, j = [int(i) for i in input().split()]
    ab[i-1] = j-1
p = [0] * n
for _ in range(q):
    i, j = [int(i) for i in input().split()]
    p[i - 1] += j
c = []
for i in range(n):
    j = 0
    while True:
        j += p[i]
        if i == 0:
            break
        i = ab[i]
    c.append(str(j))
print(" ".join(c))

Submission status: Wrong Answer

Solution code:
n, q = [int(i) for i in input().split()]
ab = [0]*n
for _ in range(n - 1):
    i, j = [int(i) for i in input().split()]
p = [0] * n
for _ in range(q):
    i, j = [int(i) for i in input().split()]
    p[i - 1] += j
c = []
for i in range(n):
    j = 0
    while True:
        j += p[i]
        if i == 0:
            break
        i = ab[i]
    c.append(str(j))
print(" ".join(c))

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
from fractions import Fraction
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n, q = MAP()
parent = [-1]*n
value = [0] * n
for i in range(n-1):
    a, b = MAP()
    parent[b-1] = a-1
for i in range(q):
    p, x = MAP()
    value[p-1] += x

for i in range(n):
    if i == 0:
        print(value[i], end = ' ')
    else:
        value[i] += value[parent[i]]
        print(value[i], end = ' ')

Submission status: Wrong Answer

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

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

for i in range(n+1):
    vq=deque()
    vq.append(i)
    pi=s[i]
    while(len(vq)>0):
        vi=vq.popleft()
        c[vi]+=pi
        child=edge[vi]
        for j in range(len(child)):
            vq.append(child[j])

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

Submission status: Wrong Answer

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

for i in range(q):
    p,x=map(int,input().split())
    vq=deque()
    vq.append(p)
    while(len(vq)>0):
#        print(vq)
        pi=vq.popleft()
        c[pi]+=x
        child=edge[pi]
        for j in range(len(child)):
            vq.append(child[j])

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

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
a=[]
c=[0 for i in range(n)]
for i in range(n-1):
    A,B=map(int,input().split())
    if A>B:
        A,B=B,A
    a.append([A,B])
a.sort()

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

for i in range(n-1):
  c[a[i][1]-1]+=c[a[i][0]-1]

print(*c)

Submission status: Wrong Answer

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

c=[0 for i in range(n)]
a=[list(map(int,input().split())) for _ in range(n-1)]
a.sort()

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

for i in range(n-1):
  c[a[i][1]-1]+=c[a[i][0]-1]

print(*c)

Submission status: Wrong Answer

Solution code:
import sys
def solve():
    sys.setrecursionlimit(1000000)
    N, Q = map(int, input().split())
    tree = [[] for i in range(N)]
    counter = [0] * N
    arrived = [False] * N
    for i in range(N-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    
    for i in range(Q):
        p, x = map(int, input().split())
        counter[p-1] += x
    
    arrived[0] = True
    adder(0, counter, tree, arrived)

    print(tree)
    print(*counter)

def adder(ind, counter, tree, arrived):
    print(counter)
    for i in tree[ind]:
        if arrived[i]:
            continue
        counter[i] += counter[ind]
        arrived[i] = True
        adder(i, counter, tree, arrived)
    if not tree[ind]:
        return

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
def solve():
    sys.setrecursionlimit(1000000)
    N, Q = map(int, input().split())
    tree = [[] for i in range(N)]
    counter = [0] * N
    for i in range(N-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
    
    for i in range(Q):
        p, x = map(int, input().split())
        counter[p-1] += x
    adder(0, counter, tree)

    print(*counter)

def adder(ind, counter, tree):
    if not tree[ind]:
        return
    for i in tree[ind]:
        counter[i] += counter[ind]
        adder(i, counter, tree)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
def solve():
    sys.setrecursionlimit(1000000)
    N, Q = map(int, input().split())
    tree = [[] for i in range(N)]
    counter = [0] * N
    for i in range(N-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
    
    for i in range(Q):
        p, x = map(int, input().split())
        adder(p-1, x, counter, tree)

    print(*counter)

def adder(ind, add_val, counter, tree):
    counter[ind] += add_val
    if not tree[ind]:
        return

    for i in tree[ind]:
        adder(i, add_val, counter, tree)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

N, Q = LI()
P = [0] * N
V = [[] for _ in range(N)]

w = [0]*N
for i in range(N-1):
    A, B = LI()
    V[A-1].append(B-1)
#    AB[i] = LI()
for i in range(Q):
    p, x = LI()
    P[p-1] += x

#print(V)
for i in range(N):
    w[i] += P[i]
i = int(0)
for l in V:
    for j in l:
#        print(i, j)
        w[j] += w[i]
#        print(w)
    i +=1


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

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
lists=[0 for i in range(n-1)]
anslist=[0 for i in range(n+1)]
qlist=[0 for j in range(q)]
for i in range(n-1):
    a,b=map(int,input().split())
    lists[i]=(a,b)
lists=sorted(lists,key=lambda x:x[0],reverse=True)
kidic=[[j] for  j in range(n+1)]
for i in lists:
    kidic[i[0]]+=kidic[i[1]]
for j in range(q):
    a,b=map(int,input().split())
    for j in set(kidic[a]):
        anslist[j]+=b
del anslist[0]
print(*anslist)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
lists=[0 for i in range(n-1)]
anslist=[0 for i in range(n+1)]
qlist=[0 for j in range(q)]
for i in range(n-1):
    a,b=map(int,input().split())
    lists[i]=(a,b)
lists=sorted(lists,key=lambda x:x[0],reverse=True)
kidic=[[j] for  j in range(n+1)]
for i in lists:
    kidic[i[0]]+=kidic[i[1]]

for j in range(q):
    a,b=map(int,input().split())
    for j in kidic[a]:
        anslist[j]+=b
del anslist[0]
print(*anslist)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
lists=[0 for i in range(n-1)]
anslist=[0 for i in range(n+1)]
qlist=[0 for j in range(q)]
for i in range(n-1):
    a,b=map(int,input().split())
    lists[i]=(a,b)
lists=sorted(lists,key=lambda x:x[0],reverse=True)
kidic={ j:[j] for  j in range(1,n+1)}
for i in lists:
    kidic[i[0]]+=kidic[i[1]]
for j in range(q):
    a,b=map(int,input().split())
    for j in kidic[a]:
        anslist[j]+=b
del anslist[0]
print(*anslist)

Submission status: Wrong Answer

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

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

for i in range(Q):
    p, x = map(int, input().split())
    stack = [p]
    visit = [0]*N
    while stack:
      v = stack.pop(0)
      visit[v-1] = 1
      for i in G[v-1]:
         if not visit[i-1]:
            visit[i-1] = 1
            stack.append(i)
    rlt = [rlt[i] + visit[i] * x for i in range(N)]  

print(*rlt)

Submission status: Wrong Answer

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

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

for i in range(Q):
    p, x = map(int, input().split())
    stack = [p]
    visit = [0]*N
    while stack:
      v = stack.pop(0)
      visit[v-1] = 1
      for i in G[v-1]:
         if not visit[i-1]:
            visit[i-1] = 1
            stack.append(i)
    rlt = [rlt[i] + visit[i] * x for i in range(N)]  

print(rlt)

Submission status: Wrong Answer

Solution code:
n , q = list(map(int,input().split()))
link = [[] for i in range(n)]
plus = [0]*n
ans = [0]*n
for i in range(n-1):
    a,b = list(map(int,input().split()))
    link[a-1].append(b)
for i in range(q):
    p,x = list(map(int,input().split()))
    plus[p-1] += x
#print(link)
#print(plus)
for i in range(n-1):
    idx = link[i]
    for j in range(len(idx)):
        plus[idx[j]-1] += plus[i]
#        print(plus)
#    print(i)
print(*plus)

Submission status: Wrong Answer

Solution code:
N,Q = map(int, input().split())
edge = [[int(e) for e in input().split()] for _ in range(N-1)]
question = [[int(q) for q in input().split()] for _ in range(Q)]

L = [[] for _ in range(N)]
for i in range(N-1):
    L[edge[i][0]-1].append(edge[i][1]-1)
    L[edge[i][1]-1].append(edge[i][0]-1)
    
qlist = [0]*N
for i in range(Q):
    qlist[question[i][0]-1] += question[i][1]
    
cnt = [0]*N
q = [[0, qlist[0]]]
cnt[0] = qlist[0]
while q:
    temp = q.pop(0)
    v = temp[0]
    c = temp[1]
    for i in L[v]:
        if cnt[i] == 0:
            q.append([i, c+qlist[i]])
            cnt[i] = c+qlist[i]
            
for i in range(N):
    print(cnt[i])

Submission status: Wrong Answer

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

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

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

for i in reversed(range(0, n-1)):
    for j in range(1, i+1):
        if e[i-j][i] == 1:
            for k in range(1, n-i):
                e[i-j][i+k] += e[i][i+k]
        else:
            continue

for i in range(n):
    for j in range (n):
        e[i][j] *= point[i]
        ans[j] += e[i][j]

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)


def PX(T, P, X):
    if not P:
        return 0
    for p in P:
        ans[p] += X
        PX(p, memo[p], ans[p])
    return 0


N, Q = map(int, input().split())
memo = [[] for _ in range(N + 1)]
ans = [0 for _ in range(N + 1)]
for i in range(1, N):
    a, b = map(int, input().split())
    memo[a] += [b]
for j in range(Q):
    p, x = map(int, input().split())
    ans[p] += x
memo.sort()
PX(1, memo[1], ans[1])
ans.pop(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)


def PX(T, P, X):
    if not P:
        return 0
    for p in P:
        ans[p] += X
        PX(p, memo[p], ans[p])
    return 0


N, Q = map(int, input().split())
memo = [[] for _ in range(N + 1)]
ans = [0 for _ in range(N + 1)]
for i in range(1, N):
    a, b = map(int, input().split())
    memo[a] += [b]
for j in range(Q):
    p, x = map(int, input().split())
    ans[p] += x
PX(1, memo[1], ans[1])
ans.pop(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)


def PX(T, P, X):
    if not P:
        return 0
    for p in P:
        ans[p] += X
        PX(p, memo[p], ans[p])
    return 0


N, Q = map(int, input().split())
memo = [[] for _ in range(N + 1)]
ans = [0 for _ in range(N + 1)]
for i in range(1, N):
    a, b = map(int, input().split())
    memo[a] += [b]
for j in range(Q):
    p, x = map(int, input().split())
    ans[p] += x
PX(1, memo[1], ans[1])
ans.pop(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ab = []
ans = [0 for _ in range(N + 1)]
for i in range(1, N):
    ab.append(list(map(int, input().split())))
for j in range(Q):
    p, x = map(int, input().split())
    ans[p] += x
ab.sort()
for a, b in ab:
    ans[b] += ans[a]
ans.pop(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q=map(int,input().split())
to=[[] for _ in range(N)]
count=[0]*N
for _ in range(N-1):
    a,b=map(int, input().split())
    to[a-1].append(b-1)
for _ in range(Q):
    p,x=map(int, input().split())
    count[p-1]+=x
q = deque()
q.append(0)
p=[True]*N
while q:
    i = q.pop()
    if p[i]:
        p[i]=False
        for j in to[i]:
            if p[j]:
                count[j]+=count[i]
                q.append(j)
print(*count)

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q=map(int,input().split())
to=[[] for _ in range(N)]
count=[0]*N
for _ in range(N-1):
    a,b=map(int, input().split())
    to[a-1].append(b-1)
for _ in range(Q):
    p,x=map(int, input().split())
    count[p-1]+=x
q = deque()
q.append(0)
while q:
    i = q.pop()
    for j in to[i]:
        count[j]+=count[i]
        q.append(j)
print(*count)

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
to=[[] for _ in range(N)]
count=[0]*N
P=[True]*N
for _ in range(N-1):
    a,b=map(int, input().split())
    to[a-1].append(b-1)
for _ in range(Q):
    p,x=map(int, input().split())
    count[p-1]+=x
for i in range(N):
    if P[i]:
        P[i]=False
        for j in to[i]:
            if P[j]:
                count[j]+=count[i]
print(*count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
tree=[[] for _ in range(N)]
counter=[0]*N
for _ in range(N-1):
    a,b=map(int, input().split())
    tree[a-1].append(b-1)
for _ in range(Q):
    p,x=map(int, input().split())
    counter[p-1]+=x
for i in range(N):
    for j in tree[i]:
        counter[j]+=counter[i]
counter=list(map(str, counter))
print(' '.join(counter))

Submission status: Wrong Answer

Solution code:
n,q = list(map(int,input().split()))
r = [0]*n
ans = [0]*n
for i in range(n-1):
  a,b = list(map(int,input().split()))
  r[b-1] = a-1
for j in range(q):
  p,x = list(map(int,input().split()))
  ans[p-1] += x
for k in range(1,n):
  ans[k] += ans[r[k]]
print(*ans)

Submission status: Wrong Answer

Solution code:
n,q = list(map(int,input().split()))
r = [0]*n
ans = [0]*n
for i in range(n-1):
  a,b = list(map(int,input().split()))
  r[b-1] = a-1
for j in range(q):
  p,x = list(map(int,input().split()))
  ans[p-1] += x
for k in range(1,n):
  ans[k] += ans[r[k]]
print(ans)

Submission status: Wrong Answer

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

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

ans = [0 for i in range(n)]
for i in range(n):
    for j in t[i]:
        ans[j] += s[i]
print(*ans[0:])

Submission status: Wrong Answer

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

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

ans = [0 for i in range(n)]
for i in range(n):
    for node in t[i]:
        ans[node] += s[i]
print(ans[0:])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

que = deque([])
for i in range(q):
    flg = [0]*(n+1)
    p,x = map(int,input().split())
    que.append(p)
    while len(que)>0:
        pos = que.popleft()
        if flg[pos] == 1:
            continue
        else:
            flg[pos] = 1
            ans[pos] += x
            que.extend(graph[pos])

print(*ans[1:])

Submission status: Wrong Answer

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

R=[[] for i in range(N)]
ans=[0]*N
amax=0
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    R[a].append(b)
    amax=max(amax,a)

for i in range(amax+1):
    for j in R[i]:
        R[i].extend(R[j])

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

print(*ans)

Submission status: Wrong Answer

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

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

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

for i in range(N):
    for j in R[i]:
        R[i].extend(R[j])

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

print(*ans)

Submission status: Wrong Answer

Solution code:
from functools import lru_cache

Submission status: Wrong Answer

Solution code:
N , Q = map(int,input().split())
q = []
graph = [[] for _ in range(N)]
point = [0] * N
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a - 1].append(b - 1)
for _ in range(Q):
    a , b = map(int,input().split())
    a = a - 1
    point[a] += b
    q.extend(graph[a])
    while q:
        work = q.pop()
        point[work] += b
        q.extend(graph[work])
print(*point)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
table = [ [] for _ in range(N) ]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    table[a].append(b)
task = [0 for _ in range(N)]
count = [0 for _ in range(N)]
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    count[p] += x

que = deque([0])
while que:
    y = que.popleft()
    for chld in table[y]:
        que.append(chld)
        count[chld] += count[y]
print(" ".join( map(str, count) ))

Submission status: Wrong Answer

Solution code:
from collections import deque
N , Q = map(int,input().split())
q = deque()
graph = [[] for _ in range(N)]
point = [0] * N
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a - 1].append(b - 1)
for _ in range(Q):
    a , b = map(int,input().split())
    a = a - 1
    #print(graph[a])
    point[a] += b
    q.extend(graph[a])
    #print(q)
    while q:
        work = q.pop()
        #print(work)
        point[work] += b
        q.extend(graph[work])

print(*point)

Submission status: Wrong Answer

Solution code:
from collections import deque
n, q = map(int, input().split())

graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
    # print(a, b)

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

# print(nodes)
result = [0 for _ in range(n + 1)]

# que = [(1, nodes[1])]
que = deque([(1, nodes[1])])

visited = [False] * (n + 1)
while que:
    i, cost = que.popleft()
    result[i] = cost
    if visited[i]:
        continue
    visited[i] = True
    for sub in graph[i]:
        que.append((sub, cost + nodes[sub]))
print(*result[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque
n, q = map(int, input().split())

graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    # print(a, b)

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

# print(nodes)
result = [0 for _ in range(n + 1)]

# que = [(1, nodes[1])]
que = deque([(1, nodes[1])])

visited = [False] * (n + 1)
while que:
    i, cost = que.popleft()
    result[i] = cost
    if visited[i]:
        continue
    visited[i] = True
    for sub in graph[i]:
        que.append((sub, cost + nodes[sub]))
print(*result[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque
n, q = map(int, input().split())

graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    # print(a, b)

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

# print(nodes)
result = [0 for _ in range(n + 1)]

# que = [(1, nodes[1])]
que = deque([(1, nodes[1])])

while que:
    i, cost = que.popleft()
    result[i] = cost
    for sub in graph[i]:
        que.append((sub, cost + nodes[sub]))
print(*result[1:])

Submission status: Wrong Answer

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

graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    # print(a, b)

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

# print(nodes)
# result = [0 for _ in range(n + 1)]

for i in range(1, n + 1):
    for sub in graph[i]:
        nodes[sub] += nodes[i]

# que = [(1, nodes[1])]
#
# while que:
#     i, cost = que.pop(0)
#     result[i] = cost
#     for sub in graph[i]:
#         que.append((sub, cost + nodes[sub]))
print(*nodes[1:])

Submission status: Wrong Answer

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

graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    # print(a, b)

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

# print(nodes)
result = [0 for _ in range(n + 1)]

que = [(1, nodes[1])]

while que:
    i, cost = que.pop(0)
    result[i] = cost
    for sub in graph[i]:
        que.append((sub, cost + nodes[sub]))
print(*result[1:])

Submission status: Wrong Answer

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

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

graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    # print(a, b)

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

# print(nodes)
result = [0 for _ in range(n + 1)]
def dfs(i=1, cost=nodes[1]):
    result[i] = cost
    for sub in graph[i]:
        dfs(sub, cost + nodes[sub])
dfs()
print(*result[1:])