Problem:


Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20



Sample input:
4 3
1 2
2 3
2 4
2 10
1 100
3 1


Sample output:
100 110 111 110

Submission status: Wrong Answer

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

eda=[set() for i in range(N+1)]
pt= [0 for i in range(N+1)]
for a,b in AB:
    eda[a].add(b)
for p,x in PX:
    pt[p]+=x

for i,lst in enumerate(eda):
    for e in lst:
        pt[e]+=pt[i]
print(" ".join(list(map(str,pt[1:]))))

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
AB=sorted([list(map(int,input().split())) for i in range(N-1)],key=lambda x:x[0])
PX=sorted([list(map(int,input().split())) for i in range(Q)],key=lambda x:x[0])

eda=[set() for i in range(N+1)]
pt= [0 for i in range(N+1)]
for a,b in AB:
    eda[a].add(b)
for p,x in PX:
    pt[p]+=x

for i,lst in enumerate(eda):
    for e in lst:
        pt[e]+=pt[i]
print(" ".join(list(map(str,pt[1:]))))

Submission status: Wrong Answer

Solution code:
import collections

def add(queue, ans, start_node, value):
    ans[start_node] += value
    for val in tree[start_node]:
        queue.append(val)
    while queue:
        node = queue.popleft()
        ans[node] += value
        if not node:
            continue

        if tree[node] != []:
            for val in tree[node]:
                queue.append(val)


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

tree = [[] for _ in range(n)]
ans = [0 for _ in range(n)]

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

for _ in range(q):
    p, x = map(int, input().split())
    queue = collections.deque()

    add(queue, ans, p-1, x)

print(*ans)

Submission status: Wrong Answer

Solution code:
# 木全体の根が決まっているのでふつうにbfsでいい気がするが...
# 毎回探索するんじゃなくて、各枝にポイントをつけておいて、探索するときに1回だけ渡して更新していく
# 考え方は累積和

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

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

path = [0]
while len(path) > 0:
    cur_node = path.pop()
    cur_point = points[cur_node]
    for node in graph[cur_node]:
        points[node] += cur_point
        path.append(node)
print(" ".join(list(map(str, points))))

Submission status: Wrong Answer

Solution code:
n, q = (int(x) for x in input().split())
ab = []
px = []
ans = [0 for _ in range(n)]
d = {}
for _ in range(n-1):
    ab.append([int(x) for x in input().split()])
for _ in range(q):
    px.append([int(x) for x in input().split()])
for i in ab:
    key = str(i[1])
    if key not in d.keys():
        d[key] = []
    d[key].append(i[0])
for i in px:
    ans[i[0] - 1] += i[1]
for i in range(2, n+1):
    key = str(i)
    if key in d.keys():
        for j in d[key]:
            ans[i - 1] += ans[j - 1]
for ans_i in ans: print(ans_i)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

cnt=[0 for i in range(n)]
cnt[0]=t_cnt[0]
dq=deque()
dq.append(0)
while dq:
 x=dq.popleft()
 for i in tree[x]:
  if cnt[i]==0:
   dq.append(i)
   cnt[i]=cnt[x]+t_cnt[i]
print(*cnt)

Submission status: Wrong Answer

Solution code:
n, q = (int(x) for x in input().split())
ab = []
px = []
ans = [0 for _ in range(n)]
d = {}
for _ in range(n-1):
    ab.append([int(x) for x in input().split()])
for _ in range(q):
    px.append([int(x) for x in input().split()])
for i in ab:
    key = str(i[0])
    if key not in d.keys():
        d[key] = []
    d[key].append(i[1])
for i in px:
    l = [i[0]]
    x = i[1]
    while len(l) != 0:
        if str(l[0]) in d.keys():
            for j in d[str(l[0])]:
                l.append(j)
        ans[l[0] - 1] += x
        l.pop(0)
for ans_i in ans: print(ans_i)

Submission status: Wrong Answer

Solution code:
n, q = (int(x) for x in input().split())
ab = []
px = []
ans = [0 for _ in range(n)]
d = {}
for _ in range(n-1):
    ab.append([int(x) for x in input().split()])
