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 sys
sys.setrecursionlimit(int(1e+6))
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

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

results = [0 for _ in range(N)]
#%%
def repeat(node, counter, pre_node):
    counter =  counters[node]
    for candidate in branch[node]:
        if candidate == pre_node:
            continue
        else:
            repeat(candidate, counter, node)
    results[node] = counter

repeat(0, 0, 0)

print((" ").join([str(i) for i in results]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, Q = map(int, readline().split())
G = [[] for _ in range(N)]
score = [0]*N
for i in range(N-1):
  x,y = map(int, readline().split())
  G[x-1].append(y-1)
def dfs(G,a,point):
  score[a] += point
  for b in G[a]:
    dfs(G,b,point)
for _ in range(Q):
  a,point = map(int, readline().split())
  dfs(G,a-1,point)
print(*score)

Submission status: Wrong Answer

Solution code:
from collections import deque

def solve(N, children, weights):
    ret = [0 for _ in range(N + 1)]
    stack = deque()
    stack.append([0, 1])
    while stack:
        weights_sum, node = stack.pop()
        weights_sum += weights[node]
        ret[node] = weights_sum

        if not children[node]:
            continue

        for c in children[node]:
            stack.append([weights_sum, c])

    for idx in range(1, N + 1):
        print(ret[idx], end=" ")

if __name__ == "__main__":
    N, Q = map(int, input().split())
    children = [[] for _ in range(N + 1)]

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

    weights = [0 for _ in range(N + 1)]

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

    solve(N, children, weights)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    g[a - 1].append(b - 1)
res = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    res[p - 1] += x
s = [0]
d = [True] * n
while s:
    p = s.pop()
    d[p] = False
    for node in g[p]:
        if d[node]:
            res[node] += res[p]
            s.append(node)
print(' '.join(map(str, res)))

Submission status: Wrong Answer

Solution code:
import collections

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

tree = [[] for _ in range(n)]
edge = collections.deque(edge)
while len(edge) != 0:
    a, b = edge.popleft()
    tree[a - 1].append(b - 1)
    tree[b - 1].append(b - 1)

que = collections.deque([0])
flag = [False] * n
while len(que) != 0:
    i = que.popleft()
    flag[i] = True
    for j in tree[i]:
        if flag[j]: continue
        counter[j] += counter[i]
        que.append(j)

print(' '.join([str(c) for c in counter]))

Submission status: Wrong Answer

Solution code:
import collections

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

tree = [[] for _ in range(n)]
edge = collections.deque(edge)
while len(edge) != 0:
    a, b = edge.popleft()
    tree[a - 1].append(b - 1)
    tree[b - 1].append(b - 1)

que = collections.deque([0])
flag = [False] * n
while len(que) != 0:
    i = que.popleft()
    flag[i] = True
    for j in tree[i]:
        if tree[j]: continue
        counter[j] += counter[i]
        que.append(j)

print(' '.join([str(c) for c in counter]))

Submission status: Wrong Answer

Solution code:
import collections

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

tree = [[] for _ in range(n)]
edge = collections.deque(edge)
# flag = [False] * n
# flag[0] = True
while len(edge) != 0:
    a, b = edge.popleft()
    tree[a - 1].append(b - 1)
    # if flag[a - 1]:
    #     tree[a - 1].append(b - 1)
    #     flag[b - 1] = True
    # elif flag[b - 1]:
    #     tree[b - 1].append(a - 1)
    #     flag[a - 1] = True
    # else:
    #     edge.append((a, b))

que = collections.deque([0])
while len(que) != 0:
    i = que.popleft()
    for j in tree[i]:
        counter[j] += counter[i]
        que.append(j)

print(' '.join([str(c) for c in counter]))

Submission status: Wrong Answer

Solution code:
import sys
import collections
sys.setrecursionlimit(4 * 10 ** 5)

n, q = map(int, input().split())
edge = [tuple(map(int, input().split())) for _ in range(n - 1)]
query = [tuple(map(int, input().split())) for _ in range(q)]

tree = [[] for _ in range(n)]
for a, b in edge:
    tree[a - 1].append(b - 1)
p = [0] * n
for x, y in query:
    p[x - 1] += y
counter = [0] * n

que = collections.deque([0])
while len(que) != 0:
    i = que.popleft()
    counter[i] += p[i]
    for j in tree[i]:
        counter[j] = counter[i]
        que.append(j)

print(' '.join([str(c) for c in counter]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(500000)

n, q, *d = map(int, open(0).read().split())
res = [0] * n
edge = [[] for _ in res]

for a, b in zip(d[:2*n-2:2],d[1:2*n-2:2]):
    a -= 1;b -= 1
    edge[a] += b,
    edge[b] += a,

def dfs(v, p):
    for j in edge[v]:
        if j != p:  # 親は弾く
            res[j] += res[v]
            dfs(j, v)

for p, x in zip(d[2*n-2::2],d[2*n-1::2]):
    p -= 1
    res[p] += x

dfs(0, -1)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, q = map(int, input().split())
d = {}
for _ in range(n-1):
    a, b = map(int, input().split())
    if a not in d:
        d[a] = [b, None, 0]
    else:
        if d[a][0] is None:
            d[a][0] = b
        else:
            d[a][1] = b
    if b not in d:
        d[b] = [None, None, 0]
for _ in range(q):
    p, x = map(int, input().split())
    dq = deque([p])
    while len(dq) > 0:
        t = dq.popleft()
        d[t][2] += x
        if d[t][0]:
            dq.append(d[t][0])
        if d[t][1]:
            dq.append(d[t][1])
print(*[d[i][2] for i in range(1, n+1)])

Submission status: Wrong Answer

Solution code:
print("a")

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = lambda: sys.stdin.readline().rstrip()


def solve():
    N, Q = map(int, input().split())
    edge = [[] for _ in range(N)]
    counter = [0 for _ in range(N)]

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

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

    q = deque()
    q.append(0)
    d = [-1 for _ in range(N)]
    d[0] = 1

    while q:
        v = q.popleft()
        for n_v in edge[v]:
            if d[n_v] == -1:
                counter[n_v] += counter[v]
                d[n_v] = 1
                q.append(n_v)

    for i in range(N):
        print(str(counter[i]) + ' ', end='')
    print()


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

ls = deque([0])
while len(ls) > 0:
    root = ls.popleft()

    for next_v in G[root]:
        count[next_v] += count[root]
        ls.append(next_v)

for i in range(N):
    if i == N-1:
        print(count[i])
    else:
        print(count[i], end=" ")

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

ls = deque([0])
while len(ls) > 0:
    root = ls.popleft()

    for next_v in G[root]:
        count[next_v] += count[root]
        ls.append(next_v)

for i in range(N):
    print(count[i], end=" ")

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

ls = deque([0])
while len(ls) > 0:
    root = ls.popleft()
    print(count[root], end=" ")

    for next_v in G[root]:
        count[next_v] += count[root]
        ls.append(next_v)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

ab = [[int(i) for i in input().split()] for j in range(N-1)]
G = [[] for i in range(N)]
check = [-1 for i in range(N)]
for i in range(N-1):
    a, b = ab[i]
    G[a-1].append(b-1)
    check[b-1] = 1

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

root = deque([])
for i in range(N):
    if check[i] == -1:
        root.append(i)
        break

while len(root) > 0:
    r = root.popleft()
    ans = count[r]
    print(ans, end=" ")

    for i in G[r]:
        count[i] += ans
        root.append(i)

Submission status: Wrong Answer

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

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

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

for i in range(N):
    ans = count[i]
    if G[i]:
        for j in range(len(G[i])):
            count[G[i][j]] += ans
    print(ans, end=" ")

Submission status: Wrong Answer

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

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

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

for i in range(N):
    ans = count[i]
    if G[i]:
        ans += count[G[i][0]]
    print(ans, end=" ")
    count[i] = ans

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ab = [[0, 0]] * (N - 1)
for i in range(N - 1):
    ab[i] = list(map(int, input().split()))
ab = sorted(ab, key=lambda x: x[0])
nodes = [0] * (N)
for i in range(Q):
    num, pt = list(map(int, input().split()))
    nodes[num-1] += pt
for a, b in ab:
    nodes[b - 1] += nodes[a - 1]
print(' '.join(map(str, nodes)))

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ab = [[0, 0]] * (N - 1)
for i in range(N - 1):
    ab[i] = list(map(int, input().split()))
ab = sorted(ab, key=lambda x: x[0])
nodes = [0] * (N)
for i in range(Q):
    num, pt = list(map(int, input().split()))
    nodes[num-1] = pt
for a, b in ab:
    nodes[b - 1] += nodes[a - 1]
print(' '.join(map(str, nodes)))

Submission status: Wrong Answer

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

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[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 i in range(n-1):
    a,b = map(int,input().split())
    ki[b] = a

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[ki[i]]
print("".join(map(str,c[1:])))

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import defaultdict

if __name__ == '__main__':
    n, q = map(int, stdin.readline().rstrip().split())
    ab = [list(map(int, stdin.readline().rstrip().split())) for _ in range(n - 1)]
    px = [list(map(int, stdin.readline().rstrip().split())) for _ in range(q)]

    x_sorted = [0] * n
    for i in range(q):
        p = px[i][0]
        x = px[i][1]
        x_sorted[p - 1] += x

    # print(x_sorted)

    start = 0
    # コストテーブルの作成
    from heapq import heappop, heappush
    inf = float('inf')
    cost = dict() # コストテーブル
    for i, j in ab:
        c = x_sorted[i - 1]
        cost[i - 1] = cost.get(i - 1, []) + [(j - 1, c)]
    # print(cost)

    d = [inf for i in range(n)] # 最短距離
    d[start] = 0

    qq = [] # min-heap, (距離, 頂点)
    heappush(qq, (d[start], start))
    while qq:
        du, u = heappop(qq) # 最短距離とその頂点
        if d[u] < du:
            continue
        for v, weight in cost.get(u, []):
            alt = du + weight
            if d[v] > alt:
                d[v] = alt
                heappush(qq, (alt, v))

    ans = []
    for i in range(n):
        ans.append(d[i] + x_sorted[i])

    for _ans in ans:
        print(_ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q = map(int,input().split())
data = [[] for i in range(n)]
counter = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    data[a].append(b)
for i in range(q):
    p,x = map(int,input().split())
    p -= 1
    counter[p] += x
dq =deque([0])
while dq:
    r = dq.pop()
    if data[r]:
        for i in data[r]:
            counter[i] += counter[r]
            dq.append(i)
print(" ".join(map(str,counter)))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q = map(int,input().split())
data = [[] for i in range(n)]
counter = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    data[a].append(b)
for i in range(q):
    p,x = map(int,input().split())
    p -= 1
    dq = deque([p])
    while dq:
        r = dq.pop()
        counter[r] += x
        if data[r]:
            for j in data[r]:
                dq.append(j)
print(" ".join(map(str,counter)))

Submission status: Wrong Answer

Solution code:
def main():
    _map, _int, _input = map, int, input
    n, q = _map(_int, _input().split())
    t = [[] for _ in range(n)]
    r = [0]*n
    for _ in range(n-1):
        a, b = map(lambda x: _int(x)-1, _input().split())
        t[a].append(b)
        t[b].append(a) # leafの順序が保証されないため双方向にリンクを設定
    # root(t[0])からleafまで親ノードのリンクを消しながら辿る
    e = [0]
    eadd = e.append
    while len(e) > 0:
        i = e.pop()
        for j in t[i]:
            t[j].remove(i)
            eadd(j)
    
    query = [0] * n
    for _ in range(q):
        p, x = _map(_int, _input().split())
        query[p-1] += x
    
    for p, x in enumerate(query):
        p -= 1
        r[p] += x
        s = t[p][:]
        spop = s.pop
        sext = s.extend
        while len(s) > 0:
            i = spop()
            r[i] += x
            sext(t[i])
    print(*r)
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, q = map(int, input().split())
    t = [[] for _ in range(n)]
    r = [0]*n
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        t[a].append(b)
    #print("tree", t)
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        r[p] += x
        s = t[p][:]
        while len(s) > 0:
            #print("tree", t)
            #print("next", s)
            i = s.pop()
            r[i] += x
            s.extend(t[i])
        #print(">>", *r)
    print(*r)
            
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


if __name__ == "__main__":
    n,q = map(int,input().split())
    edge = [[] for i in range(n)]
    for i in range(n-1):
        a,b = map(int,input().split())
        a-=1
        b-=1
        edge[a].append(b)
        edge[b].append(a)
    cnt = [0]*n
    for i in range(q):
        p,x = map(int,input().split())
        cnt[p-1] += x
    for i in range(1,n):
        cnt[i] += cnt[i-1]
    print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n-1)]
ans = [0 for _ in range(n)]
ch = [[] for _ in range(n)]
pr = [0 for _ in range(n)]
for a,b in ab:
    a,b = min(a,b),max(a,b)
    pr[b-1] = a-1
    ch[a-1].append(b-1)

        
cntl = [0 for _ in range(n)]
for i in range(q):
    p,x = map(int,input().split())
    cntl[p-1] += x
#print(cntl)
#print(pr)
#print(ch)
for i in range(n):
    x = cntl[i]
    x += ans[pr[i]] 
    ans[i] = x

   

print(*ans)

Submission status: Wrong Answer

Solution code:
import collections
n,k = map(int, raw_input().split(' '))



###
#get the data
###
adj = collections.defaultdict(list)
for ii in range(n-1):
	u,v  = map(int,raw_input().split(' '))
	adj[u].append(v)

queries = collections.Counter()
for iii in range(k):
	u,v  = map(int,raw_input().split(' '))
	queries[u] += v

###
#BFS 
###
res = collections.Counter()
q = collections.deque([(1,0)])
while(q):
	u,count = q.popleft()
	count += queries[u]
	res[u] = count
	for  v in adj[u]: q.append((v,count))

print ' '.join([ str(res[u]) for u in range(1,n + 1)])

Submission status: Wrong Answer

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

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

graph = defaultdict(deque)

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

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


def solve(graph, arr):
    curr, root = 1, -1
    visited = [False] * (n + 1)

    def dfs(curr, root):
        stack = graph[curr]

        while stack:
            leaf = stack.pop()
            if visited[leaf]:
                continue
            arr[leaf] += arr[curr]
            visited[leaf] = True
            dfs(leaf, curr)

        return arr

    return dfs(curr, root)


ans = solve(graph, arr)

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
import copy

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

d = defaultdict(set)

for i in range(N - 1):
    a, b = map(int, input().split())
    d[a].add(b)
    for j in d.keys():
        if a in d[j]:
            d[j].add(b)

ans = [0] * (N + 1)

for i in range(Q):
    p, x = map(int, input().split())
    ans[p] += x
    q = deque(d[p])
    while q:
        curr = q.popleft()
        ans[curr] += x

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
import copy

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

d = defaultdict(deque)

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

ans = [0] * (N + 1)

for i in range(Q):
    p, x = map(int, input().split())
    ans[p] += x
    q = copy.deepcopy(d[p])
    while q:
        curr = q.popleft()
        ans[curr] += x
        if len(d[curr]) > 0:
            tmp = d[curr]
            for j in tmp:
                q.append(j)

print(*ans[1:])

Submission status: Wrong Answer

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

ab = [0] * (N+1)
for _ in range(N-1): # O(N)
    a, b = map(int, input().split())
    ab[b] = a

ab.sort()

#print(ab)

c = [0] * (N+1)
for _ in range(Q): # O(Q)
    p, x = map(int, input().split())
    c[p] += x
    
for i in range(2, N+1): # O(N)
    c[i] += c[ab[i]]
    
print(" ".join(map(str, c[1:])))

Submission status: Wrong Answer

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

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

ab.sort(reverse=True)

#print(ab)

top = [[] for i in range(N+1)]
for i in range(N-1): # O(N)
    top[ab[i][0]].append(ab[i][1])
    top[ab[i][0]] += top[ab[i][1]]
    
#print(top)

ans = [0] * (N+1)
for _ in range(Q): # O(Q*x)
    p, x = map(int, input().split())
    for j in set(top[p]):
        ans[j] += x
    ans[p] += x

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

Submission status: Wrong Answer

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

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

ab.sort(reverse=True)

#print(ab)

top = [[] for i in range(N+1)]
for i in range(N-1): # O(N)
    top[ab[i][0]].append(ab[i][1])
    top[ab[i][0]] += top[ab[i][1]]
    
#print(top)

ans = [0] * (N+1)
for _ in range(Q): # O(Q*x)
    p, x = map(int, input().split())
    for j in top[p]:
        ans[j] += x
    ans[p] += x

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
edges = [[] for _ in range(N)]
count = [0]*N

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

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

not_yet = [1]*N
not_yet[0] = 0
stack = [0]

while stack:
    parent = stack.pop()
    # print(parent)
    for child in edges[parent]:
        if not_yet[child]:
            count[child] += count[parent]
            not_yet[child] = 0
            stack.append(child)

print(*count)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
edges = [[] for _ in range(N)]
count = [0]*N

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

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

not_yet = [1]*N
not_yet[0] = 0
stack = [0]

while stack:
    parent = stack.pop()
    # print(parent)
    for child in edges[parent]:
        # if not_yet[child]:
        count[child] += count[parent]
        # not_yet[child] = 0
        stack.append(child)

print(*count)

Submission status: Wrong Answer

Solution code:
import sys


sys.setrecursionlimit(10 ** 6)


def dfs(v):
    for nv in g[v]:
        if nv in visited:
            continue
        visited.add(nv)
        counter[nv] += counter[v]
        dfs(nv)


def input():
    return sys.stdin.readline().strip()


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

stack = [0]
visited = {0}
while stack:
    v = stack.pop()
    for nv in g[v]:
        if nv in visited:
            continue
        stack.append(nv)
        visited.add(nv)
        counter[nv] += counter[v]
print(" ".join(map(str, counter)))

Submission status: Wrong Answer

Solution code:
import sys


sys.setrecursionlimit(10 ** 6)


def dfs(v):
    for nv in g[v]:
        if nv in visited:
            continue
        visited.add(nv)
        counter[nv] += counter[v]
        dfs(nv)


def input():
    return sys.stdin.readline().strip()


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

stack = [0]
visited = {0}
while stack:
    v = stack.pop()
    for nv in g[v]:
        if nv in visited:
            continue
        stack.append(nv)
        visited.add(nv)
        counter[nv] += counter[v]
print(*counter)

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


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

stack = [0]
visited = {0}
while stack:
    v = stack.pop()
    for nv in g[v]:
        if nv in visited:
            continue
        stack.append(nv)
        visited.add(nv)
        counter[nv] += counter[v]
print(*counter)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
from collections import deque


def main():
    input = stdin.buffer.readline
    n, q = map(int, input().split())
    g = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        g[a - 1].append(b - 1)
    counts = [0] * n
    for _ in range(q):
        p, x = map(int, input().split())
        counts[p - 1] += x

    q = deque()
    q.append(0)
    seen = [False] * n
    seen[0] = True

    while q:
        v = q.popleft()

        for n_v in g[v]:
            if seen[n_v]:
                continue
            seen[n_v] = True
            counts[n_v] += counts[v]
            q.append(n_v)

    print(*counts)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
from collections import deque


def main():
    input = stdin.buffer.readline
    n, q = map(int, input().split())
    g = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        g[a - 1].append(b - 1)
    counts = [0] * n
    for _ in range(q):
        p, x = map(int, input().split())
        counts[p - 1] += x

    q = deque()
    q.append(0)

    while q:
        v = q.popleft()

        for n_v in g[v]:
            counts[n_v] += counts[v]
            q.append(n_v)

    print(*counts)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n-1)]
px = [list(map(int, input().split())) for _ in range(q)]

tree = [[] for _ in range(n+1)]
for a,b in ab:
  tree[a].append(b)

ans = [0] * (n+1)
for p,x in px:
  ans[p] += x
  stack = deque(tree[p])
  while stack:
    t = stack.pop()
    ans[t] += x
    if tree[t]:
      for v in tree[t]: stack.append(v)
print(" ".join(map(str,ans[1:])))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200010)
input = sys.stdin.readline
def main():
    N, Q = map(int,input().split())
    G = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int,input().split())
        G[a-1].append(b-1)

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

    def dfs(ima):
        for tsugi in G[ima]:
            V[tsugi] += V[ima]
            dfs(tsugi)
    dfs(0)
    print(*V)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

