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:
from collections import deque


def dfs(G, start, x):
    S = deque()
    child = deque()
    VL[start - 1] = 1
    Counter[start - 1] += x
    S.append(start)
    while len(S) > 0:
        node = S.pop()
        child.clear()
        for g in G:
            if g[0] == node and VL[g[1] - 1] == 0:
                child.append(g[1])
        if len(child) > 0:
            for c in child:
                VL[c - 1] = 1
                S.append(c)
                Counter[c - 1] += x


N, Q = map(int, input().split())
G = [tuple(map(int, input().split())) for _ in range(N - 1)]
PX = [tuple(map(int, input().split())) for _ in range(Q)]
VL = [0 for _ in range(N)]
Counter = [0 for _ in range(N)]

for p, x in PX:
    dfs(G, p, x)
    VL = [0 for _ in range(N)]

Counter = [str(a) for a in Counter]
print(" ".join(Counter))

Submission status: Wrong Answer

Solution code:
from collections import deque
n , q = map(int,input().split())
lot = [[] for i in range(n)]
ans = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int,input().split())
    lot[a-1].append(b-1)
    lot[b-1].append(a-1)
tyu = [0]*n
for i in range(q):
    p , x = map(int,input().split())
    tyu[p-1] += x

taiki = deque()
taiki.append(0)
owari = []
while  taiki:
    s = taiki.pop()
    for i in lot[s]:
        if owari.count(i) == 0:
            tyu[i] += tyu[s]
            taiki.append(i)
    owari.append(s)
print(tyu)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n, q = map(int, input().split())
child = defaultdict(list)
ns = set()
for i in range(n - 1):
    a, b = map(int, input().split())
    child[a].append(b)
    ns.add(a)
    ns.add(b)
cnt = defaultdict(int)
for i in range(q):
    p, x = map(int, input().split())
    cnt[p] += x
for i in sorted(ns):
    for j in child[i]:
        cnt[j] += cnt[i]
print(' '.join(str(value) for _, value in sorted(cnt.items(), key=lambda x: x[0])))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
#from math import floor, ceil
#from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [LI()for i in range(n)]
#inf = 10**17
#mod = 10**9 + 7

n,q=MI()
mydict={}
for i in range(n):
    mydict[i]=[]
#print(mydict)
for i in range(n-1):
    line=LI()
    mydict[line[0]-1].append(line[1]-1)
#print(mydict)

nodes=[0]*n
for i in range(q):
    p,r=MI()
    nodes[p-1]+=r

Q=deque()
Q.append(0)

while Q:
    a=Q.popleft()
    L=mydict[a]
    for k in L:
        Q.append(k)
        nodes[k]+=nodes[a]
    
