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):
n, q = (int(_) for _ in input().split())
res = [0] * n
l = [[] for i in range(n-1)]
for i in range(n-1):
    a, b = (int(_) for _ in input().split())
    l[a-1].append(b)
p = [list(map(int, input().split())) for i in range(q)]

for i in p:
    res[i[0]-1] += i[1]
for i in range(n-1):
    for ii in l[i]:
        res[ii-1] += res[i]

[print(i,end=" ") for i in res]
print()

Solution (Python):
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def init(n):
  for i in range(N):
    par[i]=i
    num[i]=0
def find(x):
  if par[x]==x:
    return x
  else:
    return find(par[x])
def unite(x,y):
  if (y<x):
    par[x]=y
  else:
    par[y]=x
def same(x,y):
  return find(x)==find(y)
def cnt(x):
  if par[x]==x:
    res = num[x]
    return res
  else:
    if used[par[x]] == True:
      res = num[x] + ans[par[x]]
      return res
    else:
      return

N,Q = IL()
ab = [IL() for i in range(N-1)]
px = [IL() for i in range(Q)]

par=[0 for i in range(N+1)]
num=[0 for i in range(N+1)]

init(N)

for a,b in ab:
  unite(a,b)

for p,x in px:
  num[p] += x

ans = [0 for i in range(N+1)]
used = [False for i in range(N+1)]
for i in range(1,N+1):
  ans[i] = cnt(i)
  used[i] = True

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

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)]
dp = [0] * n
ls = [[] for i in range(n)]
for i in ab:
    ls[i[0]-1].append(i[1]-1)
for i in px:
    dp[i[0]-1] += i[1]
for i in range(n):
    for j in ls[i]:
        dp[j] += dp[i]
print(*dp)

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

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

Tree_sorted = sorted(Tree, key=lambda x:x[0])

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

scores = [0 for _ in range(N + 1)]
for i in range(Q):
    scores[P[i]] += X[i]

for tree in Tree_sorted:
    scores[tree[1]] += scores[tree[0]]

for i, score in enumerate(scores):
    if i == 0:
        continue
    print(score, end=' ')

Solution (Python):
N ,Q = map(int,input().split())
A =[]
B =[]
P =[]
X =[]
node_child = [[] for i in range(N)]
node_score = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
for i in range(Q):
    p,x = map(int,input().split())
    P.append(p)
    X.append(x)
for i in range(N-1):
    node_child[A[i]-1].append(B[i])
for i in range(Q):
    node_score[P[i]-1].append(X[i])
score = [0]*N

for i in range(N):
    score[i] += sum(node_score[i])
    for j in node_child[i]:
        score[j-1] += score[i]
for i in score:
    print('{0} '.format(i),end='' )

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n,q = get_nums_l()

lines = sys.stdin.readlines()
edges = list(map(lambda s:list(map(int, s.split(" "))), lines[:n-1]))
queries = list(map(lambda s:list(map(int, s.split(" "))), lines[n-1:]))

scores = [0] * (n+1)

for q in queries:
    scores[q[0]] += q[1]

queue = deque()
mark = [False] * (n+1)
mark[1] = True


childrens = [ [] for _ in range(n+1) ]
for e in edges:
    a = e[0]
    b = e[1]
    childrens[a].append(b)
    childrens[b].append(a)


queue.append(1)

while queue:
    a = queue.popleft()
    children = childrens[a]
    for b in children:
        if not mark[b]:
            scores[b] += scores[a]
            mark[b] = True
            queue.append(b)

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

Solution (Python):
N,Q = map(int,input().split())
parents = {1:0}
for _ in range(N-1):
    a,b = map(int,input().split())
    key,val = max(a,b), min(a,b)
    parents[key] = val
counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
results = [0]*(N+2)
for i in range(1,N+1):
    results[i] = counts[i]+results[parents[i]]
    print(results[i], end=' ')

Solution (Python):
N, Q = map(int,input().split()) 
List_ab = [list(map(int,input().split())) for i in range(N-1)] 
List_px = [list(map(int,input().split())) for i in range(Q)] 

count_list = [0]*N

List_ab.sort()

for i in range(Q):
    count_list[List_px[i][0]-1] += List_px[i][1]
        
for i in range(N-1):
    count_list[List_ab[i][1]-1] += count_list[List_ab[i][0]-1]
    
print(*count_list)

Solution (Python):
import collections

