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(200010)

def main():
    def input():
        return sys.stdin.readline()[:-1]

    N, Q = map(int,input().split())
    G = [[] for k in range(N)]

    D = [-1 for k in range(N)]
    D[0] = 0

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

    def kyori(ima):
        for tsugi in G[ima]:
            if D[tsugi] == -1:
                D[tsugi] = D[ima]+1
                kyori(tsugi)
    kyori(0)

    def dfs(ima):
        for tsugi in G[ima]:
            if D[ima] < D[tsugi]:
                ans[tsugi] += ans[ima]
                dfs(tsugi)
    dfs(0)

    print(*ans,sep=" ")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200010)

def main():
    def input():
        return sys.stdin.readline()[:-1]

    N, Q = map(int,input().split())
    G = [[] for k in range(N)]

    D = [-1 for k in range(N)]
    D[0] = 0

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

    def kyori(ima):
        for tsugi in G[ima]:
            if D[tsugi] == -1:
                D[tsugi] = D[ima]+1
                kyori(tsugi)
    kyori(0)

    def dfs(ima):
        for tsugi in G[ima]:
            ans[tsugi] += ans[ima]
            if D[ima] < D[tsugi]:
                dfs(tsugi)
    dfs(0)

    print(*ans,sep=" ")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200010)
def main():
    def input():
        return sys.stdin.readline()[:-1]

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

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
def main():
    def input():
        return sys.stdin.readline()[:-1]

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

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
a = []
b = []
for i in range(N-1):
    a.append(list(map(int, input().split())))
for i in range(Q):
    b.append(list(map(int, input().split())))
class Tree():
    def __init__(self):
        self.leaf = []
        self.num = 0
        self.data = 0

def plus(treenum, num):
    tree[treenum].num += tree[treenum].data
    tree[treenum].num += num
    num = num + tree[treenum].data
    #print(treenum, num)
    for i in range(len(tree[treenum].leaf)):
        plus(tree[treenum].leaf[i] ,num)


tree = []
for i in range(N):
    tree.append(Tree())

for i in range(N - 1):
    tree[a[i][0] - 1].leaf.append(a[i][1] - 1)

for i in range(Q):
    tree[b[i][0] - 1].data += b[i][1]

plus(0, 0)

for i in range(N):
    print(tree[i].num, end = " ")

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
a = []
b = []
for i in range(N-1):
    a.append(list(map(int, input().split())))
for i in range(Q):
    b.append(list(map(int, input().split())))
class Tree():
    def __init__(self):
        self.leaf = []
        self.num = 0
tree = []
for i in range(N):
    tree.append(Tree())
def plus(treenum, num):
    tree[treenum].num += num
    #print(treenum, num)
    for i in range(len(tree[treenum].leaf)):
        plus(tree[treenum].leaf[i] ,num)


for i in range(N - 1):
    tree[a[i][0] - 1].leaf.append(a[i][1] - 1)

for i in range(Q):
    plus(b[i][0] -1, b[i][1])
for i in range(N):
    print(tree[i].num, end = " ")

Submission status: Wrong Answer

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

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

counter = [0 for _ in range(n)] 

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

qux = [0]

while qux:
    x = qux.pop()
    for ko in ki[x]:
        counter[ko] += counter[x]
        qux.append(ko)

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

Submission status: Wrong Answer

Solution code:
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)]

v = [[] for _ in range(n)]
for i in range(n-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    v[a].append(b)
    v[b].append(a)
    
f = [0] * (n + 1)
for i in range(q):
    p = px[i][0] - 1
    f[p] += px[i][1]

Submission status: Wrong Answer

Solution code:
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)]

