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:
# 問題：https://atcoder.jp/contests/abc138/tasks/abc138_d

class DirectedGraph():

    def __init__(self, n):
        self.adjacency_dict = {}
    
    def add_vertex(self, v):
        self.adjacency_dict[v] = []

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)

    def remove_edge(self, v1, v2):
        self.adjacency_dict[v1].remove(v2)

    def remove_vertex(self,v):
        while self.adjacency_dict[v] != []:
            adjacency_vertex = self.adjacency_dict[v][-1]
            self.remove_edge(v, adjacency_vertex)
        del adjacency_vertex[v]

    def get_vertexes(self):
        return self.adjacency_dict.keys()

    def get_edges(self, v):
        return self.adjacency_dict[v]

    def print_graph(self):
        print(self.adjacency_dict)

            

def main():
    n, q = map(int, input().strip().split())
    g = DirectedGraph(n)
    for i in range(n):
        g.add_vertex(i+1)
    for i in range(n-1):
        a, b = map(int, input().strip().split())
        g.add_edge(a, b)

    res = [0 for _ in range(n)]
    for i in range(q):
        p, x = map(int, input().strip().split())
        res[p-1] += x
    
    queue = [1]
    while len(queue) > 0:
        root = queue.pop(0)
        for node in g.get_edges(root):
            res[node-1] += res[root-1]
            queue.append(node)

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 問題：https://atcoder.jp/contests/abc138/tasks/abc138_d

class DirectedGraph():

    def __init__(self):
        self.adjacency_dict = {}
    
    def add_vertex(self, v):
        self.adjacency_dict[v] = []

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)

    def remove_edge(self, v1, v2):
        self.adjacency_dict[v1].remove(v2)

    def remove_vertex(self,v):
        while self.adjacency_dict[v] != []:
            adjacency_vertex = self.adjacency_dict[v][-1]
            self.remove_edge(v, adjacency_vertex)
        del adjacency_vertex[v]

    def get_vertexes(self):
        return self.adjacency_dict.keys()

    def get_edges(self, v):
        return self.adjacency_dict[v]

    def print_graph(self):
        print(self.adjacency_dict)


def main():
    n, q = map(int, input().strip().split())
    g = DirectedGraph()
    for i in range(n):
        g.add_vertex(i+1)
    for i in range(n-1):
        a, b = map(int, input().strip().split())
        g.add_edge(a, b)

    res = [0 for _ in range(n)]
    queue = []
    for i in range(q):
        p, x = map(int, input().strip().split())
        queue.append(p)
        while len(queue) > 0:
            root = queue.pop(0)
            res[root-1] += x
            for node in g.get_edges(root):
                queue.append(node)
    print(' '.join(map(str, res)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

graph = defaultdict(list)
for _ in range(n-1):
    p,c = list(map(int, input().split()))
    graph[p].append(c)

counter = defaultdict(int)
for _ in range(q):
    i,c = list(map(int, input().split()))
    counter[i] += c


ans = [0] * (n + 1)
def dfs(node, total_counter):
    total_counter += counter.get(node, 0)
    if node in graph:
        for child in graph[node]:
            dfs(child, total_counter)
    ans[node] = total_counter

dfs(1, 0)

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():

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

    q = deque()
    score = [0] * N
    seen = [0] * N
    for _ in range(Q):
        a, s = map(int, input().split())
        score[a-1] += s

    q.append(0)
    while q:
        tmp = q.pop()
        seen[tmp] += 1
        for v in G[tmp]:
            if seen[v]:
                continue
            q.append(v)
            score[v] += score[tmp]

    print(*score)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():

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

    q = deque()
    score = [0] * N
    for _ in range(Q):
        a, s = map(int, input().split())
        score[a-1] += s

    q.append(0)
    while q:
        tmp = q.pop()
        for v in G[tmp]:
            q.append(v)
            score[v] += score[tmp]

    print(*score)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():

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

    q = deque()
    score = [0] * N
    for _ in range(Q):
        a, s = map(int, input().split())
        q.append(a-1)
        while q:
            tmp = q.pop()
            score[tmp] += s
            for v in G[tmp]:
                q.append(v)

    print(*score)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():

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

    q = deque()
    score = [0] * N
    for _ in range(Q):
        a, s = map(int, input().split())
        q.append(a-1)
        seen = set()
        while q:
            tmp = q.popleft()
            score[tmp] += s
            seen.add(tmp)
            for v in G[tmp]:
                if v not in seen:
                    q.append(v)

    print(*score)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
 
N, Q = map(int, input().split())
root = [[] for i in range(N)]
ans = [0]*N
 
for i in range(N-1):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  root[a].append(b)
  root[b].append(a)

print(root)
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)

dfs(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2*10**5+10)

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

def addtree(p):
    x=A[p]
    tmp=set(a for a in S if a[0]==p)
    for s in tmp:
        A[s[1]]+=x
        addtree(s[1])
    return

addtree(0)
print(*A)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(N-1)]
P = [0] * N
for _ in range(Q):
    k, n = map(int, input().split())
    P[k-1] += n