n, q = map(int, input().split())
nxts = collections.defaultdict(list)
score = [0] * n 
for _ in range(n - 1):
    a, b = map(int, input().split())
    nxts[a - 1].append(b - 1)
    nxts[b - 1].append(a - 1)
for i in range(q):
    p, x = map(int, input().split())
    score[p - 1] += x


stack = [(0, -1)]
while stack:
    node, parent = stack.pop()
    for nxt in nxts[node]:
        if nxt != parent:
            score[nxt] += score[node]
            stack.append((nxt, node))

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

Solution (Python):
N, Q = map(int, input().split())
tree = [[] for i in range(N)]
for i in range(N-1):
    ai, bi = map(int, input().split())
    ai -= 1
    bi -= 1
    tree[ai].append(bi)
point = [0] * N
for j in range(Q):
    pi, xi = map(int, input().split())
    pi -= 1
    point[pi] += xi

answer = [0] * N
queue = []
counter = 0
queue.append((0, 0))
while True:
    if counter == len(queue):
        break
    node, p = queue[counter]
    counter += 1


    answer[node] = p + point[node]
    for t in tree[node]:
        queue.append((t, answer[node]))

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

Solution (Python):
from collections import deque

N, Q = list(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)]

points = [0 for _ in range(N+1)]
g = [[] for _ in range(N+1)]
for a, b in AB:
    g[a].append(b)



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

q = deque([1])
while q:
    node = q.popleft()
    for child in g[node]:
        points[child] += points[node]
        q.append(child)

for point in points[1:]:
    print(point)

Solution (Python):
import collections

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

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

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

    vals = [0 for _ in range(N)]
    v = 0
    if 1 in ops: v = ops[1]
    cur = [[1, v]]

    while cur:
        temp = []
        for a, v in cur:
            vals[a-1] += v
            for b in graph[a]:
                temp.append([b, v+ops[b]])
        cur = temp

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

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
N, Q = list(map(int, stdin.readline().rstrip().split(' ')))
edge = [list(map(int, stdin.readline().rstrip().split(' '))) for _ in range(N-1)]
vals = [list(map(int, stdin.readline().rstrip().split(' '))) for _ in range(Q)]

res = [0] * N
for i, v in vals:
    res[i-1] = res[i-1] + v

edge.sort()
for p, d in edge:
    res[d-1] = res[d-1] + res[p-1]

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

Solution (Python):
from collections import deque


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


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



par = [0] * n
que = deque([0])

while que:
    cur = que.popleft()
    for nxt in graph[cur]:
        if nxt == par[cur]:
            continue
        cost[nxt] += cost[cur]
        que.append(nxt)
        par[nxt] = cur





print(*cost)

Solution (Python):
n,q = map(int,input().split())
sco = [0]*(n+1)
edge = [[i] for i in range(n+1)]

def dfs(i,s):
    E = edge[i]
    if(len(E)==1):
        return 0
    for e in E:
        if(e==i):continue
        sco[e]+=s
        dfs(e,s)

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

for i in range(q):
    p,x=map(int,input().split())
    sco[p]+=x
for i in range(1,n+1):
    E=edge[i]
    for e in E:
        if(e==i):continue
        sco[i]+=sco[e]
print(*sco[1:])

Solution (Python):
from collections import deque

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

adj = {}
val = [0 for i in range(N+1)]
ans = [0 for i in range(N+1)]
check = [False for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    if a not in adj:
        adj[a] = []
    if b not in adj:
        adj[b] = []
    adj[a].append(b)
    adj[b].append(a)

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

queue = deque([(1,0)])

while len(queue):
    q, c = queue.pop()
    check[q]=True
    ans[q] = val[q] + c
    for nq in adj[q]:
        if check[nq] == False:
            queue.append((nq,ans[q]))
print(*ans[1::])

Solution (Python):
n, q = map(int, input().split())
ko=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int, input().split())
    ko[a-1].append(b-1)
ans=[0]*n
for i in range(q):
    a,b=map(int, input().split())
    ans[a-1]+=b
nex=[0]
while nex:
    p=nex.pop()
    for i in ko[p]:
        ans[i]+=ans[p]
    nex+=ko[p]
print(*ans)

Solution (Python):
n, q = list(map(int, input().split()))
tree = [[] for i in range(n)]
for i in range(n - 1):
    a, b= list(map(int, input().split()))
    tree[a-1].append(b-1)
s = [0] * n
for i in range(q):
    p, x = list(map(int, input().split()))
    s[p-1] += x

queue = [0]