print(" ".join(map(str,nodes)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
#from math import floor, ceil
#from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [LI()for i in range(n)]
#inf = 10**17
#mod = 10**9 + 7

n,q=MI()
tree=[LI()for i in range(n-1)]
nodes=[0]*n
for i in range(q):
    p,q=MI()
    nodes[p-1]+=q
#print(nodes)

q=deque()
q.append(1)
#print(tree)
#print(q)
while q:
    a=q.popleft()
    for tr in tree:
        #print(tr)
        if tr[0]==a:
            q.append(tr[1])
            nodes[tr[1]-1]+=nodes[a-1]
    #print(q)
#print(nodes)
print(" ".join(map(str,nodes)))

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict

N, Q = [int(i) for i in input().split(' ')]
Graph = defaultdict(deque)
for _ in range(N-1):
    a, b = [int(i) for i in input().split(' ')]
    Graph[a].append(b)

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

stack = deque([1])
arrive = [0] * N
while stack:
    v = stack[-1]
    arrive[v-1] = 1
    if Graph[v]:
        w = Graph[v].popleft()
        if arrive[w-1] == 0:
            stack.append(w)
            point[w-1] += point[v-1]

    else:
        stack.pop()

s = ' '.join(str(n) for n in point)
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict

N, Q = [int(i) for i in input().split(' ')]
Graph = defaultdict(deque)
for _ in range(N-1):
    a, b = [int(i) for i in input().split(' ')]
    Graph[a].append(b)

Point = defaultdict(int)
for _ in range(Q):
    a, b = [int(i) for i in input().split(' ')]
    Point[a] += b

stack = deque([1])
arrive = [0] * N
ans = [0] * N
p = Point.get(1,0)
while stack:
    v = stack[-1]
    # p += Point.get(v,0)
    # ans[v-1] = p
    arrive[v-1] = 1
    if Graph[v]:
        w = Graph[v].popleft()
        if arrive[w-1] == 0:
            stack.append(w)
            p += Point.get(w,0)

    else:
        ans[v-1] = p
        p -= Point.get(v,0)
        stack.pop()

s = ' '.join(str(n) for n in ans)
print(s)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
rela = [[] for i in range(N)]
for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    rela[a].append(b)
    #rela[b].append(a)
hoge = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    p -= 1
    hoge[p] += x
from collections import deque
que = deque([0])
while que:
    cur = que.popleft()
    for new in rela[cur]:
        if new == cur:
            continue
        hoge[new] += hoge[cur]
        que.append(new)
print(*hoge)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
rela = [[] for i in range(N)]
for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    rela[a].append(b)
hoge = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    p -= 1
    hoge[p] += x
from collections import deque
que = deque([0])
while que:
    cur = que.pop()
    for new in rela[cur]:
        hoge[new] += hoge[cur]
        que.append(new)
print(*hoge)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
rela = [[] for i in range(N)]
for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    rela[a].append(b)
hoge = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    p -= 1
    hoge[p] += x
from collections import deque
que = deque([0])
while que:
    cur = que.pop()
    for new in rela[cur]:
        hoge[new] += hoge[cur]
        que.append(new)
print(*hoge)

Submission status: Wrong Answer

Solution code:
from collections import deque
N,Q = map(int, input().split())
ab = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int, input().split())
    ab[a-1].append(b-1)

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

Q = deque([0])
while Q:
    parent = Q.popleft()
    for child in ab[parent]:
        counter[child] += counter[parent]
        Q.append(child)
print(*counter)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
def input():
    return sys.stdin.readline()[:-1]

N,Q=map(int,input().split())
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]=point[a]+b

def dfs(now,prev=-1):
    for i in graph[now]:
        if i==prev:
            continue
        point[i]=point[i]+point[now]
        dfs(i,now)
dfs(0)
print(*point)

Submission status: Wrong Answer

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

n,q = map(int,input().split())
points = [0]*(n+1)
tree = [[] for _ in range(n+1)]
for i in range(n-1):
    l, m = map(int,input().split())
    tree[l].append(m)

def dfs(e,p):
    points[e] += p
    for i in tree[e]:
        dfs(i,p)


for i in range(q):
    e, p = map(int, input().split())
    dfs(e,p)
print(*points[1:])
# print(tree)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


class Node:
    def __init__(self, parent, left):
        """
        Left-child, Right-sibiling
        """
        self.parent = parent
        self.left = left
        self.right = []
        self.point = 0


def get_depth_info(T, D, node: int, position: int):
    D[node] = position
    if T[node].left != NIL:
        get_depth_info(T, D, T[node].left, position + 1)
    if T[node].right:
        for idx in range(len(T[node].right)):
            r_child = T[node].right[idx]
            get_depth_info(T, D, r_child, position + 1)


def set_point(T, D, node, point):
    T[node].point += point
    node_depth = D[node]

    for i in range(N):
        if D[i] > node_depth:
            T[i].point += point


N, Q = map(int, input().split())
T = {k: Node(NIL, NIL) for k in range(N)}
D = [NIL] * N