for _ in range(q):
    px.append([int(x) for x in input().split()])
for i in ab:
    key = str(i[0])
    if key not in d.keys():
        d[key] = []
    d[key].append(i[1])
for i in px:
    l = [i[0]]
    x = i[1]
    while len(l) != 0:
        if str(l[0]) in d.keys():
            for i in d[str(l[0])]:
                l.append(i)
        ans[l[0] - 1] += x
        l.pop(0)
for ans_i in ans: print(ans_i)

Submission status: Wrong Answer

Solution code:
n, q = (int(x) for x in input().split())
ab = []
px = []
ans = [0 for _ in range(n)]
d = {}
for _ in range(n-1):
    ab.append([int(x) for x in input().split()])
for _ in range(q):
    px.append([int(x) for x in input().split()])
for i in ab:
    key = str(i[0])
    if key not in d.keys():
        d[key] = []
    d[key].append(i[1])
for i in px:
    l = [i[0]]
    x = i[1]
    while len(l) != 0:
        if str(l[0]) in d.keys():
            for i in d[str(l[0])]:
                l.append(i)
        ans[l[0] - 1] += x
        l.pop(0)
print(ans)

Submission status: Wrong Answer

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

P=[[] for i in range(N)]
ans=[0]*N


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

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

for i in range(1,N):
    for j in P[i]:
        ans[i]+=ans[j]

print(*ans)

Submission status: Wrong Answer

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

P=[[i] for i in range(N)]
ans=[0]*N


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

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

for i in range(1,N):
    for j in P[i]:
        ans[i]+=ans[j]

print(*ans)

Submission status: Wrong Answer

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

A=[[i] for i in range(N)]
ans=[0]*N


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

for i in range(N):
    for j in A[i]:
        A[i].extend(A[j])
        A[i]=list(set(A[i]))

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

print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

cnt=[0 for i in range(n)]
cnt[0]=t_cnt[0]
dq=deque()
dq.append(0)
while dq:
 x=dq.popleft()
 for i in tree[x]:
  if cnt[i]==0:
   if i==x:
    continue
   dq.append(i)
   cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

cnt=[0 for i in range(n)]
cnt[0]=t_cnt[0]
dq=deque()
dq.append(0)
while dq:
 x=dq.popleft()
 for i in tree[x]:
  if cnt[i]==0:
   dq.append(i)
   cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

cnt=[0 for i in range(n)]
cnt[0]=t_cnt[0]
dq=deque()
dq.append(0)
while dq:
 x=dq.popleft()
 for i in tree[x]:
  if cnt[i]==0:
   dq.append(i)
   cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

cnt=[0 for i in range(n)]
cnt[0]=t_cnt[0]
dq=deque()
dq.append(0)
while dq:
 x=dq.popleft()
 for i in tree[x]:
  if cnt[i]==-1:
   dq.append(i)
   cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n-1)]
px=[list(map(int,input().split())) for i in range(q)]

tree=[[] for i in range(n)]
for i,j in l:
 tree[i-1].append(j-1)

t_cnt=[0 for i in range(n)]
for p,x in px:
 t_cnt[p-1]+=x

cnt=[-1 for i in range(n)]
cnt[0]=t_cnt[0]
dq=deque()
dq.append(0)
while dq:
 x=dq.popleft()
 for i in tree[x]:
  if cnt[i]==-1:
   dq.append(i)
   cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Wrong Answer

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

n,q=map(int,input().split())
v_counter=[0 for _ in range(n+1)]
#v_parent=[0 for _ in range(n+1)]
v_child=[[] for _ in range(n+1)]

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

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

que=deque()
que.append(1)
while(len(que)>0):
    ind=que.popleft()
    child=v_child[ind]
    for j in child:
        v_counter[j]+=v_counter[ind]
    que.extend(child)