while(len(queue) != 0):
    n = queue.pop()
    for i in range(len(tree[n])):
        s[tree[n][i]] += s[n]
        queue.append(tree[n][i])








print(" ".join(list(map(str, s))))

Solution (Python):
N,Q=map(int, input().split())
node_lst=[list(map(int, input().split())) for i in range(N-1)]
ope_lst=[list(map(int, input().split())) for i in range(Q)]
cnt_lst=[0 for i in range(N)]
for ope in ope_lst:
    p, x = ope[0], ope[1]
    cnt_lst[p-1]+=x
node_lst=sorted(node_lst, key=lambda x:x[0])
for l in node_lst:
    src, dst = l[0]-1, l[1]-1
    cnt_lst[dst]+=cnt_lst[src]
print(" ".join([str(c) for c in cnt_lst]))

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):
import sys
input=sys.stdin.readline

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

V = [0]*N

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





for p in P:
    V[p[0]-1] += p[1]
for a in A:
    V[a[1]-1] += V[a[0]-1]

print(*V)

Solution (Python):
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
AB = [None] * (N-1)
for i in range(N-1):
    a, b = map(int, input().split())
    AB[i] = (a, b)
PX = [None] * (Q)
for i in range(Q):
    p, x = map(int, input().split())
    PX[i] = (p, x)
    
edges = AB[:]
vals = PX[:]

from collections import defaultdict
vals_dict = defaultdict(lambda: 0)
for v, n in vals:
    vals_dict[v] += n


graph = defaultdict(lambda: {})
for a, b in edges:
    graph[a][b] = vals_dict[b]
    graph[b][a] = vals_dict[a]

    

from heapq import heappush, heappop
def dijkstra(graph, start, N):
    A = [None] * (N+1)
    queue = [(0, start)]
    while queue:
        path_len, v = heappop(queue)
        if A[v] is None: 
            A[v] = path_len
            for w, edge_len in graph[v].items():
                if A[w] is None:
                    heappush(queue, (path_len + edge_len, w))

    
    return [0 if x is None else x for x in A]

shortest = dijkstra(graph, 1, N)
shortest = [s+vals_dict[1] for s in shortest]

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

Solution (Python):
def solve():
    cnt = [0] * int(2 * 1e5 + 10)
    li = []
    n, q = map(int, input().split())
    for _ in range(n - 1):
        a, b = map(int, input().split())
        li.append((a, b))
    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x
    for a, b in sorted(li):
        cnt[b] += cnt[a]
    print(*cnt[1 : n + 1])
solve()

Solution (Python):
n,qq = map(int,input().split())

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

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


l = [0]*n
count = [0]*n

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

p = [True]*n
p[0] = False


q = [0]

while q:
    v = q.pop()
    for w in g[v]:
        if p[w]:
            p[w] = False
            l[w] += l[v]
            q.append(w)

print(*l)

Solution (Python):
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n-1)]
ans = [0 for _ in range(n)]
for _ in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x
tree = [[]for _ in range(n)]
for a, b in ab:
  tree[a-1].append(b-1)
del ab
for index, i in enumerate(tree):
  for j in i:
    ans[j] += ans[index]
print(" ".join(map(str, ans)))

Solution (Python):
from collections import deque

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

stack = deque()
stack.append(0)
ans = [0] * N
ans = V

while stack:
    v = stack.popleft()
    for e in E[v]:
        ans[e] += ans[v]
        stack.append(e)

print(*ans)

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

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

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

res = [0] * (n+1)
queue = [(1, root_value[1])]
while len(queue) > 0:
    pair = queue.pop()
    res[pair[0]] += pair[1]
    if neighbor[pair[0]] != []:
        for i in neighbor[pair[0]]:
            queue.append((i, pair[1] + root_value[i]))
    
for i in res[1:]:
    print(i, end=" ")

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

dic = {}
lst = [0] * (N+1)

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


for i in range(Q):
  p, x = map(int,input().split())
  lst[p] += x
  
for i in range(1,N+1):
  print(lst[i],end=' ')
  if i in dic.keys():
    tmp_lst = dic[i]
    for j in tmp_lst:
      lst[j] += lst[i]
    
print()

Solution (Python):
def main():
    inp = input().split(' ')
    n = int(inp[0])
    q = int(inp[1])
    ab = {}
    tree = [0 for i in range(n)]
    for i in range(n-1):
        inp = input().split(' ')
        ab[int(inp[1])] = int(inp[0])
    for i in range(q):
        inp = input().split(' ')
        tree[int(inp[0])-1] += int(inp[1])
    for i in range(n):
        parent = ab.get(i+1)
        if parent is None:
            continue
        else:
            tree[i] += tree[parent-1]
    str_tree = list(map(lambda x: str(x), tree))
    print(' '.join(str_tree))