for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if T[a].left == NIL:
        left_child = b
        T[a].left = left_child
        T[left_child].parent = a
    else:
        T[a].right.append(b)
        T[b].parent = a

get_depth_info(T, D, 0, 0)

for j in range(Q):
    p, x = map(int, input().split())
    p -= 1
    set_point(T, D, p, x)

scores = []
for i in range(N):
    scores.append(T[i].point)

print(*scores)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


class Node:
    def __init__(self, parent, left):
        """
        Left-child, Right-sibiling
        """
        self.parent = parent
        self.left = left
        self.right = []
        self.point = 0


def get_depth_info(T, D, node: int, position: int):
    D[node] = position
    if T[node].left != NIL:
        get_depth_info(T, D, T[node].left, position + 1)
    if T[node].right:
        for idx in range(len(T[node].right)):
            r_child = T[node].right[idx]
            get_depth_info(T, D, r_child, position + 1)


def set_point(T, D, node, point):
    T[node].point += point
    node_depth = D[node]

    for i in range(N):
        if D[i] > node_depth:
            T[i].point += point

    # if T[node].left != NIL:
    #     l_child = T[node].left
    #     set_point(T, l_child, point)
    #     # T[l_child].point += point
    #
    # if T[node].right:
    #     for idx in range(len(T[node].right)):
    #         r_child = T[node].right[idx]
    #         set_point(T, r_child,  point)
    #         # T[r_child].point += point


N, Q = map(int, input().split())
T = {k: Node(NIL, NIL) for k in range(N)}
D = [NIL] * N

for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if T[a].left == NIL:
        left_child = b
        T[a].left = left_child
        T[left_child].parent = a
    else:
        T[a].right.append(b)
        T[b].parent = a

root_node = NIL
for i in range(N):
    if T[i].parent == NIL:
        root_node = i

get_depth_info(T, D, root_node, 0)

for j in range(Q):
    p, x = map(int, input().split())
    p -= 1
    set_point(T, D, p, x)

scores = []
for i in range(N):
    scores.append(T[i].point)

print(*scores)

# def main():


# if __name__ == '__main__':
#     main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


class Node:
    def __init__(self, parent, left):
        """
        Left-child, Right-sibiling
        """
        self.parent = parent
        self.left = left
        self.right = []
        self.point = 0


def set_point(T, node, point):
    T[node].point += point

    if T[node].left != NIL:
        l_child = T[node].left
        set_point(T, l_child, point)
        # T[l_child].point += point

    if T[node].right:
        for idx in range(len(T[node].right)):
            r_child = T[node].right[idx]
            set_point(T, r_child,  point)
            # T[r_child].point += point


N, Q = map(int, input().split())
T = {k: Node(NIL, NIL) for k in range(N)}

for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if T[a].left == NIL:
        left_child = b
        T[a].left = left_child
        T[left_child].parent = a
    else:
        T[a].right.append(b)
        T[b].parent = a

# for i in range(N):
#     print(i, T[i])

for j in range(Q):
    p, x = map(int, input().split())
    p -= 1
    set_point(T, p, x)


scores = []
for i in range(N):
    scores.append(T[i].point)

print(*scores)

# def main():


# if __name__ == '__main__':
#     main()

Submission status: Wrong Answer

Solution code:
from collections import deque
N,Q= map(int, input().split())
tree = [[] for _ in range(N)]
counter = [0]*N
visited = [False]*N
q = deque([0])

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

while q:
    parent = q.popleft()
    visited[parent] = True
    for child in tree[parent]:
        if visited[child]:
            continue
        counter[child] += counter[parent]
        q.append(child)
print(*counter)

Submission status: Wrong Answer

Solution code:
import queue


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

ans = [0 for i in range(n)]
que = queue.Queue()
que.put((0, pts[0]))
while not que.empty():
    get = que.get()
    ans[get[0]] += get[1]
    for i in range(len(tree[get[0]])):
        que.put((tree[get[0]][i], get[1]+pts[tree[get[0]][i]]))
