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 = list(map(int, input().split()))
#n = 4
#q = 3

ans = [0]*n
ki = {}
for i in range(1,n+1):
    ki[i] = []

#z = ["1 2", "2 3", "2 4"]

for i in range(n-1):
    e = list(map(int, input().split()))
    #e = list(map(int, z[i].split()))
    ki[e[0]].append(e[1])

#print(ki)

def df(graph, start, point):
    stack = [start]
    visited = []
    while stack:
        v = stack.pop(0)
        ans[v - 1] += point
        if v not in visited:
            visited.append(v)
            stack = graph.get(v, []) + stack

#y = ["2 10", "1 100", "3 1"]
for i in range(q):
    p = list(map(int, input().split()))
    #p = list(map(int, y[i].split()))
    df(ki, p[0], p[1])

print(' '.join(list(map(str, ans))))
#for n in ans:
#    print(n, end=" ")

Submission status: Wrong Answer

Solution code:
# Input N, Q
N, Q = [int(c) for c in input().split()]

# Input edges
edges = [[] for i in range(N)]
for i in range(N-1):
    u, v = [int(c)-1 for c in input().split()]
    edges[u].append(v)
    edges[v].append(u)

# Form tree
parent = [None for i in range(N)]
childs = [[] for i in range(N)]
def dfs(root):
    parent[root] = -1
    stack = [root]
    while stack:
        now = stack.pop()
        for nex in edges[now]:
            if parent[nex] is None:
                parent[nex] = now
                childs[now].append(nex)
                stack.append(nex)
dfs(0)

# Queries
incremented = [0 for i in range(N)]
for i in range(Q):
    v, add = [int(c) for c in input().split()]
    v -= 1
    incremented[v] += add

# Lower propagate
total = [0 for i in range(N)]
def propagate(root):
    global total
    stack = [root]
    while stack:
        now = stack.pop()
        total[now] += incremented[now]
        for child in childs[now]:
            total[child] += total[root]
            stack.append(child)
propagate(0)

# Print
print(" ".join(str(num) for num in total))

Submission status: Wrong Answer

Solution code:
import numpy as np
N,Q = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N-1)]
AB.reverse()
T = [[i] for i in range(N+1)]
for a,b in AB:
    T[a]+=T[b]
PQ = [list(map(int,input().split())) for _ in range(Q)]
X = np.array([0]*(N+1))
for p,q in PQ:
    X[T[p]] = X[T[p]] + q
print(*X[1:])

Submission status: Wrong Answer

Solution code:
import numpy as np
N,Q = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N-1)]
AB.reverse()
T = [[i] for i in range(N+1)]
for a,b in AB:
    T[a]+=T[b]
PQ = [list(map(int,input().split())) for _ in range(Q)]
X = [0]*N
for p,q in PQ:
    for i in T[p]:
        X[i-1]+=q
print(*X)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())

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

ans = [0] * n
pool = {}
s = []
def dfs(cur, num, ss):
  ans[cur-1] += num
  ss += (stack[cur])
  while len(ss) > 0:
    dfs(ss.pop(), num, ss)

for i in range(q):
  p, x = map(int, input().split())
  pool[p] = pool.get(p, 0) + x
for k, v in pool.items():
  dfs(k, v, s)
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n, q = map(int, input().split())
ab = defaultdict(list)
for i in range(n-1):
  a, b = map(int, input().split())
  ab[a].append(b)
px = defaultdict(int)
for i in range(q):
  p, x = map(int, input().split())
  px[p] += x

d = defaultdict(int)
for p in range(1, 1+n):
  x = px[p]
  d[p] += x
  try:
    for e in ab[p]:
      d[e] += d[p]
  except KeyError:
    break
ans = [d[i] for i in range(1, 1+n)]
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

tree = [[] for i in range(n+1)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    
result = [0]*(n+1)
for i in range(q):
  a, b = map(int, input().split())
  result[a] += b
    
que=deque()
que.append(1)
 
while que:
  n = que.popleft()
  for i in tree[n]:
    result[i] += result[n]
    que.append(i)
    
for i in result[1:]:
  print(i, end=" ")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())

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

ans = [0] * n
pool = {}
def dfs(cur, num):
	ans[cur-1] += num
	if len(stack[cur]) <= 0:
		return
	for c in stack[cur]:
		dfs(c, num)

for i in range(q):
  p, x = map(int, input().split())
  pool[p] = pool.get(p, 0) + x
for k, v in pool.items():
  dfs(k, v)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
n, q = map(int, input().split())

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

ans = [0] * n
pool = {}
def dfs(cur, num):
	ans[cur-1] += num
	if len(stack[cur]) <= 0:
		return 
	for c in stack[cur]:
		dfs(c, num)

for i in range(q):
  p, x = map(int, input().split())
  pool[p] = pool.get(p, 0) + x
for k, v in pool.items():
  dfs(k, v)
print(*ans)

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

n, q = map(int, sys.stdin.readline().split())

child = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int, sys.stdin.readline().split())
    child[a].append(b)

score = [0] * (n+1)
for i in range(q):
    p, x = map(int, sys.stdin.readline().split())
    score[p] += x

wait = [1]

while True:
    target = wait.pop()
    for nex in child[target]:
        score[nex] += score[target]
        wait.append(nex)
    if len(wait) == 0:
        break