if __name__ == '__main__':
    main()

Solution (Python):
N, Q = map(int, input().split())
root = []
counter = [0] * N
ans = [0] * N

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

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

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

ans[0] = counter[0]

for k in range(1,N):
    ans[k] = counter[k] + ans[root[k-1][0]-1]

print(*list(ans))

Solution (Python):
from collections import deque

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

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


visited = [0]*N
q = deque()
q.append(0)
while q:
    v = q.pop()
    x0 = ans[v]
    visited[v] = 1
    for u in table[v]:
        if visited[u]:
            continue
        q.append(u)
        ans[u] += x0
for i in range(N):
    print(ans[i])

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


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

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


def recursive(p):
    for c in AB[p]:
        X[c] += X[p]
        recursive(c)


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

Solution (Python):
n, q = map(int, input().split())
d = {1: [0, 0]}
for i in range(n-1):
    a, b = map(int, input().split())
    d[b] = [a, 0]

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

for i in range(2, n+1):
    d[i][1] += d[d[i][0]][1]

l = []
for i in sorted(d.keys()):
    l.append(d[i][1])
    
print(" ".join(map(str, l)))

Solution (Python):
n,m = map(int,input().split())
uv = [tuple(map(int, input().split())) for _ in range(n-1)]
colors = [0 for _ in range(n+1)]
for i in range(m):
    p,x = map(int,input().split())
    colors[p] += x
edges = [dict() for _ in range(n+1)]
for u, v in uv:
    edges[u][v] = 0
plan = set((1,))
while len(plan) > 0:
    g = plan.pop()
    for n,w in edges[g].items():
            colors[n] = colors[g] +colors[n]
            plan.add(n)
print(*colors[1:])

Solution (Python):
from sys import stdin

N,Q = [int(x) for x in stdin.readline().rstrip().split()]
ans = [0] * (N+1)
node = []
for _ in range(N-1):
    a,b = [int(x) for x in stdin.readline().rstrip().split()]
    node.append((a,b))
for _ in range(Q):
    p,x = [int(x) for x in stdin.readline().rstrip().split()]
    ans[p] += x
    
for i in sorted(node):
    a,b = i
    ans[b] += ans[a]

for j in ans[1:]:
    print(j,end=" ")

Solution (Python):



def solve(N, Q, a, b, p, x):
    parent = [0] * (N+1)
    for ai, bi in zip(a, b):
        parent[bi] = ai
    plus = [0] * (N+1)
    for pi, xi in zip(p, x):
        plus[pi] += xi
    ans = [0] * (N+1)
    for i in range(1, N+1):
        ans[i] = ans[parent[i]] + plus[i]
    return ans[1:]


if __name__ == '__main__':
    N, Q = map(int, input().split())
    a = [0] * (N-1)
    b = [0] * (N-1)
    for idx in range(N-1):
        a[idx], b[idx] = map(int, input().split())
    p = [0] * Q
    x = [0] * Q
    for idx in range(Q):
        p[idx], x[idx] = map(int, input().split())

    ans = solve(N, Q, a, b, p, x)

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

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

ab_list = [list(map(int, input().split())) for i in range(n-1)]
ab_list = sorted(ab_list)

ans = [0 for i in range(n)]

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

for ab in ab_list:
    ans[ab[1]-1] += ans[ab[0]-1]

print(*ans)

Solution (Python):
def main():
    N, Q = [int(x) for x in input().split()]
    edges = [[] for x in range(N)]
    cnt = [0] * N
    for i in range(N-1):
        a, b = [int(x) for x in input().split()]
        edges[a - 1].append(b - 1)
    
    
    for i in range(Q):
        p, x = [int(y) for y in input().split()]
        cnt[p - 1] += x
    

    stack = [0]
    while len(stack) > 0:
        node = stack.pop()
        for t in edges[node]:
            cnt[t] += cnt[node]
            stack.append(t)
        
    print(" ".join(map(str, cnt)))
    return


if __name__ == "__main__":
    main()

Solution (Python):
N, Q = map(int, input().split())
childrens = [[] for i in range(0, N)]
for i in range(0, N-1):
    a, b = map(int, input().split())
    childrens[a-1].append(b-1)