ans_q = queue.Queue()
ans_q.put((0, ans[0]))

while not ans_q.empty():
    start = ans_q.get()
    cost = start[1]
    ans[start[0]] = cost
    for e in edge[start[0]]:
        ans_q.put((e, cost+ans[e]))

for i in range(n):
    print(ans[i], end=' ')
print()

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q=list(map(int,input().split()))
GR=[[] for _ in range(n)]
for i in range(n-1):
  a,b=list(map(int,input().split()))
  GR[a-1].append(b)
  
ans=[0 for _ in range(n)]
for i in range(q):
  p,x=list(map(int,input().split()))
  gr=deque([p]) 
  while gr:
    po=gr.popleft()
    ans[po-1] += x
    
    for no in GR[po-1]:
      gr.append(no)
      
print(' '.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
class Node:
    class DummyNode:
        def __init__(self):
            self.parent = None
            self.data = 0
        
    def __init__(self, parent):
        self.data = 0
        self.parent = parent
        #self.children = list()
        
    def __repr__(self):
        return str(self.data) + "," + str(self.children)
        
    #def add_counter(self, a):
    #    self.data += a
    #    for child in self.children:
    #        child.add_counter(a)
            
    def print_counter(self):
        self.data += self.parent.data
        print(self.data)
        
        
N,Q = map(int, input().split() )

dummy = Node.DummyNode()
nodes = [ Node(dummy) for i in range(N) ]

for _ in range(N-1):
    a,b = map(int, input().split() )
    #nodes[a-1].children.append(nodes[b-1])
    nodes[b-1].parent = nodes[a-1]
    
#for node in nodes:
#    print(node)

for _ in range(Q):
    p,x = map(int, input().split() )
    #nodes[p-1].add_counter(x)
    nodes[p-1].data += x
    
for node in nodes:
    node.print_counter()