score.pop(0)

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

Submission status: Wrong Answer

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

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+1)
for _ in range(Q):
	a,b=map(int,input().split())
	cost[a]+=b
for a in A:
	cost[a[1]] += cost[a[0]]
print(*cost[1:])

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+1)

for _ in range(Q):
	a,b=map(int,input().split())
	cost[a]+=b

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

Submission status: Wrong Answer

Solution code:
N,Q = map(int, input().split())
# 頂点関係情報をリスト化
AB=[list(map(int,input().split())) for _ in range(N-1)]
AB.sort()
# スコアを0で初期化
score=[0]*(N+1)
# クエリの数だけ繰り返す
for _ in range(Q):
	p,x=map(int,input().split())
	score[p]+=x
for elm in AB:
  # 子のスコアに親のスコアを足す
  score[elm[1]] += score[elm[0]]
print(*score[1:])

Submission status: Wrong Answer

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

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

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

for i in range(1, N):
    ans[i] += ans[root[i]]
print(*ans)

Submission status: Wrong Answer

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

# 頂点関係情報をリスト化
AB=[list(map(int,input().split())) for _ in range(N-1)]
AB.sort()

# スコアを0で初期化
score=[0]*(N+1)

# クエリの数だけ繰り返す
for _ in range(Q):
	p,x=map(int,input().split())
	score[p]+=x

for elm in AB:
  # 子のスコアに親のスコアを足す
  score[elm[1]] += score[elm[0]]
    
print(*score[1:])

Submission status: Wrong Answer

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

ans = (N+1) * [0]

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

for com in command:
    ans[com[0]] += com[1]
for num in nums:
    ans[num[1]] += ans[num[0]]

print(*ans[1:])

Submission status: Wrong Answer

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

def main():
    n, q = map(int,input().split())
    top_list = [list(map(int,input().split())) for i in range(n - 1)]
    ope_list = [list(map(int,input().split())) for i in range(q)]
 
    ans = [0] * n
 
    for i in ope_list:
#自分にスコアを加算
        ans[i[0] - 1] += i[1]
#自分にぶら下がる頂点にスコアを加算
        for j in top_list:
            if i[0] == j[0]:
                ans[j[1] -1] += i[1]
#自分の孫以下の頂点にスコアを加算
                for k in top_list:
                    if j[1] == k[0]:
                        ans[k[1] -1] += i[1]
 
    print(*ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n,q=map(int,input().split())
Edges=defaultdict(list)
for _ in range(n-1):
    a,b=map(int,input().split())
    Edges[a-1].append(b-1)
Nodes = [0]*n
for _ in range(q):
    p,x=map(int,input().split())
    Nodes[p-1] += x

def dfs(Edges,Nodes):
    visited = [False]*(len(Nodes))
    stack = []
    stack.append((0, 0))

    while len(stack)>0:
        current, preval = stack.pop()
        if visited[current]:
            continue
        visited[current] = True
        Nodes[current] += preval
        preval = Nodes[current]
        for i in Edges[current]:
            stack.append((i, preval))
    return Nodes

Nodes = dfs(Edges,Nodes)

print(" ".join(list(map(str,Nodes))))

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)]
parent = [0] * n
ans = [0] * n

for i in ab:
  a,b=i[0],i[1]
  parent[b-1] = a-1
  
for j in px:
  p,x = j[0], j[1]
  ans[p-1] += x

for key, i in enumerate(ans[1:]):
  key += 1
  ans[key] += ans[parent[key]]
print(*ans)

Submission status: Wrong Answer

Solution code:
import collections

N, Q = map(int, input().split())
lst = [[] for _ in range(N)]
ans = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    lst[b-1].append(a-1)
    lst[a-1].append(b-1)
print(lst)
for j in range(Q):
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
               
q=collections.deque()
q.append(0)
flag = [0]*N
while q:
    y = q.pop()
    flag[y] = 1
    for k in lst[y]:
        if flag[k] == 1:
            continue
        ans[k] += ans[y]
        q.append(k)
print(*ans)

Submission status: Wrong Answer

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

sys.setrecursionlimit(10000000)

N, Q = [int(x) for x in input().split()]
# V = [set() for _ in range(N)]
V = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    # V[a].add(b)
    V[a].append(b)

C = [0] * N

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

ANS = [0] * N


def dfs(n, val):
    ANS[n] += (val + C[n])
    for next in V[n]:
        dfs(next, val + C[n])


def main():
    dfs(0, 0)
    print(" ".join([str(x) for x in ANS]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
edges = [set() for i in range(n)]
for _ in range(n-1):
    a, b= map(int,input().split())
    edges[a-1].add(b-1)
cnt = [0] * n

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

l = [[0, cnt[0]]]
while len(l) != 0:
    a, b = l.pop(0)
    if a != 0:
        cnt[a] += b
    for i in edges[a]:
        l.append([i, cnt[a]])
print(*cnt)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
edges = [set() for i in range(n)]
for _ in range(n-1):
    a, b= map(int,input().split())
    edges[a-1].add(b-1)
cnt = [0] * n

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

l = [tuple([0, cnt[0]])]
while len(l) != 0:
    a, b = l.pop(0)
    if a != 0:
        cnt[a] += b
    for i in edges[a]:
        l.append(tuple([i, cnt[a]]))
print(*cnt)