print(' '.join(map(str, ans)))

Submission status: Wrong Answer

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

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

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

for i in range(n):
    for j in edges[i]:
        counter[i]+=counter[j]
print(*counter)

Submission status: Wrong Answer

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

def dfs(i: int, c: int):
    for j in range(n):
        if edges[i][j]==1:
            dfs(j, c)
            counter[j]+=c



for i in range(q):
    a,b=map(int, input().split())
    counter[a-1]+=b
    que[a-1]+=b

for i in range(n):
    dfs(i, que[i])
print(*counter)

Submission status: Wrong Answer

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

def dfs(i: int, c: int):
    for j in range(n):
        if edges[i][j]==1:
            dfs(j, c)
            counter[j]+=c



for i in range(q):
    a,b=map(int, input().split())
    counter[a-1]+=b
    dfs(a-1, b)



print(*counter)

Submission status: Wrong Answer

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

def dfs(i: int, c: int):
    for j in range(n):
        if edges[i][j]==1:
            dfs(j, c)
            counter[j]+=c



for i in range(q):
    a,b=map(int, input().split())
    counter[a-1]+=b
    dfs(a-1, b)



print(counter)

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input = sys.stdin.readline
    N,Q=map(int, input().split())
    G=[[] for _ in range(N)]
    for _ in range(N-1):
        a,b=map(int, input().split())
        a,b=a-1,b-1
        G[a].append(b)
    C=[0]*N
    for _ in range(Q):
        p,x=map(int, input().split())
        C[p-1]+=x
    stk=[0]
    while stk:
        v = stk.pop()
        for to in G[v]:
            C[to]+=C[v]
            stk.append(to)
    print(*C)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
edge=[[]for _ in range(n)]
for _ in range(n-1):
  a,b=map(int,input().split())
  edge[a-1].append(b-1)
ans=[0]*n
for _ in range(q):
  p,x=map(int,input().split())
  ans[p-1]+=x
Q=[0]
while Q:
  P=[]
  for i in Q:
    for j in edge[i]:
      ans[j]+=ans[i]
      P.append(j)
  Q=P
print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = [int(i) for i in input().split()]
abs = []
for i in range(n - 1):
	abs.append([int(j) for j in input().split()])
abs = sorted(abs, key=lambda x:x[0])

parents = [0] * (n + 1)
for a, b in abs:
	parents[b] = a
    
pxs = []
for i in range(q):
	pxs.append([int(j) for j in input().split()])
pxs = sorted(pxs, key=lambda x:x[0])

scores = [0] * (n + 1)
for p, x in pxs:
	scores[p] += x

for i, v in enumerate(parents):
	scores[i] += scores[v]
    
ret = ''
for score in scores[1:]:
	ret += str(score) + ' '
print (ret[0:-1])

Submission status: Wrong Answer

Solution code:
n, k = list(map(int,input().split(' ')))
path = []
for i in range(n+1):
    path.append([])
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt
q = [1]
while q:
    v = q.pop(0)
    for next_v in path[v]:
        val[next_v] += val[v]
        q.append(next_v)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Wrong Answer

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

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

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

for v in vertexes[1:]:
    v[1] += vertexes[v[0]][1]

