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())
parent = [0 for _ in range(N)]
counter = [0 for _ in range(N)]

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

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

for i in range(1,N):
    counter[i] += counter[parent[i]]

print(" ".join(list(map(str,counter))),end="\n")

Submission status: Wrong Answer

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

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

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

#bfs---------------
queue = [0]
while(len(queue)):
    now = queue.pop(0)
    for i in child[now]:
        queue.append(i)
        counter[i] += counter[now]
print(" ".join(list(map(str,counter))),end="\n")

Submission status: Wrong Answer

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

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

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

#bfs---------------
queue = [0]
while(len(queue)):
    now = queue.pop(0)
    for i in child[now]:
        queue.append(i)
        counter[i] += counter[now]
print(" ".join(list(map(str,counter))),end="\n")

Submission status: Wrong Answer

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

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

point = [counter[i] for i in range(N)]
#bfs---------------
queue = [0]
while(len(queue)):
    now = queue.pop(0)
    for i in child[now]:
        queue.append(i)
        point[i] += point[now]
print(" ".join(list(map(str,point))),end="\n")

Submission status: Wrong Answer

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

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

point = [counter[i] for i in range(N)]
#bfs---------------
queue = [0]
while(len(queue)):
    now = queue.pop(0)
    for i in child[now]:
        queue.append(i)
        point[i] += point[now]
print(point)

Submission status: Wrong Answer

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

for i in range(1,N):
    now = i
    while(1):
        point[i] += counter[parent[now]]
        if parent[now] == 0:
            break
        else:
            now = parent[now]
print(" ".join(list(map(str,point))))

Submission status: Wrong Answer

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

for i in range(1,N):
    now = i
    while(1):
        point[i] += counter[parent[now]]
        if parent[now] == 0:
            break
        else:
            now = parent[now]
print(" ".join(list(map(str,point))))
print()

Submission status: Wrong Answer

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

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

counter = [0 for _ in range(N)]
for i in range(Q):
    counter[p[i]-1] += x[i]

for i in range(N-1):
    parent[b[i]-1] = a[i]-1

point = [counter[i] for i in range(N)]

for i in range(1,N):
    now = i
    while(1):
        point[i] += counter[parent[now]]
        if parent[now] == 0:
            break
        else:
            now = parent[now]
print(point)

Submission status: Wrong Answer

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

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

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


def dfs(curr, pare):
    for chi in g[curr]:
        if chi == pare: continue
        pl[chi] += pl[curr]
        dfs(chi, curr)

def main():
    dfs(0,-1)
    print(*pl)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q = list(map(int, input().split()))
tre = [[] for _ in range(N)]
val = [0] * N
sta = deque([1])
for i in range(N-1):
    a,b = list(map(int, input().split()))
    tre[a-1].append(b)

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

while len(sta) != 0:
    cur = sta.pop()
    v = val[cur-1]
    for target in tre[cur-1]:
        sta.append(target)
        val[target-1] += v

print(*val)

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q = list(map(int, input().split()))
tre = [[] for _ in range(N)]
val = [0] * N
q = deque([1])
for i in range(N-1):
    a,b = list(map(int, input().split()))
    tre[a-1].append(b)

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

while len(q) != 0:
    cur = q.popleft()
    v = val[cur-1]
    for target in tre[cur-1]:
        q.append(target)
        val[target-1] += v

print(*val, sep=" ")

Submission status: Wrong Answer

Solution code:
# D - Ki

import queue

class Node:
    def __init__(self, children, point):
        self.children = children
        self.point = 0

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

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

q = queue.Queue()
q.put(0)
while not q.empty():
    key = q.get()
    for child in nodes[key].children:
        nodes[child].point += nodes[key].point
        q.put(child)

print(*[node.point for node in nodes])

Submission status: Wrong Answer

Solution code:
# D - Ki

class Node:
    def __init__(self, children, point):
        self.children = children
        self.point = 0

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

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

for i in range(N):
    for child in nodes[i].children:
        nodes[child].point += nodes[i].point

print(*[node.point for node in nodes])

Submission status: Wrong Answer

Solution code:
# D - Ki

class Node:
    def __init__(self, children, point):
        self.children = children
        self.point = 0

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

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

for i in range(N):
    for child in nodes[i].children:
        nodes[child].point += nodes[i].point

print(*[node.point for node in nodes])