ans = [0] * N
def make(num, A, p):
    ans[num-1] += p
    for (i, k) in A:
        if i == num:
            make(k, A, p)
    return
for i in range(0, N+1):
    if P[i-1] == 0:
        continue
    else:
        make(i, A, P[i-1])
print(*ans)

Submission status: Wrong Answer

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

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

#lis=[list(map(int,input().split())) for i in range(Q)]    

ans=[0]*N


for i in range(Q):
    node,cost=map(int,input().split())
    d=[node-1]
    vist=[0]*N
    while d:
        a=d.pop()
        if vist[a]==0:
            vist[a]=1
            ans[a]+=cost
            for i in arr[a]:
                d.append(i)
        

print(*ans)

Submission status: Wrong Answer

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

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

#lis=[list(map(int,input().split())) for i in range(Q)]    

ans=[0]*N


for i in range(Q):
    node,cost=map(int,input().split())
    d=[node-1]
    while d:
        a=d.pop()
        ans[a]+=cost
        for i in arr[a]:
            d.append(i)
        

print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(N-1)]
P = [tuple(map(int, input().split())) for _ in range(Q)]
def solve(A, num):
    ans = []
    if True in [num==i[0] for i in A] :
        for i in A:
            if num == i[0]:
                ans.append(i[1])
                k = solve(A, i[1])
                if k:
                    ans +=k
        return ans
    else:
        return False
ans = [0] * N
for i in P:
    ans[i[0]-1] += i[1]
    l = solve(A, i[0])
    if l:
        for k in l:
            ans[k-1] += i[1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(N-1)]
P = [tuple(map(int, input().split())) for _ in range(Q)]
def solve(A, num):
    ans = []
    if True in [num==i[0] for i in A] :
        for i in A:
            if num == i[0]:
                ans.append(i[1])
                k = solve(A, i[1])
                if k:
                    ans +=k
        return ans
    else:
        return False
ans = [0] * N
for i in P:
    print(i)
    ans[i[0]-1] += i[1]
    l = solve(A, i[0])
    if l:
        for k in l:
            ans[k-1] += i[1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
A,B=[],[]
PX=[0]*N
for _ in range(N-1):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
for _ in range(Q):
    p,x=map(int,input().split())
    PX[p-1]+=x

adj={}
for i in range(1,N+1):
    l=[]
    for j in range(N-1):
        if A[j]==i:
            l.append(B[j])
    adj[i]=l

counter=[0]*(N)

def dfs(adj,p,x):
    stack=[p]
    visited=set()
    while stack:
        v=stack.pop()
        if v in visited:
            continue
        visited.add(v)
        counter[v-1]+=x
        for u in adj[v]:
            if u not in visited:
                stack.append(u)
    return

for i in range(1,N+1):
    dfs(adj,i,PX[i-1])

print(*counter)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(400000)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)] # paths[i] : 頂点iと繋がってる点を全て表す
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

ans = [0]*(N+1) # ans[i] : 頂点iについて答える値を格納

counters = [0]*(N+1) # そのノードの部分木に与える点数の総和を格納
for x in px:
    counters[x[0]] += x[1]

# visit = [0]*(N+1) #dfsをする時に一度訪れた点に再訪しないための確認
def dfs(node, score, ans):
    # visit[node] = 1 # 訪問したことを記録
    score += counters[node] # 自分の部分木に加点する分を加えて後世に残す
    ans[node] = score # 点数の記録
    for loof in paths[node]: # 自分と繋がってる各ノードについて
        # if visit[loof] == 0: # まだ探索していなければ
            dfs(loof, score, ans) # 探索

dfs(1, 0, ans)
    

# for i in range(1, N+1):
#     print(ans[i])
# print('\n'.join(map(str, ans[1:])))

# for i in range(1, N+1):
#     print(ans[i], end = " ")
# print()
print(" ".join(map(str, ans[1:])))

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
A,B,P,X=[],[],[],[]
for _ in range(N-1):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
for _ in range(Q):
    p,x=map(int,input().split())
    P.append(p)
    X.append(x)