v = [[] for _ in range(n)]
for i in range(n-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    v[a].append(b)
    v[b].append(a)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
arr = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    arr[a-1].append(b)
    
ans = [0 for _ in range(n)] 

for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x
    
for i, c in enumerate(ans):
    for j in arr[i]:
        ans[j-1] += c
            
print(ans)

Submission status: Wrong Answer

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

for i in range(N - 1):
  fromNode, toNode = map(int, input().split())
  children[fromNode - 1].append(toNode - 1)
  
for i in range(Q):
  p, x = map(int, input().split())
  stack = [p - 1]
  while(len(stack) > 0):
    cur = stack.pop()
    result[cur] += x
    stack.extend(children[cur])
  
print(' '.join(map(str,result)))

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q = map(int,input().split())
AB = [tuple(int(i) for i in input().split()) for j in range(N-1)]
PX = [tuple(int(i) for i in input().split()) for j in range(Q)]

child = [[] for i in range(N+1)]
for a,b in AB: 
    child[a].append(b)

value = [0]*(N+1)
for p,x in PX:
    value[p] += x 

q = deque([1])
while q:
    i = q.popleft()
    for j in child[i]:
        q.append(j)
        value[j] += value[i]

ans = ' '.join(map(str,value[1:]))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q = map(int,input().split())
AB = [tuple(int(i) for i in input().split()) for j in range(N-1)]
PX = [tuple(int(i) for i in input().split()) for j in range(Q)]

# childのindex:ノードの番号
# indexのノードがもつ子ノードの番号のリストを収納
child = [[] for i in range(N+1)] #N
for a,b in AB: # (N-1)*2
    child[a].append(b) # 1+1
# N + 2*(N-1)*2 = 5*N -4

value = [0]*(N+1) # 1*(N+1)
for p,x in PX: # Q
    value[p] += x # 1+1
# 2*Q + (N+1)

q = deque([1]) # 1
while q:
    # print(q)
    i = q.popleft() # 1
    for j in child[i]: # 
        q.append(j)
        value[j] += value[i]

ans = ' '.join(map(str,value[1:]))
print(ans)

    
# par = [None] * (N+1)
# q = [1]
# while q:
#     x = q.pop()
#     for y in child[x]:
#         if y == par[x]:
#             continue
#         value[y] += value[x]
#         q.append(y)
#         par[y] = x

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
AB = [tuple(int(i) for i in input().split()) for j in range(N-1)]
PX = [tuple(int(i) for i in input().split()) for j in range(Q)]

# childのindex:ノードの番号
# indexのノードがもつ子ノードの番号のリストを収納
child = [[] for i in range(N+1)] #N
for a,b in AB: # (N-1)*2
    child[a].append(b) # 1+1
# N + 2*(N-1)*2 = 5*N -4

value = [0]*(N+1) # 1*(N+1)
for p,x in PX: # Q
    value[p] += x # 1+1
# 2*Q + (N+1)

stack = [1] # 1
while stack: # 
    i = stack.pop() # 1
    for j in child[i]: # 
        stack.append(j)
        value[j] += value[i]

ans = ' '.join(map(str,value[1:]))
print(ans)

    

# import sys
# readline = sys.stdin.readline
# readlines = sys.stdin.readlines

# N,Q = map(int,readline().split())
# AB = [tuple(int(x) for x in readline().split()) for _ in range(N-1)]
# PX = [tuple(int(x) for x in readline().split()) for _ in range(Q)]

# child = [[] for _ in range(N+1)]
# for a,b in AB:
#     child[a].append(b)
#     child[b].append(a)

# value = [0] * (N+1)
# for p,x in PX:
#     value[p] += x

# par = [None] * (N+1)
# q = [1]
# while q:
#     x = q.pop()
#     for y in child[x]:
#         if y == par[x]:
#             continue
#         value[y] += value[x]
#         q.append(y)
#         par[y] = x

# ans = ' '.join(map(str,value[1:]))
# print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import defaultdict 
from copy import copy

n, q = [int(i) for i in input().split()]
a = []
b = []
p = []
x = []

for i in range(n-1):
    a_, b_ = [int(i) for i in input().split()]
    a.append(a_-1)
    b.append(b_-1)
    
for i in range(q):
    p_, x_ = [int(i) for i in input().split()]
    p.append(p_-1)
    x.append(x_)


childlen = defaultdict(list)

for i in range(n-1):
    childlen[a[i]].append(b[i])
    
result = np.zeros(n, dtype=np.int)
for i in range(q):
    result[p[i]] += x[i]
    stack = copy(childlen[p[i]])
    while len(stack) > 0:
        s = stack.pop()
        result[s] += x[i]
        stack.extend(childlen[s])
        
print(" ".join(map(str, result)))

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q = map(int,input().split())
c = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    c[a-1].append(b-1)
px = deque([int(i) for i in input().split()] for j in range(q))
que = deque()
ans = [0 for i in range(n)]
while px:
    l = px.popleft()
    x = l[1]
    que.append(l[0]-1)
    while que:
        p = que.popleft()
        ans[p] += x
        for j in c[p]:
            que.append(j)
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q = map(int,input().split())
c = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    c[a-1].append(b-1)
px = [[int(i) for i in input().split()] for j in range(q)] #pはずれてる
que = deque()
ans = [0 for i in range(n)]
for i in range(q):
    que.clear()
    x = px[i][1]
    que.append(px[i][0]-1)
    while que:
        p = que.popleft()
        ans[p] += x
        for j in c[p]:
            que.append(j)
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q = map(int,input().split())
ab = [[int(i)-1 for i in input().split()] for j in range(n-1)]
px = [[int(i) for i in input().split()] for j in range(q)] #pはずれてる
c = [[] for i in range(n)]
que = deque()
ans = [0 for i in range(n)]
for i in range(n-1):
    c[ab[i][0]].append(ab[i][1])
for i in range(q):
    que.clear()
    x = px[i][1]
    que.append(px[i][0]-1)
    while que:
        p = que.popleft()
        ans[p] += x
        for j in c[p]:
            que.append(j)
for j in range(n):
    print(ans[j])

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q = map(int,input().split())
ab = [[int(i)-1 for i in input().split()] for j in range(n-1)]
px = [[int(i) for i in input().split()] for j in range(q)] #pはずれてる
c = [[] for i in range(n)]
que = deque()
ans = [0 for i in range(n)]
for i in range(n-1):
    c[ab[i][0]].append(ab[i][1])
for i in range(q):
    que.clear()
    x = px[i][1]
    que.append(px[i][0]-1)
    while que:
        p = que.popleft()
        ans[p] += x
        for j in c[p]:
            que.append(j)
for j in range(n):
    print(ans)

Submission status: Wrong Answer

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

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

stack = [0]
while len(stack) != 0:
    tmp = stack.pop()
    for i in graph[tmp]:
        stack.append(i)
        count[i] += count[tmp]

print (*count)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import collections
import sys

sys.setrecursionlimit(1000000)




def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * N

    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            add_counter(i,x)

    for pi, xi in zip(p, x):
        add_counter(pi,xi)

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

def solve2(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * N

    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            add_counter(i,x)

    for pi, xi in zip(p, x):
        add_counter(pi,xi)

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


def solve3(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * (N)
    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    for pi, xi in zip(p, x):
        counter[pi-1] += xi

    is_gone = set()

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            if i not in is_gone:
                is_gone.add(i)
                add_counter(i,counter[cur-1])


    add_counter(1,0)
    print(" ".join(map(str,counter)))






# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  # type: "List[int]" 
    x = [int()] * (Q)  # type: "List[int]" 
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve3(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import collections
import sys

sys.setrecursionlimit(1000000)




def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * N

    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            add_counter(i,x)

    for pi, xi in zip(p, x):
        add_counter(pi,xi)

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

def solve2(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * N

    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            add_counter(i,x)

    for pi, xi in zip(p, x):
        add_counter(pi,xi)

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


def solve3(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * (N)
    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    for pi, xi in zip(p, x):
        counter[pi-1] += xi

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            add_counter(i,counter[cur-1])

    add_counter(1,0)
    print(" ".join(map(str,counter)))






# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  # type: "List[int]" 
    x = [int()] * (Q)  # type: "List[int]" 
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve3(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n + 1)]
score = [0 for _ in range(n + 1)]

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

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

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


print(*score[1:])

Submission status: Wrong Answer

Solution code:
import sys
import collections


input = sys.stdin.readline
n, qq = list(map(int,input().rstrip().split()))

ls = collections.defaultdict(list)
ls[1]

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

ans = [0] * n

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

q = collections.deque()
q.append(1)
fq = collections.defaultdict(list)
while True:
    if len(q)!=0:
        p = q.popleft()
        fq[p]
        for i in ls[p]:
            if i not in fq:
                q.append(i)
                ans[i-1] += ans[p-1]
    else:
        break


print(*ans)

Submission status: Wrong Answer

Solution code:
# ロジック

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

graph = []
ope = []

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

for i in range(Q):
    p, x=map(int, input().split())
    ope.append((p, x))

def getnext(Num):
    next = set()
    for t in graph:
        if (t[0] == Num):
            next.add(t[1])
            
    return next

result = [0]*(N+1)
next = set()

for o in ope:
    next.add(o[0])
    score = o[1]
    
    while next:
        n = next.pop()
        result[n] += score
        next |= getnext(n)

for i in result[1:]:
    print(i, end= " ")

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n, qq = list(map(int, input().rstrip('\n').split()))

    ls = collections.defaultdict(list)
    ls[1]
    for i in range(n-1):
        a, b = list(map(int, input().rstrip('\n').split()))
        ls[a] += [b]
        ls[b] += [a]

    ans = [0 for _ in range(n)]
    for i in range(qq):
        p, x = list(map(int, input().rstrip('\n').split()))
        ans[p-1] += x

    q = collections.deque()
    q.append(1)
    fq = collections.defaultdict(list)
    while True:
        if len(q) != 0:
            p = q.popleft()
            fq[p]
            for i in ls[p]:
                if i not in fq:
                    q.append(i)
                    ans[i-1] += ans[p-1]
        else:
            break
    print(ans)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
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, m = MAP()
G = [[]for i in range(n)]
for i in range(n-1):
    a,b = MAP()
    G[a-1].append(b-1)

ps = [(0,0)]*m
for i in range(m):
    p,x = MAP()
    ps[i] = (p-1,x)

res = [0]*n

for p in ps:
    res[p[0]] += p[1]

for i in range(n):
    for Gs in G[i]:
        res[Gs] += res[i]

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
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, m = MAP()
G = [[]for i in range(n)]
for i in range(n-1):
    a,b = MAP()
    G[a-1].append(b-1)

ps = [(0,0)]*m
for i in range(m):
    p,x = MAP()
    ps[i] = (p-1,x)

res = [0]*n
def dfs(vertex, value):
    res[vertex] += value
    if len(G[vertex]) == 0:
        return
    for Gs in G[vertex]:
        dfs(Gs,value)
    
for i in range(m):
    dfs(ps[i][0],ps[i][1])

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

Submission status: Wrong Answer

Solution code:
def dfs(v,p=-1):
  for u in graph[v]:
    if u == p: continue
    ans[u] += ans[v]
    dfs(u,v)

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

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

#dfs(1)
print(' '.join([str(a) for a in ans[1:]]))

Submission status: Wrong Answer

Solution code:
#D
from collections import deque
n,q=map(int,input().split())
data=[[] for i in range(n)]
counter=[0 for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    data[a].append(b)
for i in range(q):
    p,x=map(int,input().split())
    counter[p]+=x
for i in range(1,n):
    if data[i]:
        for j in range(len(data[i])):
            counter[data[i][j]]+=counter[i]
counter=deque(counter)
counter.popleft()
print(*counter)

Submission status: Wrong Answer

Solution code:
#D
from collections import deque
n,q=map(int,input().split())
data=[[] for i in range(n)]
counter=[0 for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    if a<b:
        data[a].append(b)
    else:
        data[b].append(a)
for i in range(q):
    p,x=map(int,input().split())
    counter[p]+=x
for i in range(1,n):
    if data[i]:
        for j in range(len(data[i])):
            counter[data[i][j]]+=counter[i]
counter=deque(counter)
counter.popleft()
print(*counter)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

def add_point(p,x):
  counter[p] += x
  queue = deque()
  queue.append(p)
  while len(queue) != 0:
    prv_node = queue.pop()
    for node in graph[prv_node]:
      counter[node] += x
      queue.append(node)
  
N,Q = map(int,input().split())
graph = defaultdict(list)
counter = {i:0 for i in range(1,N+1)}
for _ in range(N-1):
  a,b = map(int,input().split())
  graph[a].append(b)

prc = {}  
for _ in range(Q):
  p, x = map(int,input().split())
  if p not in prc: prc[p] = x
  else: prc[p] += x

for p, x in prc.items():
  add_point(p,x)

print(' '.join([str(counter[i]) for i in range(1,N+1)]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

def add_point(p,x):
  counter[p] += x
  queue = deque()
  queue.append(p)
  done = set([p])
  while len(queue) != 0:
    prv_node = queue.pop()
    for node in graph[prv_node]:
      if node in done: continue
      counter[node] += x
      queue.append(node)
      done.add(node)
  
N,Q = map(int,input().split())
graph = defaultdict(list)
counter = {i:0 for i in range(1,N+1)}
for _ in range(N-1):
  a,b = map(int,input().split())
  graph[a].append(b)
  graph[b].append(a)

for _ in range(Q):
  add_point(*map(int,input().split()))

print(' '.join([str(counter[i]) for i in range(1,N+1)]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

def add_point(p,x):
  counter[p] += x
  queue = deque()
  queue.append(p)
  while len(queue) != 0:
    prv_node = queue.pop()
    for node in graph[prv_node]:
      counter[node] += x
      queue.append(node)
  
N,Q = map(int,input().split())
graph = defaultdict(list)
counter = {i:0 for i in range(1,N+1)}
for _ in range(N-1):
  a,b = map(int,input().split())
  graph[a].append(b)

for _ in range(Q):
  add_point(*map(int,input().split()))

print(' '.join([str(counter[i]) for i in range(1,N+1)]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,Q = [int(x) for x in input().split()]
E ={i: [] for i in range(1, N+1)}
q = []
for _ in range(N-1):
  u,v = [int(x) for x in input().split()]
  E[u].append(v)
for _ in range(Q):
  u,v = [int(x) for x in input().split()]
  q.append((u,v))
ans = [0]*(N)
for i in range(Q):
  ans[q[i][0]-1] += q[i][1]
for i in range(1,N+1):
  for j in E[i]:
    ans[j-1] += ans[i-1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N,Q = [int(x) for x in input().split()]
E ={i: [] for i in range(1, N+1)}
q = []
for _ in range(N-1):
  u,v = [int(x) for x in input().split()]
  E[u].append(v)
for _ in range(Q):
  u,v = [int(x) for x in input().split()]
  q.append((u,v))
q.sort(key = lambda x:x[0])
ans = [0]*(N)
for i in range(Q):
  ans[q[i][0]-1] += q[i][1]
for i in range(1,N+1):
  for j in E[i]:
    ans[j-1] += ans[i-1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
T = [0] * (N+1)
V = [0] * (N+1)
# ある頂点の親だけを記録
for _ in range(N-1):
    a, b = map(int, input().split())
    T[b] = a
# ある頂点に対するポイントをまとめておく
for _ in range(Q):
    p, x = map(int, input().split())
    V[p] += x
# ある頂点の点数は、頂点そのものに対する処理点数 + 親の点数
for i in range(1, N+1):
    V[i] += V[T[i]]
print(*V[1:])

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
rs = [map(int, input().split()) for _ in range(N-1)]
ps = [map(int, input().split()) for _ in range(Q)]

cs = [0] * N
nodes = [set() for _ in range(N)]
for n1, n2 in rs:
    nodes[n1-1].add(n2-1)

def f(n, p):
    cs[n] += p
    if not nodes[n]:
        return
    for _n in nodes[n]:
        f(_n, p)

while ps:
    n, p = ps.pop()
    f(n-1, p)

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
rs = [map(int, input().split()) for _ in range(N-1)]
ps = [map(int, input().split()) for _ in range(Q)]

cs = [0] * N
nodes = [set() for _ in range(N)]
for n1, n2 in rs:
    nodes[n1-1].add(n2-1)

def f(n, p):
    cs[n] += p
    if not nodes[n]:
        return
    for _n in nodes[n]:
        f(_n, p)

while ps:
    n, p = ps.pop()
    f(n-1, p)

print(cs)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
def walk(n):
    global adj, point
    if trav[n]==0:
        trav[n] = 1
    else:
        return
    if len(adj[n])==0:
        return
    else:
        for a in adj[n]:
            point[a] += point[n]
        for a in adj[n]:
            walk(a)
N, Q = map(int, input().split())
adj = [[] for _ in range(N)]
point = [0 for _ in range(N)]
trav = [0 for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    adj[a-1].append(b-1)
for _ in range(Q):
    p, x = map(int, input().split())
    point[p-1] += x
walk(0)
print(" ".join(map(str, point)))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
def walk(n):
    global adj, point
    if len(adj[n])==0:
        return
    else:
        for a in adj[n]:
            point[a] += point[n]
        for a in adj[n]:
            walk(a)
N, Q = map(int, input().split())
adj = [[] for _ in range(N)]
point = [0 for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    adj[a-1].append(b-1)
for _ in range(Q):
    p, x = map(int, input().split())
    point[p-1] += x
walk(0)
print(" ".join(map(str, point)))

Submission status: Wrong Answer

Solution code:
# def main():
#     N, Q = list(map(int, input().split()))
#     parents = [[] for i in range(N+1)]
#     ans = [0] * (N+1)

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

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

#     print(parents[1:])

#     for i in range(1, N+1):
#         for p in parents[i]:
#             ans[i] += ans[p]

#     print(*ans[1:])

import queue


def main():
    N, Q = list(map(int, input().split()))
    tree = [[] for i in range(N+1)]
    ans = [0] * (N+1)
    visit = [False]*(N+1)

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

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

    q = queue.Queue()
    q.put(1)
    visit[1] = True

    while (not q.empty()):
        n = q.get()
        print(tree[n])
        for i in tree[n]:
            if visit[i] is False:
                ans[i] += ans[n]
                # print(i, '+=', n)
                visit[i] = True
                q.put(i)

    print(*ans[1:])


if __name__ == '__main__':
    main()