print(*v_counter[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

cnt=[-1 for i in range(n)]
cnt[0]=t_cnt[0]
dq=deque()
dq.append(0)
while dq:
 x=dq.popleft()
 for i in tree[x]:
  if cnt[i]==-1:
   dq.append(i)
   cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Wrong Answer

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

n,q=map(int,input().split())
v_counter=[0 for _ in range(n+1)]
#v_parent=[0 for _ in range(n+1)]
v_child=[[] for _ in range(n+1)]

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

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

que=deque()
que.append(1)
while(len(que)>0):
    ind=que.popleft()
    child=v_child[ind]
    for j in child:
        v_counter[j]+=v_counter[ind]
    que.extend(child)

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

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n-1)]
px=[list(map(int,input().split())) for i in range(q)]

tree=[[] for i in range(n)]
for i,j in l:
 tree[i-1].append(j-1)

t_cnt=[0 for i in range(n)]
for p,x in px:
 t_cnt[p-1]+=x

seen=[]
cnt=[0 for i in range(n)]
cnt[0]=t_cnt[0]
dq=deque()
dq.append(0)
while dq:
 x=dq.popleft()
 for i in tree[x]:
  if i not in seen:
   seen.append(i)
   dq.append(i)
   cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Wrong Answer

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

n,q=map(int,input().split())
v_counter=[0 for _ in range(n+1)]
#v_parent=[0 for _ in range(n+1)]
v_child=[[] for _ in range(n+1)]

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

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

for i in range(1,n+1):
#    v_counter[i]+=v_counter[v_parent[i]]
    for j in v_child[i]:
        v_counter[j]+=v_counter[i]

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

Submission status: Wrong Answer

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

n,q=map(int,input().split())
v_counter=[0 for _ in range(n+1)]
v_parent=[0 for _ in range(n+1)]

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

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

for i in range(1,n+1):
    v_counter[i]+=v_counter[v_parent[i]]

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

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
COUNTS = {i:0 for i in range(1, n + 1)}
SUBTREES = {i:[] for i in range(1, n + 1)}

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

i = n
for _ in range(n):
    VERTICES = [j for j in SUBTREES[i]]
    for j in VERTICES:
        SUBTREES[i].extend(SUBTREES[j])
    i -= 1

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

COUNTS = sorted(COUNTS.items())
for count in COUNTS:
    answer = count[1]
    print(answer, end = " ")

Submission status: Wrong Answer

Solution code:
n,q = (int(x) for x in input().split())
a,b,p,x = [0]*(n-1), [0]*(n-1), [0]*q, [0]*q

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

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

ans = [0] * n

for i in range(q):
    ans[p[i]-1] += x[i]

for i in range(n-1):
    ans[b[i]-1] += ans[a[i]-1]

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

print()

Submission status: Wrong Answer

Solution code:
n,q = (int(x) for x in input().split())
a,b,p,x = [0]*(n-1), [0]*(n-1), [0]*q, [0]*q

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

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

ans = [0] * n

for i in range(q):
    ans[p[i]-1] += x[i]

for i in range(n-1):
    ans[b[i]-1] += ans[a[i]-1]

for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
n,q=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]
px=[list(map(int,input().split())) for i in range(q)]
# print(ab)
# print(px)
ab.sort()
cnt=[0]*(n+1)
for i in range(q):
    cnt[px[i][0]]+=px[i][1]
# print(cnt)
for i in range(n-1):
    cnt[ab[i][1]]+=cnt[ab[i][0]]
for i in range(1,n+1):
    print(cnt[i])

Submission status: Wrong Answer

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

def main():
    N, Q = map(int, input().split())

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

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

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

    visited = [False] * N

    def dfs(v):
        visited[v] = True
        childs = G[v]
        if not childs:
            return
        
        val = c[v]
        for n in childs:
            c[n] += val
            dfs(n)

    dfs(0)

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

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

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

visited = [False] * N

def dfs(v):
    visited[v] = True
    childs = G[v]
    if not childs:
        return
    
    val = c[v]
    for n in childs:
        c[n] += val
        dfs(n)

dfs(0)

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

Submission status: Wrong Answer

Solution code:
import copy

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,Q = map(int,readline().split())
ki = [list(map(int,readline().split())) for i in range(N-1)]
co = [list(map(int,readline().split())) for i in range(Q)]
 
ki.sort()
#print(ki)
 
ver = [0 for i in range(N)]
 