adj={}
for i in range(1,N+1):
    l=[]
    for j in range(N-1):
        if A[j]==i:
            l.append(B[j])
    adj[i]=l

counter=[0]*(N)

def dfs(adj,p,x):
    stack=[p]
    visited=set()
    while stack:
        v=stack.pop()
        if v in visited:
            continue
        visited.add(v)
        counter[v-1]+=x
        for u in adj[v]:
            if u not in visited:
                stack.append(u)
    return

for i in range(Q):
    dfs(adj,P[i],X[i])

print(*counter)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
d = [0]*(n+1)
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
for _ in range(q):
    p,x = map(int,input().split())
    d[p] += x
ans = [0]*(n+1)
seen = [False]*(n+1)
from collections import deque
q = deque([(1,d[1])])
while q:
    s,cost = q.popleft()
    seen[s] = True
    ans[s] += cost
    for e in g[s]:
        if not seen[e]:
            q.append((e,cost+d[e]))
# def helper(s,cost):
#     seen[s] = True
#     ans[s] += cost
#     for e in g[s]:
#         if not seen[e]:
#             helper(e,cost+d[e])
# for i in range(1,n+1):
#     if not seen[i]:
#         helper(i,d[i])
res = []
for a in ans[1:]:
    res.append(str(a))

print(' '.join(res))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Q=[0]



while Q:
  v=Q.pop(0)
  cnt=ans[v]
  for i in lst[v]:
    ans[i]+=cnt
    Q.append(i)

ans=[str(i) for i in ans]

print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
#ABC168D
n,q=map(int,input().split())
lst=[[] for i in range(n)]
ans=[0]*n

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

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

Q=[0]



while Q:
  v=Q.pop(0)
  cnt=ans[v]
  for i in lst[v]:
    ans[i]+=cnt
    Q.append(i)

ans=[str(i) for i in ans]

print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
# n=int(input())
import sys
sys.setrecursionlimit(10**7)
n, q = map(int, input().split())
# vl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
adjl = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int, input().split())
    adjl[a].append(b)
c = [0 for _ in range(n+1)]
point = [0 for _ in range(n+1)]


def dfs(u, add):
    global point
    point[u] = c[u]+add
    for v in adjl[u]:
        dfs(v, point[u])


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


dfs(1, 0)
point.remove(0)
print(*point, sep=" ")

Submission status: Wrong Answer

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

l=[]

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

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

for v in l:
    point[v[1]-1]+=point[v[0]-1]
    
print(" ".join(str(n) for n in point))

Submission status: Wrong Answer

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

l=[]

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

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


for v in l:
    point[v[0]-1]+=point[v[1]-1]
    
print(" ".join(str(n) for n in point))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
dic = defaultdict(list)
N, Q = map(int, input().split())
point = [0 for _ in range(N)]
seen = [-1 for _ in range(N)]

for i in range(N-1):
    a, b = map(int, input().split())
    dic[a].append(b)
    dic[b].append(a)
    
for _ in range(Q):
    p, x = map(int, input().split())
    point[p-1]+=x


queue = deque(dic[1])

while queue:
    p = queue.popleft()
    point[p-1] += point[dic[p][0]-1]
    for i in range(1, len(dic[p])):
        queue.append(dic[p][i])
print(" ".join(str(n) for n in point))

Submission status: Wrong Answer

Solution code:
import copy
import math
import time
import statistics
import math
import itertools
import bisect
from decimal import *

# a = get_int()
def get_int():
    return int(input())
# a = get_string()
def get_string():
    return input()
# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]
# a_list = get_string_list():
def get_string_list():
    return input().split()
# a, b = get_int_multi()
def get_int_multi():
    return map(int, input().split())
# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))
# print("{} {}".format(a, b))
# for num in range(0, a):
# a_list[idx]
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''

def main():

    start = time.time()

    n, q = get_int_multi()

    a = []
    a.append([-1,1,0])
    for _ in range(n-1):
        wk = get_int_list()
        wk.append(0)
        a.append(wk)

    b = []
    for _ in range(q):
        p, x = get_int_multi()
        tasu(a, p, x)

    a.sort(key=lambda x: x[1])

    ans = ""
    for itm in a:
        ans = ans + str(itm[2]) + " "

    print(ans[:-1])

def tasu(a, p, x):
    for itm in a:
        if itm[1] == p:
            itm[2] += x
            break
    for itm in a:
        if itm[0] == p:
            tasu(a, itm[1], x)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

graph = [[] for i in range(N)]

