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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
n, q = map(int, readline().split())
ab = [list(map(int, readline().split())) for i in range(n - 1)]
px = [list(map(int, readline().split())) for j in range(q)]
graph = [[] for _ in range(n + 1)]

for a, b in ab:
  graph[a].append(b)
  graph[b].append(a)
memo = [0] * (n + 1)
for p, x in px:
  memo[p] += x
stack = [(1, 0)]

while stack:
  x, parent = stack.pop()
  memo[x] += memo[parent]
  for y in graph[x]:
    if y == parent:
      continue
    stack.append((y, x))
print(*memo[1:])

Solution (Python):
import sys
from collections import OrderedDict

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = set()
        self.counter_dict[v] = 0

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

    def add_count(self, v, count):
        self.counter_dict[v] += count

    def dfs_stack(self, start):
        stack = [start]
        visited = set()

        parent_vertex = 0
        while stack:
            current_vertex = stack.pop()
            visited.add(current_vertex)
            for v in self.adjacency_dict[current_vertex]:
                if v not in visited and v != parent_vertex:
                    self.counter_dict[v] += self.counter_dict[current_vertex]
                    stack.append(v)
            parent_vertex = current_vertex

    def dfs_rec(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs_rec(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs_stack(1)
    
    tree.print_counters()


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
    

    
N,Q = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
    
graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
    
value = [0] * (N+1)
for p,x in PX:
    value[p] += x
    
def dfs(v,parent,add):
    value[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])
    
dfs(1,0,0)
    
answer = ' '.join(map(str,value[1:]))
print(answer)

Solution (Python):


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

graph = [[] for _ in range(N+1)]

for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)



counter = [0 for _ in range(N+1)]

parent = [None for _ in range(N+1)]

for p,x in PX:
    counter[p] += x