for i in range(Q):
    ver[co[i][0]-1] += co[i][1]
 
ver2 = copy.copy(ver)

#print(ver)
#print(ver2)
 
for i in range(N-1):
    if ver[ki[i][1]-1] != ver2[ki[i][1]-1]:
        ver[ki[i][0]-1] += ver[ki[i][1]-1]
    else:
        ver[ki[i][1]-1] += ver[ki[i][0]-1]

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

Submission status: Wrong Answer

Solution code:
import copy

N,Q = map(int,input().split())
ki = [list(map(int,input().split())) for i in range(N-1)]
co = [list(map(int,input().split())) for i in range(Q)]
 
ki.sort()
 
ver = [0 for i in range(N)]
 
for i in range(Q):
    ver[co[i][0]-1] += co[i][1]
 
ver2 = copy.copy(ver)
 
for i in range(N-1):
    if ver[ki[i][1]-1] != ver2[ki[i][1]-1]:
        ver[ki[i][0]-1] += ver[ki[i][1]-1]
    else:
        ver[ki[i][1]-1] += ver[ki[i][0]-1]

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

Submission status: Wrong Answer

Solution code:
import copy

N,Q = map(int,input().split())
ki = [list(map(int,input().split())) for i in range(N-1)]
co = [list(map(int,input().split())) for i in range(Q)]
 
ki.sort()
#print(ki)
 
ver = [0 for i in range(N)]
 
for i in range(Q):
    ver[co[i][0]-1] += co[i][1]
 
ver2 = copy.copy(ver)

#print(ver)
#print(ver2)
 
for i in range(N-1):
    if ver[ki[i][0]-1] == ver2[ki[i][0]-1] and ver[ki[i][1]-1] != ver2[ki[i][1]-1]:
        ver[ki[i][0]-1] += ver[ki[i][1]-1]
    else:
        ver[ki[i][1]-1] += ver[ki[i][0]-1]

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

Submission status: Wrong Answer

Solution code:
import copy

N,Q = map(int,input().split())
ki = [list(map(int,input().split())) for i in range(N-1)]
co = [list(map(int,input().split())) for i in range(Q)]
 
#print(ki)
 
ver = [0 for i in range(N)]
 
for i in range(Q):
    ver[co[i][0]-1] += co[i][1]
 
ver2 = copy.copy(ver)

#print(ver)
#print(ver2)
 
for i in range(N-1):
    if ver[ki[i][0]-1] == ver2[ki[i][0]-1] and ver[ki[i][1]-1] != ver2[ki[i][1]-1]:
        ver[ki[i][0]-1] += ver[ki[i][1]-1]
    else:
        ver[ki[i][1]-1] += ver[ki[i][0]-1]

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

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ki = [list(map(int,input().split())) for i in range(N-1)]
co = [list(map(int,input().split())) for i in range(Q)]

ki.sort(key=lambda x:x[1])
#print(ki)

ver = [[0,False,False] for i in range(N)]
ver[0][1] = True

for c in co:
    ver[c[0]-1][0] += c[1]
#print(ver)

x = 1
while x != 0:
    x = 0
    for k in ki:
        if ver[k[0]-1][1] == True and ver[k[1]-1][2] == False:
            ver[k[1]-1][0] += ver[k[0]-1][0]
            ver[k[1]-1][1] = True
            ver[k[1]-1][2] = True
            x = 1

ans = [ver[i][0] for i in range(N)]

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

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ki = [list(map(int,input().split())) for i in range(N-1)]
co = [list(map(int,input().split())) for i in range(Q)]

ki.sort(key=lambda x:x[1])
#print(ki)

ver = [0 for i in range(N)]

for i in range(Q):
    ver[co[i][0]-1] += co[i][1]

#print(ver)

for i in range(N-1):
    ver[ki[i][1]-1] += ver[ki[i][0]-1]

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

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ki = [list(map(int,input().split())) for i in range(N-1)]
co = [list(map(int,input().split())) for i in range(Q)]

ki.sort()
#print(ki)

ver = [0 for i in range(N)]

for i in range(Q):
    ver[co[i][0]-1] += co[i][1]