score = [0 for _ in range(N)]

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

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

    visited = [False for _ in range(N)]
    queue = deque()
    queue.append(p-1)
    score[p-1] += x

    while queue:
        now = queue.popleft()

        for adj in graph[now]:
            if visited[adj] or (now > adj):
                continue
            score[adj] += x
            visited[adj] = True
            queue.append(adj)

for i in range(N):
    print(score[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)

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

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

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

def dfs(now , prev = -1):
    for next in g[now]:
        # 次のノードが前に参照した値の時はcontinue
        if next == prev:
            continue
        # 現在の値を次のポイントに加算することで累積和をとる
        cnt[next] += cnt[now]
        # 次のノードと現在のノードを引数にdfsを継続する
        dfs(next , now)

dfs(0)
print(*cnt)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)

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

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

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

def dfs(v):
    for i in g[v]:
        cnt[i] += cnt[v]
        dfs(i)

dfs(0)
print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
N, Q = map(int, input().split())
d = defaultdict(list)
count = defaultdict(int)
for i in range(N):
    d[i + 1] = []
    count[i + 1] = 0

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

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

# print(d)
# print(L)


for v in L:
    idx = deque()
    idx.append(v[0])
    while len(idx) > 0:
        q = idx.popleft()
        count[q] += v[1]
        if len(d[q]) != 0:
            for k in d[q]:
                if len(d[k]) != 0:
                    idx.append(k)
                else:
                    count[k] += v[1]

ans = []
for i in range(N):
    ans.append(str(count[i + 1]))


print(' '.join(ans))

Submission status: Wrong Answer

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

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

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

ans = []
for i in range(N):
    v = 0
    p = i
    v += V[i]
    while p != -1:
        p = B[p]
        v += V[p]
    ans.append(str(v))