results = [0 for i in range(0, N)]
for i in range(0, Q):
    p, x = map(int, input().split())
    results[p-1] += x

for i in range(0, N):
    for child in childrens[i]:
        results[child] += results[i]

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

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


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

add_list = []
for i in range(q):
    p = list(map(int,input().split()))
    add_list.append(p)



ans = [0] * n
edge_list = [-1] * n

for e in edge:
    edge_list[e[1]-1] = e[0]-1

add_value = [0]*n
for v in add_list:
    add_value[v[0]-1] += v[1]





dp = [-1] * n
def get_value(i):
    if(dp[i]!=-1):
        return dp[i]
    tmp = add_value[i]
    if edge_list[i] != -1:
        tmp += get_value(edge_list[i])
    dp[i] = tmp
    return dp[i]

for i in range(n):
    ans[i] = get_value(i)
    

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

Solution (Python):
N, Q = map(int,input().split())
nord = [set() for i in range(N)]

for i in range(N-1):
    a, b = map(int,input().split())
    nord[b-1].add(a-1)
    
nord_num = [0 for i in range(N)]
for i in range(Q):
    p, x = map(int,input().split())
    nord_num[p-1] = nord_num[p-1] + x
    
for i in range(N):
    for j in nord[i]:
        nord_num[i] = nord_num[j] + nord_num[i]
        
count = 1
for i in nord_num:
    if count == N:
        print(i)
    else:
        print(i, end=" ")
        count += 1

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

tree = {i:[] for i in range(1,N+1)}


vals = [0]*(N+1)


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

for i in [0]*Q:
	p,x = map(int,input().split(' '))
	vals[p] += x

ans = [0]*(N+1)

ans[1] = vals[1]

dq = deque([1])
while(dq):
	n = dq.popleft()
	for t in tree[n]:
		ans[t] = vals[t]+ans[n]
		dq.append(t)

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

Solution (Python):
import sys

sys.setrecursionlimit(10**6 + 1000)


words = lambda t : list(map(t, input().split()))
n,q = words(int)
par = [0] * n
for i in range(n-1):
    a,b = words(int)
    par[b-1] = a-1

points = [0] * n
for i in range(q):
    p,x = words(int)
    points[p-1] += x

ans = [0] * n
ans[0] = points[0]
for i in range(1,n):
    ans[i] = ans[par[i]] + points[i]


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

Solution (Python):


def main():
    N, Q = map(int, input().split())
    V = [[] for _ in range(N)]

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

    P = [None]*N
    S = [0]
    P[0] = -1

    while len(S) > 0:
        parntNode = S.pop()
        for childNode in V[parntNode]:
            if P[childNode] == None:
                P[childNode] = parntNode
                S.append(childNode)


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

    Y = [None]*N
    Y[0] = X[0]
    
    for i in range(1, N):
        if Y[i] != None:
            continue

        T = []
        node = i

        while Y[node] == None:
            T.append(node)
            node = P[node]

        s = Y[node]

        for j in reversed(T):
            s += X[j]
            Y[j] = s
        
    print(' '.join(map(str, Y)))
    

if __name__ == '__main__':
    main()

Solution (Python):

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

ab = []

link = {} 

for i in range(N-1):

    a,b = map(int,input().split())

    if a-1 not in link : link[a-1] = []
    if b-1 not in link : link[b-1] = []


    link[a-1].append(b-1)
    link[b-1].append(a-1)



plus = [0] * N

for i in range(Q):

    p,x = map(int,input().split())

    plus[p-1] += x


Q = []

ans = [0] * N
end = [0] * N
p = [-1] * N
p[0] = 0

nowq = 0
Q.append(0)

end[0] = 1


while nowq < N:

    nowpo = Q[nowq]
    
    ans[ nowpo ] = ans[p[ nowpo ]] + plus[nowpo]

    for i in link[nowpo]:

        if end[i] == 0:

            Q.append(i)
            p[i] = nowpo
            


            end[i] = 1


    nowq += 1



for i in ans:
    print (i)

Solution (Python):
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)

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

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

F = [0]
ans[0] = W[0]
while F:
    v = F.pop()
    for c in G[v]:
        ans[c] = ans[v] + W[c]
        F.append(c)

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

Solution (Python):
def main():
    n,q = map(int,input().split())
    tree = [1 for _ in range(n+1)]
    for _ in range(n-1):
        a,b = map(int,input().split())
        tree[b]=a

    ls_q = [[int(x) for x in input().split()] for _ in range(q)]

    ans = [0 for _ in range(n+1)]
    for i,j in ls_q:
        ans[i] += j

    for i in range(2,n+1):
        ans[i] += ans[tree[i]]

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