#print(ver)

for i in range(N-1):
    ver[ki[i][1]-1] += ver[ki[i][0]-1]

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

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ki = [list(map(int,input().split())) for i in range(N-1)]
co = [list(map(int,input().split())) for i in range(Q)]

ver = [0 for i in range(N)]

for i in range(Q):
    ver[co[i][0]-1] += co[i][1]

#print(ver)

for i in range(N-1):
    ver[ki[i][1]-1] += ver[ki[i][0]-1]

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,q=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(n-1):
        u,v=map(int,input().split())
        u-=1; v-=1
        E[u].append(v)
        E[v].append(u)

    # Euler tour
    Q=[(~0,-1),(0,-1)]
    begin=[None]*n
    end=[None]*n
    cnt=-1
    while(Q):
        v,p=Q.pop()
        cnt+=1
        if(v>=0):
            if(begin[v] is None): begin[v]=cnt
            for nv in E[v][::-1]:
                if(nv==p): continue
                Q.append((~nv,v))
                Q.append((nv,v))
        else:
            end[v]=cnt

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

    for i in range(2*n-1):
        imos[i+1]+=imos[i]

    # output
    for v in range(n):
        print(imos[begin[v]])
resolve()

Submission status: Wrong Answer

Solution code:
import collections

n,q=map(int,input().split())
a=sorted(list(list(map(int,input().split())) for i in range(n-1)))
p=collections.defaultdict(int)
for i in range(q):
  k,v=map(int,input().split())
  p[k]+=v

li=[0]*n
li[0]+=p[1]
for i in range(n-1):
  li[a[i][1]-1]+=li[a[i][0]-1]+p[a[i][1]]
print(*li)

Submission status: Wrong Answer

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

li=[]
ans=[0]*(n+1)

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

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

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

for a,b in li:
  ans[b]+=ans[a]

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
ki = [[]for _ in range(n)]
P = [0]*n
visited = [0] * n
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    
    ki[a].append(b)
    ki[b].append(a)
for i in range(q):
    p,x = map(int,input().split())
    p -= 1
    P[p] += x
stack = [0]
while stack:
    for now in stack:
        visited[now] = 1
        a = []
        for next in ki[now]:
            if visited[next]:continue
            P[next]+= P[now]
            a.append(next)
    stack = a
for i in range(n):
    print(P[i])

Submission status: Wrong Answer

Solution code:
n,q = (int(i) for i in input().split())

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

for _ in range(n-1):
    k,v = (int(i) for i in input().split())
    ab[k].append(v)

for _ in range(q):
    k,v = (int(i) for i in input().split())
    Query[k] += v

ans[1] = Query[1]
for k,v in ab.items():
    for i in v:
        ans[i] = ans[k] + Query[i]

for a in ans.values(): print(a,end=' ')
print()

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split(" "))
g = [[i] for i in range(n)]
counter = [0]*n
for i in range(n-1):
  s, t = map(int, input().split(" "))
  g[s-1].append(t-1)
  g[t-1].append(s-1)
for j in range(q):
  root, plus = map(int, input().split(" "))
  if root == 1:
    for k in range(n):
      counter[k]+=plus
  else:
    for k in range(n):
      if root-1 in g[k]:
        counter[k]+=plus
print(*counter)

Submission status: Wrong Answer

Solution code:
import sys
inputs = sys.stdin.readlines()

n, q = map(int, inputs[0].split())
edges = [tuple(map(int, inputs[1+i].split())) for i in range(n-1)]
weights = [tuple(map(int, inputs[n+i].split())) for i in range(q)]

tree = [[] for _ in range(n+1)]
for x, y in edges:
    tree[x].append(y)

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

stack = [1]
while stack:
    x = stack.pop()
    for y in tree[x]:
        stack.append(y)
        val[y] += val[x]

print(*val[1:])

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
inputs = sys.stdin.readlines()

n, q = map(int, inputs[0].split())
edges = [tuple(map(int, inputs[1+i].split())) for i in range(n-1)]
weights = [tuple(map(int, inputs[n+i].split())) for i in range(q)]