print(' '.join(str(v[1]) for v in vertexes[1:]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(150000)

n, k = list(map(int,input().split(' ')))
path = []
for i in range(n+1):
    path.append([])
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt
# DFS
def f(current):
    for next_v in path[current]:
        val[next_v] += val[current]
        if path[next_v]:
            f(next_v)

f(1)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Wrong Answer

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

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

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

for v in vertexes[1:]:
    v[1] += vertexes[v[0]][1]

print(' '.join(str(v[1]) for v in vertexes[1:]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)

n, k = list(map(int,input().split(' ')))
path = []
for i in range(n+1):
    path.append([])
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt
# DFS
def f(current):
    for next_v in path[current]:
        val[next_v] += val[current]
        if path[next_v]:
            f(next_v)

f(1)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200001)

n, k = list(map(int,input().split(' ')))
path = []
for i in range(n+1):
    path.append([])
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt
# BFS
# q = [1]
# while q:
#     v = q.pop(0)
#     for next_v in path[v]:
#         val[next_v] += val[v]
#         q.append(next_v)

# DFS
def f(current):
    for next_v in path[current]:
        val[next_v] += val[current]
        f(next_v)

f(1)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Wrong Answer

Solution code:
class Tree:
    def __init__(self):
        self.counter = 0
        self.parent = 0


n, q = list(map(int, input().split()))
vertexes = [Tree() for i in range(n + 1)]

# an = sorted([list(map(int, input().split())) for _ in range(n - 1)], key=lambda a: min(a))
an = [list(map(int, input().split())) for _ in range(n - 1)]
for a in an:
    a, b = min(a), max(a)
    vertexes[b].parent = a

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

for v in vertexes[1:]:
    v.counter += vertexes[v.parent].counter
print(' '.join(str(v.counter) for v in vertexes[1:]))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int,input().split(' ')))
path = []
for i in range(n+1):
    path.append([])
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a].append(b)
val = {}
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] = val.get(v, 0) + pt
q = [1]
while q:
    v = q.pop(0)
    for next_v in path[v]:
        val[next_v] = val.get(next_v, 0) + val.get(v, 0)
        q.append(next_v)
 
print(' '.join([str(val.get(i+1, 0)) for i in range(n)]))

Submission status: Wrong Answer

Solution code:
class Tree:
    def __init__(self):
        self.counter = 0
        self.parent = 0
        self.branches = set()

    def calc(self):
        return self.counter + (self.parent.calc() if self.parent else 0)


n, q = list(map(int, input().split()))
vertexes = [Tree() for i in range(n + 1)]
vertexes[0].branches.add(vertexes[1])

# an = sorted([list(map(int, input().split())) for _ in range(n - 1)], key=lambda a: min(a))
an = [list(map(int, input().split())) for _ in range(n - 1)]
for a in an:
    a, b = min(a), max(a)
    vertexes[a].branches.add(vertexes[b])
    vertexes[b].parent = a

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

for v in vertexes[1:]:
    v.counter += vertexes[v.parent].counter
print(' '.join(str(v.counter) for v in vertexes[1:]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100001)

n, k = list(map(int,input().split(' ')))
path = {}  # a -> b
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a] = path.get(a, [])
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt
def f(current):
    for next_v in path.get(current, []):
        val[next_v] += val[current]
        f(next_v)

f(1)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)


class Tree:
    def __init__(self):
        self.counter = 0
        self.parent = None
        self.branches = set()

    def calc(self):
        return self.counter + (self.parent.calc() if self.parent else 0)


n, q = list(map(int, input().split()))
vertexes = [Tree() for i in range(n + 1)]
vertexes[0].branches.add(vertexes[1])

an = sorted([list(map(int, input().split())) for _ in range(n - 1)], key=lambda a: min(a))
for a in an:
    a, b = min(a), max(a)
    vertexes[a].branches.add(vertexes[b])
    vertexes[b].parent = vertexes[a]

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

for v in vertexes[1:]:
    if v.parent:
        v.counter += v.parent.counter
print(' '.join(str(v.counter) for v in vertexes[1:]))

Submission status: Wrong Answer

Solution code:
n, q = [int(i) for i in input().split()]
abs = []
for i in range(n - 1):
	abs.append([int(j) for j in input().split()])
abs = sorted(abs, key=lambda x:x[0])

relations = [[i] for i in range(n)]
for a, b in abs:
	relations[b - 1] = [b - 1] + relations[a - 1]
    