Submission status: Wrong Answer

Solution code:
# D - Ki

import queue

class Node:
    def __init__(self, children, point):
        self.children = children
        self.point = 0

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

for _ in range(Q):
    p, x = map(int, input().split())
    q = queue.Queue()
    q.put(p)
    while not q.empty():
        key = q.get()
        nodes[key - 1].point += x
        for child in nodes[key - 1].children:
            q.put(child)

print(*[node.point for node in nodes])

Submission status: Wrong Answer

Solution code:
N,Q = [int(x) for x in input().split()]
ki = [0]*N
ab = [0]*(N-1)

for i in range(N-1):
    ab[i] = [int(x)-1 for x in input().split()]

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1] += val

ab.sort()

for a,b in ab:
    ki[b]+=ki[a]

for i in ki:
    print(i,end=" ")
print()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
N, Q = list(map(int, input().split()))
tree = [[] for i in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    # tree[b-1].append(a-1)
counter = [0]*N
p_lis = [0] * Q
x_lis = [0] * Q
for i in range(Q):
    p, x = map(int, input().split())
    counter[p-1] += x
for i in range(N):
    for child in tree[i]:
        counter[child] += counter[i]
print(*counter)

Submission status: Wrong Answer

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

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

from collections import deque

todo = deque([(1,0)])
while len(todo) > 0:
    x,parent = todo.pop()
    ans[x] += ans[parent]
    for next in tree[x]:
        if next == parent:
            continue
        else:
            todo.append((next,x))
            
answer = ' '.join(map(str,ans[1:]))
print(answer)

Submission status: Wrong Answer

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

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

from collections import deque

count = [0]*N

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

queue = deque([0])
checked  = [0]*N
checked[0] = 1
while queue:
    v = queue.pop()
    checked[v] = 1
    x = count[v]
    Vs = query[v]
    for i in Vs:
        if checked[i]:
            continue
        count[i] += x
        queue.append(i)
    
print(*count)

Submission status: Wrong Answer

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

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

from collections import deque

count = [0]*N

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

queue = deque([0])

while queue:
    v = queue.pop()
    x = count[v]
    Vs = query[v]
    for i in Vs:
        count[i] += x
        queue.append(i)
    
print(*count)

Submission status: Wrong Answer

Solution code:
import queue
n,q = map(int,input().split())
c = [[] for _ in range(n)]
for i in range(n-1):
    A,B = map(int,input().split())
    c[A-1].append(B)
    c[B-1].append(A)
z = [0] * n
for i in range(q):
    P,X = map(int,input().split())
    z[P - 1] += X

r = [[] for _ in range(n)]

d = [-1] * n
d[0] = 0
q = queue.Queue()
q.put(1)
while(not q.empty()):
    tp = q.get()
    td = d[tp - 1]
    for n2 in c[tp - 1]:
        if(d[n2 - 1] == -1):
            d[n2 - 1] = td + 1
            r[n2 - 1].append(tp)
            q.put(n2)

o = [0]*n
for i in range(n):
    t = z[i]
    for rr in r[i]:
        t += z[rr - 1]
    o[i] = t
    z[i] = t
print(*o)

Submission status: Wrong Answer

Solution code:
import queue
n,q = map(int,input().split())
a = [0] * (n - 1)
b = [0] * (n - 1)
c = [[] for _ in range(n)]
for i in range(n-1):
    a[i],b[i] = map(int,input().split())
    c[a[i]-1].append(b[i])
    c[b[i]-1].append(a[i])
p = [0] * q
x = [0] * q
z = [0] * n
for i in range(q):
    p[i],x[i] = map(int,input().split())
    z[p[i] - 1] += x[i]

r = [[] for _ in range(n)]

d = [-1] * n
d[0] = 0
q = queue.Queue()
q.put(1)
while(not q.empty()):
    tp = q.get()
    td = d[tp - 1]
    for n2 in c[tp - 1]:
        if(d[n2 - 1] == -1):
            d[n2 - 1] = td + 1
            r[n2 - 1].append(tp)
            q.put(n2)

o = []
for i in range(n):
    t = z[i]
    for rr in r[i]:
        t += z[rr - 1]
    o.append(t)
    z[i] = t
print(' '.join(map(str,o)))

Submission status: Wrong Answer

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

def bfs(x,counter):
  qu=deque([])
  qu.append(x)
  qu.append(0)
  while qu:
    now=qu.popleft()
    pre=qu.popleft()
    counter[now-1]+=counter[pre-1]
    for i in edge[now]:
      if i!=pre:
        qu.append(i)
        qu.append(now)#あとであとのノードなるもの
      else:continue
  return counter
bfs(1,counter)
print(*counter)

Submission status: Wrong Answer

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

def bfs(x,counter):
  qu=deque([])
  qu.append(x)
  qu.append(0)
  while qu:
    now=qu.popleft()
    pre=qu.popleft()
    counter[now-1]+=counter[pre-1]
    for i in edge[now]:
      if i!=pre:
        qu.append(i)
        qu.append(now)#あとであとのノードなるもの
  return counter
bfs(1,counter)
print(*counter)

Submission status: Wrong Answer

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

def bfs(x,counter):
  qu=deque([])
  qu.append(x)
  qu.append(0)
  while qu:
    now=qu.popleft()
    pre=qu.popleft()
    counter[now-1]+=counter[pre-1]
    for i in edge[now]:
      if i!=pre:
        qu.append(i)
        qu.append(now)#あとであとのノードなるもの
  return counter
bfs(1,counter)
print(*counter)

Submission status: Wrong Answer

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

def bfs(x,counter):
  qu=deque([])
  qu.append(x)
  qu.append(0)
  while qu:
    now=qu.popleft()
    pre=qu.popleft()
    counter[now]+=counter[pre]
    for i in edge[now]:
      if i!=pre:
        qu.append(i)
        qu.append(now)#あとであとのノードなるもの
  return counter
bfs(1,counter)
print(counter)

Submission status: Wrong Answer

Solution code:
def main():
    N, Q = map(int, input().split())
    d = dict()
    num = [0]*N
    for i in range(N-1):
        a, b = map(int, input().split())
        d[b-1] = a-1  # stores the index of the parent node

    for i in range(Q):
        p, x = map(int, input().split())
        num[p-1] += x
    for i in range(len(num)):
        parent_idx = d.get(i, -1)
        if parent_idx >= 0:
            num[i] += num[parent_idx]

    print(*num)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans=[0]*(n+1)
for i in range(1,n+1):
  ans[i]+=c[i]
  for j in ab[i]:
    ans[j]+=ans[i]

c=[str(i) for i in ans[1:]]
print(" ".join(c))

Submission status: Wrong Answer

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

ans=[0]*(n+1)
for i in range(1,n+1):
  root=ab[i]
  ans[i]=c[i]
  while root != 0:
    ans[i]+=c[root]
    root=ab[root]
  
c=[str(i) for i in ans[1:]]
print(" ".join(c))

Submission status: Wrong Answer

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

for i in s:
  c[i] += c[ab[i]]
  
c=[str(i) for i in c[1:]]
print(" ".join(c))

Submission status: Wrong Answer

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

for i in range(2,n+1):
  c[i] += c[ab[i]]
  
c=[str(i) for i in c[1:]]
print(" ".join(c))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q=map(int,input().split())
edge=[[] for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  edge[a].append(b)

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

def bfs(x,x_list):
  qu=deque([])
  qu.append(x)
  while qu:
    node_number = qu.popleft()
    nodes = edge[node_number]
    for i in nodes:
      qu.append(i)
      x_list[i]+=x_list[node_number]
  return x_list
answer=bfs(1,x_list)[1:]
print(*answer)

Submission status: Wrong Answer

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

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

E = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    ai = a-1
    bi = b-1
    E[ai].append(bi)
    # E[bi].append(ai)

X = [list(map(int, input().split())) for _ in range(Q)]
C = [0 for _ in range(N)]

for x in X:
    C[x[0]-1] += x[1]

Seen = [False] * N


def dfs(v, c):
    Seen[v] = True
    C[v] += c

    if E[v] == []:
        return

    for nextv in E[v]:
        if Seen[nextv]:
            continue
        dfs(nextv, C[v])


dfs(0, 0)

print(*C)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

n, q = map(int, input().split())
child_dict = defaultdict(list)
value_dict = defaultdict(lambda: 0)
input_list = []
position_list = [10 * 6] * (n + 1)
position_list[0] = 1

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

for a, b in input_list:
    if position_list[a] < position_list[b]:
        a, b = b, a
    position_list[a] = position_list[b] + 1
    child_dict[a].append(b)

for _ in range(q):
    c, d = map(int, input().split())
    value_dict[c] += d
    

stack = deque()
stack.append(1)

while stack:
    top_ = stack.pop()
    for child in child_dict[top_]:
        value_dict[child] += value_dict[top_]
    stack.extend(child_dict[top_])
ans = [value_dict[i] for i in range(1 ,n+1)]
print(*ans)

Submission status: Wrong Answer

Solution code:
def count(x):
    if len(child_list[x]) > 0:
        for index in child_list[x]:
            total_list[index] += total_list[x]
            count(index)

n, q = map(int, input().split())
child_list = [[] for _ in range(n)]
position_list = [10 * 6] * n
position_list[0] = 1
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if position_list[a] < position_list[b]:
        a, b = b, a
    position_list[a] = position_list[b] + 1
    child_list[b].append(a)
total_list = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    total_list[p - 1] += x

count(0)
print(*total_list)

Submission status: Wrong Answer

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

abn = [None] * (n - 1)
for i in range(n - 1):
    abn[i] = list(map(int, input().split(' ')))

ws = [0] * n
for i in range(q):
    row = list(map(int, input().split(' ')))
    ws[row[0] - 1] += row[1]


tree = [None] * n
for ab in abn:
    tree[ab[1] - 1] = ab[0] - 1

result = [0] * n
for i in range(n):
    temp = i
    while True:
        result[i] += ws[temp]
        temp = tree[temp]
        if temp is None:
            break
for i in range(n):
    print(result[i])

Submission status: Wrong Answer

Solution code:
def count(x):
    total_list[x] += point_list[x]
    if len(child_list[x]) > 0:
        for index in child_list[x]:
            total_list[index] += total_list[x]
            count(index)

n, q = map(int, input().split())
child_list = [[] for _ in range(n)]
position_list = [10 * 6] * n
position_list[0] = 1
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if position_list[a] < position_list[b]:
        a, b = b, a
    position_list[a] = position_list[b] + 1
    child_list[b].append(a)
point_list = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    point_list[p - 1] += x

total_list = [0] * n
count(0)
print(*total_list)

Submission status: Wrong Answer

Solution code:
from collections import deque
 
N, Q = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
P = [0] * N
for _ in range(Q):
    p,q = map(int,input().split())
    P[p-1] += q
 
stack = deque([0])
parent = [-1] * N

while stack:
    v = stack.pop()
    if G[v]:
        w = G[v].pop()
        stack.append(w)
        if not parent[v] == w:
            parent[w] = v
            P[w] += P[v]

print(*P)

Submission status: Wrong Answer

Solution code:
from collections import deque
 
N, Q = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
P = [0] * N
for _ in range(Q):
    p,q = map(int,input().split())
    P[p-1] += q
 
stack = deque([0])
arrive = [0] * N
arrive[0] = 1
# print(P)
while stack:
    v = stack.pop()
    if G[v]:
        w = G[v].pop()
        stack.append(w)
        if arrive[w] == 0:
            arrive[w] = 1
            # print(w,P)
            P[w] += P[v]

 
print(*P)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
P = [0] * N
for _ in range(Q):
    p,q = map(int,input().split())
    P[p-1] += q

stack = deque([0])
arrive = [0] * N
arrive[0] = P[0]

while stack:
    v = stack.pop()
    if G[v]:
        w = G[v].pop()
        stack.append(w)
        if arrive[w] == 0:
            arrive[w] = arrive[v] + P[w]

print(*arrive)

Submission status: Wrong Answer

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

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

se = set([1])
stack = [1]
while stack:
    print(C)
    x = stack.pop()
    for y in graph[x]:
        if y in se:
            continue
        stack.append(y)
        se.add(y)
        # 親x, 子y
        C[y] += C[x]

print(' '.join(str(i) for i in C[1:]))

Submission status: Wrong Answer

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

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

stack = [1]
while stack:
    # print(C)
    x = stack.pop()
    for y in child[x]:
        stack.append(y)
        # 親x, 子y
        C[y] += C[x]

print(' '.join(str(i) for i in C[1:]))

Submission status: Wrong Answer

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

for i in range(1, N + 1):
    for k in range(i + 1, N + 1):
        for j in range(k + 1, N + 1):
            if k in child[i] and j in child[k]:
                child[i].add(j)

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

print(' '.join(str(i) for i in C[1:]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline 
sys.setrecursionlimit(10 ** 6) 
N,Q = map(int, input().split())

tree = [[] for _ in range(N)]

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

ans = [0] * N

for i in range(Q):
  p,x = map(int, input().split())
  ans[p - 1] += x
  
def dfs(cur, par):
  for chl in tree[cur]:
    ans[chl] += ans[cur]
    dfs(chl,cur)

dfs(0, -1)

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline 
sys.setrecursionlimit(10 ** 6) 
N,Q = map(int, input().split())

tree = [[] for _ in range(N)]

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

ans = [0] * N

for i in range(Q):
  p,x = map(int, input().split())
  ans[p - 1] += x
  
def dfs(cur, par):
  for chl in tree[cur]:
    if chl == par:
      continue
    ans[chl] += ans[cur] 
    dfs(chl,cur)

dfs(0, -1)

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline 
sys.setrecursionlimit(10 ** 6) 
N,Q = map(int, input().split())

tree = [[] for _ in range(N)]

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

print(tree)

ans = [0] * N

for i in range(Q):
  p,x = map(int, input().split())
  ans[p - 1] += x
  
def dfs(cur, par):
  for chl in tree[cur]:
    if chl == par:
      continue
    ans[chl] += ans[cur] 
    dfs(chl,cur)

dfs(0, -1)

print(*ans)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ab=sorted([list(map(int,input().split())) for _ in range(n-1)],key=lambda x:x[0])
d={}
for i in range(q):
  q,x=map(int,input().split())
  if q in d:
    d[q]+=x
  else:
    d[q]=x
ans=[0]*n
for i in ab:
  if i[0] in d:
    if ans[i[0]-1]==0:
      ans[i[0]-1]+=d[i[0]]
  if i[1] in d:
    if ans[i[1]-1]==0:
      ans[i[1]-1]+=d[i[1]]
  ans[i[1]-1]+=ans[i[0]-1]
print(' '.join(list(map(str,ans))))

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ab=sorted([list(map(int,input().split())) for _ in range(n-1)],key=lambda x:x[0])
d={}
for i in range(q):
  q,x=map(int,input().split())
  if q in d:
    d[q]+=x
  else:
    d[q]=x
ans=[0]*n
for i in ab:
  if i[0] in d:
    if ans[i[0]-1]==0:
      ans[i[0]-1]+=d[i[0]]
  if i[1] in d:
    if ans[i[1]-1]==0:
      ans[i[1]-1]+=d[i[1]]
  ans[i[1]-1]+=ans[i[0]-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,q=map(int,input().split())
    P=[0]*n
    P[0]=-1
    D=[-1]*n
    D[0]=0
    for i in range(n-1):
        a,b=map(int,input().split())
        a-=1
        b-=1  
        if D[a]==-1:
            P[a]=b
            D[a]=D[b]+1
        elif D[b]==-1:
            P[b]=a
            D[b]=D[a]+1
    X=[0]*n
    for i in range(q):
        p,x=map(int,input().split())
        X[p-1]+=x
    for i in range(1,n):
        X[i]+=X[P[i]]
    print(*X)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,q=map(int,input().split())
    P=[0]*n
    P[0]=-1
    for i in range(n-1):
        a,b=map(int,input().split())
        P[b-1]=a-1
    X=[0]*n
    for i in range(q):
        p,x=map(int,input().split())
        X[p-1]+=x
    for i in range(1,n):
        X[i]+=X[P[i]]
    print(*X)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

L = []
for i in range(N):
    L += [[i+1]]
#print(L)

for i in range(N-1):
    a,b = map(int, input().split())
    for j in range(N):
        if (a in L[j]) == True:
            L[j] += [b]
    #print(L)

A = np.zeros(N)
for i in range(Q):
    p,x = map(int, input().split())
    for i in L[p-1]:
        A[i-1] += x
    #print(A)
Astr=[str(int(i)) for i in A]
print(" ".join(Astr))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)

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

PX = [list(map(int, input().split())) for _ in range(Q)]
PX = sorted(PX)

points = [0] * N
def dfs(v, x):
  if len(G[v]) == 0:
    return
  for nv in G[v]:
    points[nv] += x
    dfs(nv, x)     

for p, x in PX:
  points[p-1] += x
  dfs(p-1, x)

print(*points)