get_decendant_nodes = {i:tuple(y for x, y in edges if x == i) for i in range(1, n+1)}

ans = defaultdict(int)
for node, w in weights:
    ans[node] += w

stack = [1]
while stack:
    x = stack.pop()
    for y in get_decendant_nodes[x]:
        stack.append(y)
        ans[y] += ans[x]

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
inputs = sys.stdin.readlines()

class Tree():
    def __init__(self, edges=None):
        self.nodes = set()
        self.edges = dict()
        if edges is not None:
            for e in edges:
                self.add_edge(e)
        
    def __iter__(self):
        return self.edges.__iter__()
    
    def __str__(self):
        return self.edges.__str__()
    
    def __repr__(self):
        return self.edges.__repr__()
    
    def add_node(self, x):
        self.nodes.add(x)
    
    def add_edge(self, x, cost=0):
        assert len(x) == 2, '{} is not appropriate edge!'.format(x)
        a, b = (x[0], x[1]) if x[0] < x[1] else (x[1], x[0])
        if a not in self.nodes:
            self.nodes.add(a)
        if b not in self.nodes:
            self.nodes.add(b)
        self.edges[(a, b)] = cost
    
    def degree(self, n):
        assert n in self.nodes, '{} is not appropriate node!'.format(n)
        return sum([(n in edge) for edge in self.edges])
    
    def get_edges(self, n):
        return [edge for edge in self.edges if n in edge]
    
    def get_nearby_nodes(self, n):
        return [e[1] if e[0] == n else e[0] for e in self.edges if n in e]
    
    def dps(self, root):
        nodes, stack, is_cycle = [], [root]
        while stack:
            current_node = stack.pop()
            assert current_node not in nodes, 'Tree has a cycle!'
                
            nodes.append(current_node)
            for n in reversed(self.get_nearby_nodes(current_node)):
                if n not in nodes:
                    stack.append(n)
        return nodes
    
    def wps(self, root):
        nodes, queue = [], deque([root])
        while queue:
            current_node = queue.popleft()
            assert current_node not in nodes, 'Tree has a cycle!'
            
            nodes.append(current_node)
            for n in self.get_nearby_nodes(current_node):
                if n not in nodes:
                    queue.append(n)
        return nodes, is_cycle
        
    def display(self):
        from IPython.display import SVG, display
        import networkx as nx
        
        net_graph = nx.Graph()
        for e in self:
            net_graph.add_path(e)
        return SVG(nx.nx_agraph.to_agraph(net_graph).draw(prog='fdp', format='svg'))

class RootedTree(Tree):
    def __init__(self, root, edges=None):
        self.root = root
        super().__init__(edges)        
    
    def get_root(self):
        return self.root
    
    def get_parent(self, n):
        if n == self.root:
            return None
        for e in self.edges:
            if n == e[1]:
                return e[0]
        raise RuntimeWarning('parent is not found!')
    
    def get_decendant_nodes(self, n):
        return [e[1] for e in self.edges if n == e[0]]
    
    def dps(self, root):
        nodes, stack = [], [root]
        while stack:
            current_node = stack.pop()
            assert current_node not in nodes, 'Tree has a cycle!'
                
            nodes.append(current_node)
            for n in reversed(self.get_decendant_nodes(current_node)):
                stack.append(n)
        return nodes
    
    def wps(self, root):
        nodes, queue = [], deque([root])
        while queue:
            current_node = queue.popleft()
            assert current_node not in nodes, 'Tree has a cycle!'
            
            nodes.append(current_node)
            for n in self.get_decendant_nodes(current_node):
                queue.append(n)
        return nodes

n, q = map(int, inputs[0].split())
edges = [tuple(map(int, inputs[1+i].split())) for i in range(n-1)]
weights = [tuple(map(int, inputs[n+i].split())) for i in range(q)]

tree = RootedTree(root=1, edges=edges)

ans = defaultdict(int)
for node, w in weights:
    ans[node] += w

stack = [1]
while stack:
    x = stack.pop()
    for y in reversed(tree.get_decendant_nodes(x)):
        stack.append(y)
        ans[y] += ans[x]

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