print(" ".join(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque
# from collections import Counter
# from itertools import accumulate
# from fractions import gcd # >=Python3.5 # lcm（最小公倍数） = (a*b)//gcd(a,b)
# from fractions import Fraction
# from math import gcd # <Python3.5
# from math import sqrt

# input = sys.stdin.readline
# def main():

#---------------------------------------------------------

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

road = [[] for _ in range(n)]
cnt  = [0 for _ in range(n)]

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

for i in range(q):
    idx,v = map(int,input().split())
    cnt[idx-1] = v

dist = [-1]*n
que  = deque()
dist[0] = 0
que.append(0)

while(len(que)!=0):
    # BFS/DFS
    v = que.pop()
    for r in road[v]:
        if dist[r]!=-1:
            continue
        dist[r] = dist[v] + 1
        cnt[r] += cnt[v]
        que.append(r)

print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy

# グラフの作成(無向グラフでは#を消す)
n, q = map(int, input().split())
graph = [deque([]) for _ in range(n+1)]
for _ in range(n-1):
    a, b  = [int(x) for x in input().split()]
    graph[a].append(b)
#print(graph)
#q_list = [list(map(int, input().split())) for _ in range(q)]
q_sum_list = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    q_sum_list[p-1] += x
#print(q_sum_list)
#arrive_rireki = [-1] * (N + 1)

ans = [0]*n
score = [0]*n

# 深さ優先探索
def dfs(v):
    arrive_rireki = [-1] * (n + 1)
    #print("check", graph_temp, arrive_rireki) #deepcopy の確認
    stack = [v]
    arrive_rireki[v] = 1
    while stack:
        #print("stack", stack)
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if arrive_rireki[w] < 0:
                arrive_rireki[w] = 1
                stack.append(w)
        else:
            stack.pop()
            #print("stack pop", v, stack)
            score[v-1] += q_sum_list[v-1]
            for i in range(len(stack)):
                score[v-1] += q_sum_list[i]
    return score

dfs(1)
#print(eda_list)
print(* score)

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy

# グラフの作成(無向グラフでは#を消す)
n, q = map(int, input().split())
graph = [deque([]) for _ in range(n+1)]
for _ in range(n-1):
    a, b  = [int(x) for x in input().split()]
    graph[a].append(b)
#print(graph)
q_list = [list(map(int, input().split())) for _ in range(q)]

#arrive_rireki = [-1] * (N + 1)

ans = [0]*n
score = [0]*n
eda_list = [[0 for _ in range(n)] for i in range(n)]
eda_list[0][0] = 1

# 深さ優先探索
def dfs(v):
    arrive_rireki = [-1] * (n + 1)
    #print("check", graph_temp, arrive_rireki) #deepcopy の確認
    stack = [v]
    arrive_rireki[v] = 1
    while stack:
        #print("stack", stack)
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if arrive_rireki[w] < 0:
                arrive_rireki[w] = 1
                stack.append(w)
                for i in range(len(stack)):
                    eda_list[stack[i]-1][w-1] = 1
        else:
            stack.pop()
            #print("stack pop", v, stack)
    return score

dfs(1)
#print(eda_list)
for i in range(q):
    for j in range(n):
        score[j] += eda_list[q_list[i][0]-1][j]*q_list[i][1]
print(* score)

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy

# グラフの作成(無向グラフでは#を消す)
n, q = map(int, input().split())
graph = [deque([]) for _ in range(n+1)]
for _ in range(n-1):
    a, b  = [int(x) for x in input().split()]
    graph[a].append(b)
#print(graph)
q_list = [list(map(int, input().split())) for _ in range(q)]

#arrive_rireki = [-1] * (N + 1)

ans = [0]*n
score = [0]*n

# 深さ優先探索
def dfs(v):
    graph_temp = copy.deepcopy(graph)
    arrive_rireki = [-1] * (n + 1)
    #print("check", graph_temp, arrive_rireki) #deepcopy の確認
    stack = [v]
    arrive_rireki[v] = 1
    score[v-1] += q_list[i][1]
    while stack:
        #print("stack", stack)
        v = stack[-1]
        if graph_temp[v]:
            w = graph_temp[v].popleft()
            if arrive_rireki[w] < 0:
                arrive_rireki[w] = 1
                score[w-1] += q_list[i][1]
                stack.append(w)
        else:
            stack.pop()
    return score

for i in range(0,q):
    dfs(q_list[i][0])
print(* score)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self):
        self.N, self.Q = map(int, input().split())
        self.list = [0 for i in range(self.N+1)]
        self.under = [0 for i in range(self.N+1)]
        self.count = [0 for i in range(self.N+1)]
        for i in range(self.N-1):
            a, b = map(int, input().split())
            self.list[b] = a
        self.__count__()
        for i in range(1, self.N+1):
            self.__count_under__(i)
        self.__print__()
    def __count__(self):
        for i in range(self.Q):
            p, x = map(int, input().split())
            self.count[p] += x
    def __count_under__(self, n):
        if n == 1:
            self.under[1] = self.count[1]
        else:
            self.under[n] = self.count[n] + self.under[self.list[n]]
    def __print__(self):
        for i in range(1, self.N+1):
            print(self.under[i])
g = Node()

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
w = [N]*N
for _ in range(N-1):
    A, B = map(int, input().split())
    w[B-1] = A-1

cnt = [0]*N
for _ in range(Q):
    P, X = map(int, input().split())
    cnt[P-1] += X

cnt_sum = [0]*(N+1)

for i in range(N):
    p = w[i]
    cnt_sum[i] = cnt_sum[p]+cnt[i]

print(" ".join([str(c) for c in cnt_sum[:-1]]))

Submission status: Wrong Answer

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


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    n, q = MI()
    connected = [-1] * n
    for i in range(n - 1):
        a, b = MI()
        connected[b - 1] = a - 1

    points = [0] * n

    for j in range(q):
        p, x = MI()
        points[p - 1] += x

    ans = [0] * n
    ans[0] = points[0]

    for i in range(1, n):
        parent = connected[i]
        ans[i] = ans[parent] + points[i]

    print(*ans, sep=" ")


main()

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ab=[[_] for _ in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    ab[max(a,b)-1].append(min(a,b))
for i in range(n):
    if len(ab[i])==1:
        num=i
        ab[i].append(-1)
if i==0:
    ans=[0]*n
    for i in range(q):
        p,x=map(int,input().split())
        ans[p-1]+=x
    ab.sort(key=lambda x: x[1])
    for i in range(1,n):
        for j in range(1,len(ab[i])):
            ans[ab[i][0]]+=ans[ab[i][j]-1]
    print(*ans)
else:
    ans=[0]*n
    for i in range(q):
        p,x=map(int,input().split())
        ans[p-1]+=x
    yn=[0]*n
    yn[0]=1
    n1=1
    queue=[]
    queue2=[]
    for i in range(n+10):
        for j in range(len(ab[n1-1])):
            if yn[ab[n1-1][j]-1]==0:
                yn[ab[n1-1][j]-1]=1
                queue.append(ab[n1-1][j])
        for j in range(len(queue)):
            ans[queue[0]-1]+=ans[n1-1]
            queue2.append(queue[0])
            del(queue[0])
        if len(queue)==0 and len(queue2)==0:
            break
        n1=queue2.pop(-1)
    print(*ans)

Submission status: Wrong Answer

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