def dfs(graph,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

dfs(graph,counter,1)

ans = " ".join(map(str,counter[1:]))
print(ans)

Solution (Python):
import sys
readline = sys.stdin.readline
N, Q = map(int, readline().split())
T = [set() for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, readline().split())
    T[a - 1].add(b - 1)
    T[b - 1].add(a - 1)

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

done = set()
for s in range(N):
    if s in done:
        continue
    todo = [s]
    while todo:
        c = todo.pop()
        for n in T[c]:
            if not n in done:
                todo.append(n)
                ans[n] += ans[c]
        done.add(c)

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

Solution (Python):
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def dfs(G, v, p, point):
    """G: graph, v: vertex, p: parent"""
    
    for c in G[v]:
        if c == p:
            continue
        point[c] += point[v]
        dfs(G, c, v, point)


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

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

    dfs(G, 0, -1, point)
    print(" ".join(map(str, point)))


if __name__ == "__main__":
    main()

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

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

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

def dfs(v, pv):
  for nv in tree[v]:
    if nv != pv:
      points[nv] += points[v]
      dfs(nv, v)

dfs(0, 0)
print(*points)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, q = map(int, readline().split())
ab = [list(map(int, readline().split())) for i in range(n - 1)]
px = [list(map(int, readline().split())) for j in range(q)]
graph = [[] for _ in range(n + 1)]
for a, b in ab:
    graph[a].append(b)
    graph[b].append(a)
memo = [0] * (n + 1)
for p, x in px:
    memo[p] += x
stack = [(1, 0)]
while stack:
    x, parent = stack.pop()
    memo[x] += memo[parent]
    for y in graph[x]:
        if y == parent:
            continue
        stack.append((y, x))
print(*memo[1:])

Solution (Python):
def submit():
  from collections import deque
  import sys
  input = sys.stdin.readline
  n,q = map(int,input().split())
  y = [[] for _ in range(n)]
  c = [0]*n
  for _ in range(n-1):
    ai,bi = map(int,input().split())
    y[ai-1].append(bi-1)
    y[bi-1].append(ai-1)
  for _ in range(q):
    pi,xi = map(int,input().split())
    c[pi-1] += xi
  z = deque([0])
  while z:
    zp = z.popleft()
    j = zp//10**6
    b = zp%(10**6)
    for yj in y[j]:
      if yj != b:
        z.append(yj*10**6+j)
        c[yj] += c[j]
  for i in range(n):
    print(c[i],end=" ")
submit()

Solution (Python):
def submit():
  from collections import deque
  import sys
  input = sys.stdin.readline
  n,q = map(int,input().split())
  y = [[] for _ in range(n)]
  c = [0]*n
  for _ in range(n-1):
    ai,bi = map(int,input().split())
    y[ai-1].append(bi-1)
    y[bi-1].append(ai-1)
  for _ in range(q):
    pi,xi = map(int,input().split())
    c[pi-1] += xi
  z = deque([[0,-1]])
  while z:
    j,b = z.popleft()
    for yj in y[j]:
      if yj != b:
        z.append([yj,j])
        c[yj] += c[j]
  for i in range(n):
    print(c[i],end=" ")
submit()

Solution (Python):
from collections import deque
def submit():
  n,q = map(int,input().split())
  y = [[] for _ in range(n)]
  c = [0]*n
  for _ in range(n-1):
    ai,bi = map(int,input().split())
    y[ai-1].append(bi-1)
    y[bi-1].append(ai-1)
  for _ in range(q):
    pi,xi = map(int,input().split())
    c[pi-1] += xi
  z = deque([[0,-1]])
  while z:
    j,b = z.popleft()
    for yj in y[j]:
      if yj != b:
        z.append([yj,j])
        c[yj] += c[j]
  for i in range(n):
    print(c[i],end=" ")
submit()

Solution (Python):
from collections import deque
n,Q=map(int,input().split())
tr=[[] for _ in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    tr[a-1].append(b-1)
    tr[b-1].append(a-1)
counter=[0]*n
for _ in range(Q):
    p,x=map(int,input().split())
    counter[p-1]+=x
al=[0]*n
qi=deque([0])
while qi:
    x=qi.pop()
    al[x]=1
    for i in tr[x]:
        if al[i]==0:
            counter[i]+=counter[x]
            qi.append(i)
print(*counter)

Solution (Python):


import sys














def main():
    mod = 10000007                  
    inf = float('inf')
    input = sys.stdin.readline      
    sys.setrecursionlimit(10**6)    
    def ii():  return int(input())
    def mi():  return map(int, input().rstrip().split())
    def lmi(): return list(map(int, input().rstrip().split()))
    def li():  return list(input().rstrip())


    def dfs_traverse(start, ans_lst, val=0):
        val += counter[start]
        ans_lst[start] = val
        for v in adj[start]:
            if ans_lst[v] is None:
                dfs_traverse(v, ans_lst, val)
    
    
    n, q = mi()
    L = [list(map(lambda x: int(x)-1, input().rstrip().split())) for _ in range(n-1)]
    count_up_commands = [lmi() for _ in range(q)]

    adj = [[] for _ in range(n)]
    for link in L:
        adj[link[0]].append(link[1])
        adj[link[1]].append(link[0])
    
    counter = [0] * n
    for command in count_up_commands:
        ind = command[0] -1
        count = command[1]
        counter[ind] += count
    
    ans_lst = [None] * n
    dfs_traverse(0, ans_lst)
    for ans in ans_lst:
        print(ans, end=" ")
    print("")


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n,q = map(int,input().split())
tree = [[] for i in range(n)]
ans = [0]*n
for i in range(n-1):
    a,b = map(lambda x:int(x)-1,input().split())
    tree[a].append(b)
    tree[b].append(a)

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

d = deque([[0,0]])
c = [False]*n
c[0] = True
while d:
    x,s = d.pop()
    s += ans[x]
    ans[x] = s
    for e in tree[x]:
        if not c[e]:
            c[e] = True
            d.append([e,s])

print(*ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def main():
    n, q = map(int, input().split())
    edges = [[] for _ in range(n+1)]

    for a, b in readlines(n-1):
        edges[a].append(b)
        edges[b].append(a)

    counter = [0] * (n+1)
    for q, x in readlines(q):
        counter[q] += x

    def dfs(u, prev):
        for v in edges[u]:
            if v == prev:
                continue
            counter[v] += counter[u]
            dfs(v, u)

    dfs(1, 0)

    print(" ".join(map(str, counter[1:])))

main()

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

N, Q = list(map(int, input().split()))
nodes = [[] for i in range(N)]
for i in range(N-1):
  r, e = list(map(int, input().split()))
  r, e = r - 1, e - 1
  nodes[r].append(e)
  nodes[e].append(r)


result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i == prev:
      continue
    result[i] += result[r]
    dfs(i, r)

if __name__ == "__main__":
  dfs(0)
  print(*result)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

def dfs(v, parent):
    for u in lines[v]:
        if u != parent:
            ans[u] += ans[v]
            dfs(u, v)

dfs(0, -1)
print (*ans)

Solution (Python):
N, Q = [int(i) for i in input().split()]
G = [[] for _ in range(N)]
counter = [0] * N
res = [0] * N
visited = [0] * N

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

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








stack = [0]
parent = [0]
while stack:
    v = stack.pop()
    p = parent.pop()
    visited[v] = 1
    res[v] = res[p]+counter[v]
    for nv in G[v]:
        if visited[nv]: continue
        stack.append(nv)
        parent.append(v)


print(*res)

Solution (Python):
mod = 10**9 + 7
from collections import deque

def main():
    N, Q = iip(listed=False)

    ql = [0 for i in range(N)]
    ll = [[] for i in range(N)]

    for i in range(N-1):
        a, b = iip(True)
        ll[a-1].append(b-1)
        ll[b-1].append(a-1)

    for i in range(Q):
        p, x = iip(True)
        ql[p-1] += x

    que = deque([])
    que.append((0, None))

    result = [None for i in range(N)]
    while que:
        data = que.popleft()
        node = data[0]
        before_node = data[1]
        if before_node is None:
            before_num = 0
        else:
            before_num = result[before_node]

        result[node] = before_num + ql[node]

        for child in ll[node]:
            if child != before_node:
                que.append((child, node))


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






def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    while r-l > 1:
        x = (l+r)//2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        return r
    if side == "right":
        return l+1



def iip(listed):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        
        pass
    return ret


def inv(n, mod):
    return power(n, mod-2)

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod


if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Solution (Python):
from collections import deque

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

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

for _ in range(q):
    u, v = map(int, input().split())
    cnt[u] += v

q = deque()
q.append(1)
checked = [0] * (n + 1)

while q:
    v = q.pop()
    checked[v] = 1
    for u in g[v]:
        if checked[u] == 1:
            continue
        cnt[u] += cnt[v]
        q.append(u)
print(*cnt[1:])

Solution (Python):
import queue
import sys
input = sys.stdin.readline

class Tree:
    def __init__(self, ind):
        self.children = []
        self.counter = 0
        self.index = ind
        self.parent = -1


N, Q = map(int, input().strip().split(" "))
tree = [Tree(i) for i in range(N)]
ans = [0 for i in range(N)]
for i in range(N - 1):
    a, b = map(int, input().strip().split(" "))
    tree[a - 1].children.append(b - 1)
    tree[b - 1].children.append(a - 1)

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


que = queue.Queue()
que.put(0)  

while not que.empty():
    ind = que.get()
    ans[ind] = tree[ind].counter
    for child in tree[ind].children:
        if child == tree[ind].parent:
            continue
        tree[child].counter += tree[ind].counter
        tree[child].parent = ind
        que.put(child)

print(' '.join([str(a) for a in ans]))

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

stk = deque()
stk.append((0,-1))
while stk:
    cur,prev = stk.pop()
    for nxt in ki[cur]:
        if nxt == prev: continue
        ans[nxt] += ans[cur]
        stk.append((nxt,cur))

print(*ans)

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

count = [0]*N

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

used = [0]*N
used[0] = 1
que = deque([0])

while que:
  i = que.popleft()
  for j in AB[i]:
    if used[j] != 0:
      continue
    used[j] = 1
    count[j] += count[i]
    que.append(j)

print(*count)

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

n , q = map(int,input().split())
point = [0] * n
edge = [[] for i in range(n)]

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

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

def dfs(now, last=-1):
    for nxt in edge[now]:
        if nxt==last: continue
        point[nxt] += point[now]
        dfs(nxt, now)
dfs(0)
print(*point)

Solution (Python):
import sys
input=sys.stdin.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())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
ans=[0 for _ in range(n)]
def dfs(u,par):
    if par!=-1:
        ans[u]+=ans[par]
    for v in g[u]:
        if v==par:
            continue
        dfs(v,u)
for _ in range(q):
    p,x=map(int,input().split())
    p-=1
    ans[p]+=x
dfs(0,-1)
print(*ans)

Solution (Python):
if __name__ == "__main__":
    N, K = map(int, input().split())
    G = [[] for _ in range(N)]
    for n in range(N-1):
        a, b = map(int, input().split())
        G[a - 1].append(b - 1)
        G[b - 1].append(a - 1)

    C = [0] * N

    for k in range(K):
        p, x = map(int, input().split())
        C[p - 1] += x

    V = [False]*N
    stack = [0]
    while stack:
        x = stack.pop()
        V[x] = True
        for y in G[x]:
            if V[y]:
                continue

            C[y] += C[x]
            stack.append(y)

    print(*C)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

n, q = map(int, input().split())
G = [[] for _ in range(n+1)]
for i in range(n-1):
    x, y = map(int, input().split())
    G[x].append(y)
    G[y].append(x)

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

"""

ans_dfs = [0 for _ in range(n)]
def dfs(now, parent, S):
    ans_dfs[now] = S + counter[now]
    for to in G[now]:
        if to == parent: continue
        dfs(to, now, ans_dfs[now])

"""

class p:
    num = -1
    parent = -1

    def __init__(self, x, y):
        self.num = x
        self.parent = y

node_list = []
ans = [0 for _ in range(n+1)]

def main():

    
    
    stack = []
    stack.append(p(1, -1))
    while len(stack) != 0:
        now = stack.pop()
        node_list.append(now)
        for to in G[now.num]:
            if to == now.parent: continue
            stack.append(p(to, now.num))

    for now in node_list:
        ans[now.num] = counter[now.num] + ans[now.parent]

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



if __name__ == '__main__':
    main()

Solution (Python):

n, q = map(int, input().split())
G = [[] for _ in range(n+1)]
for i in range(n-1):
    x, y = map(int, input().split())
    G[x].append(y)
    G[y].append(x)

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

"""

ans_dfs = [0 for _ in range(n)]
def dfs(now, parent, S):
    ans_dfs[now] = S + counter[now]
    for to in G[now]:
        if to == parent: continue
        dfs(to, now, ans_dfs[now])

"""

class p:
    num = -1
    parent = -1

    def __init__(self, x, y):
        self.num = x
        self.parent = y

node_list = []
ans = [0 for _ in range(n+1)]

def main():

    
    
    stack = []
    stack.append(p(1, -1))
    while len(stack) != 0:
        now = stack.pop()
        node_list.append(now)
        for to in G[now.num]:
            if to == now.parent: continue
            stack.append(p(to, now.num))

    for now in node_list:
        ans[now.num] = counter[now.num] + ans[now.parent]

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



if __name__ == '__main__':
    main()

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

C = [0] * N


for k in range(K):
    p, x = map(int, input().split())
    C[p - 1] += x


V = [False]*N
Q = [0]
while Q:
    x = Q.pop()
    V[x] = True
    for y in G[x]:
        if not V[y]:
            C[y] += C[x]
            Q.append(y)
print(*C)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N, Q = input_nums()
    C = [0]*N
    G = [[] for i in range(N)]
    for i in range(N-1):
        a, b = input_nums()
        G[a-1].append(b-1)
        G[b-1].append(a-1)

    for i in range(Q):
        p, x = input_nums()
        C[p-1] += x
    isVisit = [False]*N
    isVisit[0] = True
    stack = [0]

    while stack:
        now = stack.pop()
        for i in G[now]:
            if not isVisit[i]:
                isVisit[i] = True
                stack.append(i)
                C[i] += C[now]
    print(*C)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
N, Q = map(int, input().split())
G = [[] for _ in range(N)]
count = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for i in range(Q):
    p, x = map(int, input().split())
    count[p-1]+=x
q = deque([0])
flag = [False]*N
while q:
    v = q.pop()
    flag[v] = True
    for e in G[v]:
        if flag[e]:
            continue
        count[e]+=count[v]
        q.append(e)
print(*count)

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

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

def dfs(v, parent):
    for u in lines[v]:
        if u != parent:
            ans[u] += ans[v]
            dfs(u, v)

dfs(0, -1)
print (*ans)

Solution (Python):
import sys



from collections import Counter, deque, defaultdict












readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

from collections import defaultdict


class Graph:
    

    def __init__(self, vertices=0):
        self.edges = defaultdict(list)
        self.counter = [0] * vertices
        self._vertex_count = vertices
        self._edges_count = 0

    @property
    def edges_count(self):
        return self._edges_count

    @property
    def vertices_count(self):
        return self._vertices_count

    def add_edge(self, u, v):
        self.edges[u].append(v)
        self.edges[v].append(u)
        self._edges_count += 1

    def add_counter(self, u, cnt):
        self.counter[u] += cnt

    def add_directed_edge(self, u, v):
        self.edges[u].append(v)
        self._edges_count += 1

    def del_edge(self, u, v):
        self.edges[u] = [_ for _ in self.edges[u] if _[0] != v]
        self.edges[v] = [_ for _ in self.edges[v] if _[0] != u]
        self._edges_count -= 1

    
    def neighbors(self, index):
        return self.edges[index]

    
    def neighbors_count(self, index):
        return len(self.edges[index])

    
    def __str__(self):
        desc = ""
        for u in range(self._edges_count):
            for v in self.edges[u]:
                if u < v:
                    desc += "{} {}".format(u, v)
        return desc


def main():
    n, q = list(map(int, readline().split()))
    g = Graph(n)
    visited = [False] * n

    for _ in range(n - 1):
        a, b = list(map(int, readline().split()))
        a, b = a - 1, b - 1
        g.add_edge(a, b)

    for _ in range(q):
        p, x = list(map(int, readline().split()))
        p = p - 1
        g.add_counter(p, x)

    que = deque()
    que.append((0, 0))
    visited[0] = True

    while que:
        cur, cnt = que.popleft()
        g.add_counter(cur, cnt)
        nxtcnt = g.counter[cur]

        for nxt in g.neighbors(cur):
            if not visited[nxt]:
                visited[nxt] = True
                que.append((nxt, nxtcnt))

    print(*g.counter, sep="\n")


if __name__ == '__main__':
    main()

Solution (Python):
N,Q=map(int,input().split())
E=[[] for _ in range(N)]
for i in range(N-1):
  x,y=map(int,input().split())
  x-=1
  y-=1
  E[x].append(y)
  E[y].append(x)
par=[-1]*N
par[0]=0
stack=[0]
order=[]
while stack:
  u=stack.pop()
  order.append(u)
  for v in E[u]:
    if par[v]==-1:
      stack.append(v)
      par[v]=u
counter=[0]*N
for i in range(Q):
  u,x=map(int,input().split())
  u-=1
  counter[u]+=x
for i in order:
  if i==0:
    pass
  else:
    counter[i]+=counter[par[i]]
for i in range(N):
  counter[i]=str(counter[i])
print(' '.join(counter))

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

def DFS(v, parent):
  global v_count
  for u in adj[v]:
    if u != parent:
      v_count[u] += v_count[v]
      DFS(u,v)

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

adj = [[] for _ in range(N)]
v_count = [0] * N

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

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


DFS(0, -1)
print(*v_count)

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

def DFS(v, parent):
  for u in adj[v]:
    if u != parent:
      v_count[u] += v_count[v]
      DFS(u,v)

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

adj = [[] for _ in range(N)]
v_count = [0] * N

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

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


DFS(0, -1)
print(*v_count)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, q = map(int, input().split())

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

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

def dfs(v1, parent, add):
    values[v1] += add
    for v2 in graph[v1]:
        if v2 == parent:
            continue
        dfs(v2, v1, values[v1])

dfs(0, 0, 0)
print(*values)

Solution (Python):

from collections import deque 
def main():
    n, q = map(int, input().split())
    to = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        to[a].append(b)
        to[b].append(a)

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

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

    dq = deque() 
    dq.append(0)
    done = [0 for _ in range(n)]
    done[0] = 1
    while len(dq) > 0:
        x = dq.popleft()
        for i in to[x]:
            if done[i] == 1:
                continue
            node[i] += node[x]
            done[i] = 1 
            dq.append(i)

    for i in range(n):
        print(node[i]) if i == n-1 else print(node[i], end=' ')
main()