if __name__ =="__main__":
    main()

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

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

n, q = getList()

vtxs = [[] for i in range(n)]
already = [0 for i in range(n)]

for i in range(n - 1):
    a, b = getList()
    vtxs[a-1].append(b -1)
    vtxs[b-1].append(a - 1)

count = [0 for i in range(n)]
for i in range(q):
    x, v = getList()
    count[x-1] += v

ans = [0 for i in range(n)]

def rep(vx, val):
    
    tgt = vtxs[vx]
    already[vx] = 1
    for tg in tgt:
        if already[tg] == 0:
            app = count[tg]
            ans[tg] = val+app
            rep(tg, val+app)

rep(0, count[0])

ans[0] = count[0]
print(*ans)

Solution (Python):
def main():
        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)]
        ab.sort()
        
        plus = [0 for _ in range(N)]

        for p,x in px:
                plus[p - 1] += x 

        for a,b in ab:
                plus[b - 1] += plus[a - 1]
        for p in plus:
                print(p)

if __name__ == '__main__':
        main()

Solution (Python):
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
    N, Q = MAP()
    AB = sorted(MATINT(N-1))
    plus = [0] * N
    ans = [0] * N
    for i in range(Q):
        P, X = MAP()
        plus[P-1] += X
    for ab in AB:
        plus[ab[1]-1] += plus[ab[0]-1]
    print(*plus)

if __name__ == '__main__':
    main()

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

e = [[] for _ in range(n)]
v = [0]*n

for _ in range(n-1):
  x = list(map(int, input().split()))
  e[x[0]-1].append(x[1]-1)  

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

for i, x in enumerate(e):
    for j in x:
        v[j] += v[i]
        
for x in v:
  print(x, end=" ")

Solution (Python):
n,q=map(int,input().split())
ab=[list(map(lambda x:int(x)-1,input().split())) for _ in [0]*(n-1)]
pq=[list(map(int,input().split())) for _ in [0]*q]
for i in range(q):
    pq[i][0]-=1

graph=[[] for _ in [0]*n]
[graph[a].append(b) for a,b in ab if a<b]
[graph[b].append(a) for a,b in ab if a>b]

plus=[0 for _ in [0]*n]
for i,j in pq:
    plus[i]+=j

pos=[]
ans=[0 for _ in [0]*n]
temp=0
q=[0]
memo=[False for _ in [0]*n]

while q:
    i=q.pop(-1)
    if memo[i]==False:
        pos.append(i)
        temp+=plus[i]
        ans[i]=temp
    memo[i]=True

    while graph[i]:
        j=graph[i].pop(-1)
        if memo[j]==False:
            q.append(j)
            break
    else:
        temp-=plus[pos[-1]]
        pos.pop(-1)
        if pos!=[]:
            q.append(pos[-1])
print(*ans)

Solution (Python):
n,q=map(int,input().split())
ab=[]
for i in range(n-1):
	x,y=map(int,input().split())
	ab.append([x,y])
ab.sort()
ans=[0]*n
cnt=[0]*n
for i in range(q):
	x,y=map(int,input().split())
	ans[x-1]+=y
for i in range(n-1):
	ans[ab[i][1]-1]+=ans[ab[i][0]-1]
print(*ans)

Solution (Python):
from collections import defaultdict


def p_d():
    N, Q = map(int, input().split())
    l = defaultdict(list)
    ans = [0] * N

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

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

    for i in range(N):
        for j in l[i]:
            ans[j - 1] += ans[i - 1]

    print(*ans)


if __name__ == '__main__':
    p_d()

Solution (Python):
n, q = [int(i) for i in input().split()]
ab_pairs = []
for _ in range(n-1):
    ab_pairs.append([int(i) - 1 for i in input().split()])

qx_pairs = []
for _ in range(q):
    qx_pairs.append([int(i) for i in input().split()])

ab_pairs_sorted = sorted(ab_pairs, key= lambda ab_pair: ab_pair[1])

point_list = [0] * n
for qx_pair in qx_pairs:
    index = qx_pair[0] - 1
    x = qx_pair[1]
    point_list[index] += x

for ab_pair in ab_pairs_sorted:
    a = ab_pair[0]
    b = ab_pair[1]
    point_list[b] += point_list[a]


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