scores = [0] * n
for i in range(q):
	p, x = [int(j) for j in input().split()]
	scores[p - 1] += x

ret = ''
for i in relations:
	score = sum([scores[j] for j in i])
	ret += str(score) + ' '
print (ret[0:-1])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)

n, k = list(map(int,input().split(' ')))
path = []
for i in range(n+1):
    path.append([])
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt
# BFS
# q = [1]
# while q:
#     v = q.pop(0)
#     for next_v in path[v]:
#         val[next_v] += val[v]
#         q.append(next_v)

# DFS
def f(current):
    for next_v in path[current]:
        val[next_v] += val[current]
        f(next_v)

f(1)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Wrong Answer

Solution code:
N, Q = [int(s) for s in input().split(' ')]

AB = [-1] * (N + 1)
AB[1] = 0
ABs = []
for n in range(N-1):
    a, b = [int(s) for s in input().split(' ')]
    if AB[a] == -1 and AB[b] == -1:
        ABs.append((a,b))
        continue
    if AB[a] >= 0:
        AB[b] = a
    else:
        AB[a] = b
next_ABs = []
while len(ABs) > 0:
    for a, b in ABs:
        if AB[a] == -1 and AB[b] == -1:
            next_ABs.append((a,b))
            continue
        if AB[a] >= 0:
            AB[b] = a
        else:
            AB[a] = b
    ABs = next_ABs
    next_ABs = []

H = [0] * (N + 1)

PQ = [0] * (N + 1)
for q in range(Q):
    p, q = [int(s) for s in input().split(' ')]
    PQ[p] += q

ret = [0] * (N + 1)
for n in range(1, N + 1):
    ret[n] = PQ[n]
    ret[n] += ret[AB[n]]

print(' '.join([str(r) for r in ret[1:]]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)


class Tree:
    def __init__(self):
        self.counter = 0
        self.parent = None
        self.branches = set()

    def calc(self):
        return self.counter + (self.parent.calc() if self.parent else 0)


n, q = list(map(int, input().split()))
vertexes = [Tree() for i in range(n + 1)]
vertexes[0].branches.add(vertexes[1])

for a, b in [list(map(int, input().split())) for _ in range(n - 1)]:
    vertexes[a].branches.add(vertexes[b])
    vertexes[b].parent = vertexes[a]

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

print(' '.join(str(v.calc()) for v in vertexes[1:]))

Submission status: Wrong Answer

Solution code:
N, Q = [int(s) for s in input().split(' ')]

AB = [0] * (N + 1)
for n in range(N-1):
    a, b = [int(s) for s in input().split(' ')]
    AB[b] = a

PQ = [0] * (N + 1)
for q in range(Q):
    p, q = [int(s) for s in input().split(' ')]
    PQ[p] += q

ret = [0] * (N + 1)
for n in range(1, N + 1):
    ret[n] = PQ[n]
    ret[n] += ret[AB[n]]

print(' '.join([str(r) for r in ret[1:]]))

Submission status: Wrong Answer

Solution code:
# とりあえずTLEかどうかだけ
n, k = list(map(int,input().split(' ')))
path = []
for i in range(n+1):
    path.append([])
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt
q = [1]
while q:
    v = q.pop(0)
    for next_v in path[v]:
        val[next_v] += val[v]
        q.append(next_v)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Wrong Answer

Solution code:
# pencil & paper
n, k = list(map(int,input().split(' ')))
path = {}  # a -> b
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a] = path.get(a, [])
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt

q = [1]
while q:
    v = q.pop(0)
    for next_v in path.get(v, []):
        val[next_v] += val[v]
        q.append(next_v)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.buffer.readline
n,q = map(int, input().split())
e = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    e[a-1].append(b-1)

ans = [0]*n

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

def dfs(i = 0, r = -1):
    for j in e[i]:
        if j != r:
            ans[j] += ans[i]
            dfs(j, i)

dfs()

print(*ans)