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):
from collections import defaultdict
from collections import deque
n,q = map(int,input().split())
d = defaultdict(list)
dp = [0] * n
ep = [0] * n
for i in range(n-1):
    ta,tb = map(int,input().split())
    d[ta-1].append(tb-1)
    d[tb-1].append(ta-1)
for i in range(q):
    ta,tb = map(int,input().split())
    dp[ta-1] +=tb
ep[0]+=1
l = deque()
l.append(0)
used = [False] * n
while l:
    t = l.pop()
    used[t] = True
    for i in d[t]:
        if not used[i]:
            dp[i] += dp[t]
            l.append(i)
print(*dp)

Solution (Python):
from collections import defaultdict
from collections import deque
n,q = map(int,input().split())
d = defaultdict(list)
dp = [0] * n
ep = [0] * n
for i in range(n-1):
    ta,tb = map(int,input().split())
    d[ta-1].append(tb-1)
    d[tb-1].append(ta-1)
for i in range(q):
    ta,tb = map(int,input().split())
    dp[ta-1] +=tb
ep[0]+=1
l = deque()
l.append(0)
used = [False] * n
while l:
    t = l.pop()
    used[t] = True
    for i in d[t]:
        if not used[i]:
            dp[i] += dp[t]
            l.append(i)
print(*dp)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,q = inpl()
g = [[]for i in range(n)]
c = [0 for i in range(n)]
seen = [False] * n
for i in range(n-1):
    a,b = inpl()
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for i in range(q):
    a,b = inpl()
    c[a-1] += b
def dfs(node):
    for v in g[node]:
        if seen[v]:
            continue
        c[v] += c[node]
        seen[v] = True
        dfs(v) 
seen[0] = True
dfs(0)
for i in c:
    print(i)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,q = inpl()
g = [[]for i in range(n)]
c = [0 for i in range(n)]
seen = set()
for i in range(n-1):
    a,b = inpl()
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for i in range(q):
    a,b = inpl()
    c[a-1] += b
def dfs(node):
    for v in g[node]:
        if v in seen:
            continue
        c[v] += c[node]
        seen.add(v)
        dfs(v) 

seen.add(0)
dfs(0)
for i in c:
    print(i)

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

cost = [0 for _ in range(n)]
for _ in range(q):
    pos, point = map(int, input().split())
    cost[pos-1] += point

memo = set([0])
stack = [0]

while len(stack) > 0:
    cur_node = stack.pop()
    cur_cost = cost[cur_node]

    for node in tree[cur_node]:
        if node not in memo:
            memo.add(node)
            cost[node] += cur_cost
            stack.append(node)

print(*cost)

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

def main():
    V = [0]*(N+1)
    for p,x in PX:
        V[p] += x
    stack = [1]
    gone = [False]*(N+1)
    gone[1] = True
    while len(stack) > 0:
        node = stack.pop(-1)
        for e in E[node]:
            if not(gone[e]):
                stack.append(e)
                V[e] += V[node]
                gone[e] = True
    print(*V[1:],sep =" ")
    return 

if __name__ == "__main__":
    main()

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

def main():
    V = [0]*(N+1)
    for p,x in PX:
        V[p] += x
    stack = [1]
    gone = [False]*(N+1)
    gone[1] = True
    while len(stack) > 0:
        node = stack.pop(-1)
        for e in E[node]:
            if not(gone[e]):
                stack.append(e)
                V[e] += V[node]
                gone[e] = True
    print(*V[1:],sep =" ")
    return 

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y


N,Q=LI()
TR=[[] for i in range(N+1)]
TR_score = [0] * (N+1)
RES = [-1]*(N+1)

for i in range(N-1):
    i,j=LI()
    TR[i].append(j)
    TR[j].append(i)

for i in range(Q):
    p, x = LI()
    TR_score[p] += x

ST = [(1, TR_score[1])]
while ST:
    i, score = ST.pop()
    RES[i] = score
    for j in TR[i]:
        if RES[j] == -1:
            ST.append((j, score + TR_score[j]))

for value in RES[1:-1]: print(value, end=" ")
print(RES[-1])

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


P = [-1]*N
Q = [0]
X = []
count = 0
while Q:
    v = Q.pop()
    X.append(v)
    if v != 0:
        C[v] += C[P[v]]
    for u in G[v]:
        if u == P[v]:
            continue
        P[u] = v
        Q.append(u)

C = [str(c) for c in C]
print(" ".join(C))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(1000000)
N,Q = map(int,readline().split())
abpx = list(map(int,read().split()))
ab = iter(abpx[:N+N-2])
px = iter(abpx[N+N-2:])
dic = {}
dic2=[0]*(N+1)
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for a,b in zip(ab,ab):
    dic[a].append(b)
    dic[b].append(a)
for p,x in zip(px,px):
    dic2[p] += x
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node-1] = nowval
    tg_list = dic[now_node][:]
    for elem in tg_list:
        dic[elem].remove(now_node)
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
N,Q = map(int,input().split())
ab = [ tuple(map(int,input().split())) for i in range(N-1) ]
px = [ tuple(map(int,input().split())) for i in range(Q) ]
dic = {}
dic2=[0]*(N+1)
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for i in range(N-1):
    tmp1 = ab[i][0]
    tmp2 = ab[i][1]
    dic[tmp1].append(tmp2)
    dic[tmp2].append(tmp1)
ab=0
for i in range(Q):
    dic2[px[i][0]] += px[i][1]
px=0
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node-1] = nowval
    tg_list = dic[now_node][:]
    for elem in tg_list:
        dic[elem].remove(now_node)
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

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

eda=[[] for i in range(N+1)]

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


stack=[[1,eda[1]]]
v=set()
v.add(1)
while stack:
    while stack[-1][1]:
        a=stack[-1][1].pop()
        if not a in v:
            v.add(a)
            pt[a]+=pt[stack[-1][0]]
            stack.append([a,eda[a]])
    stack.pop()
print(" ".join(list(map(str,pt[1:]))))

Solution (Python):
from collections import deque

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

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

visited = set()
visited.add(0)

dq = deque()
dq.append(0)

while dq:
    x = dq.popleft()
    for j in Adj[x]:
        if j not in visited:
            visited.add(j)
            dq.append(j)
            Counter[j] += Counter[x]
ans = ''
for i in range(n-1):
    ans += str(Counter[i]) + ' '
ans += str(Counter[n-1])
print(ans)

Solution (Python):









n, q = map(int, input().split())
graph = [[] for _ in range(n)]
points = [0 for _ in range(n)]

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

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

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

Solution (Python):
from collections import deque

def main():
 n,q=map(int,input().split())

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

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

 seen=set([0])

 dq=deque()
 dq.append(0)

 while dq:
  x=dq.popleft()
  for i in tree[x]:

   if i not in seen:
    seen.add(i)
    dq.append(i)
    cnt[i]+=cnt[x]

 print(*cnt)

if __name__ == "__main__":
  main()

Solution (Python):
from collections import deque

def main():
 n,q=map(int,input().split())

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

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

 seen=set([0])

 dq=deque()
 dq.append(0)

 while dq:
  x=dq.popleft()
  for i in tree[x]:

   if i not in seen:
    seen.add(i)
    dq.append(i)
    cnt[i]+=cnt[x]

 print(*cnt)

if __name__ == "__main__":
  main()

Solution (Python):
from collections import deque

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

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

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

seen=set([0])

dq=deque()
dq.append(0)

while dq:
 x=dq.popleft()
 for i in tree[x]:

  if i not in seen:
   seen.add(i)
   dq.append(i)
   cnt[i]+=cnt[x]

print(*cnt)

Solution (Python):
from collections import deque

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

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

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

cnt=[-1]*n
cnt[0]=t_cnt[0]

seen=set([0])

dq=deque()
dq.append(0)

while dq:
 x=dq.popleft()
 ref_v=cnt[x]
 for i in tree[x]:

  if i not in seen:
   seen.add(i)
   dq.append(i)
   cnt[i]=ref_v+t_cnt[i]

print(*cnt)

Solution (Python):
from collections import deque

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

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

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

cnt=[-1]*n
cnt[0]=t_cnt[0]

seen=set([0])

dq=deque()
dq.append(0)

while dq:
 x=dq.popleft()
 for i in tree[x]:




   if i not in seen:
    seen.add(i)
    dq.append(i)
    cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Solution (Python):
from collections import deque

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

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

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

cnt=[-1]*n
cnt[0]=t_cnt[0]

seen=set([0])

dq=deque()
dq.append(0)

while dq:
 x=dq.popleft()
 for i in tree[x]:

   if i in seen:
    continue
   else:
    seen.add(i)
    dq.append(i)
    cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

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

sys.setrecursionlimit(10**6)


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

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

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

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

def dfs(now,prev=-1):
    for next in A[now]:
        if next==prev:continue
        else:
            c[next]+=c[now]
            dfs(next,now)
dfs(0)
print(*c)

Solution (Python):


import collections
N, Q = map(int, input().split())
g = [set() for i in range(N)]

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

parent = [-1 for i in range(N)]
parent[0] = 0

q = collections.deque()
q.append(0)

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

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

while(q):
    x = q.popleft()
    for y in g[x]:
        if parent[y] == -1:
            q.append(y)
            parent[y] = x
            point[y] += point[x]

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

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
def input():
    return sys.stdin.readline()


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

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

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

def dfs(now, prev):
    for vertex in GRAPH[now]:
        if vertex == prev:
            continue
        else:
            next = vertex
        COUNTS[next] += COUNTS[now]
        dfs(next, now)
dfs(1, 0)

answer = " ".join(map(str, COUNTS[1:]))
print(answer)

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

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) 
  tree_index[b].append(a)
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_index[cur]:
      if chl == par: 
        continue
      ans[chl] += ans[cur] 
      dfs(chl, cur)
dfs(0, -1)
print(*ans)

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

def input():
  return sys.stdin.readline()[:-1]

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

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

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

counter=[0]*(N+1)
for i in range(Q):
  counter[px[i][0]]+=px[i][1]
  
def f(num, prenum=-1, p=0):
  counter[num]+=p
  for postnum in link[num]:
    if postnum==prenum:
      continue
    f(postnum, num, counter[num])
    
f(1)

print(*counter[1:])

Solution (Python):
import sys
from collections import deque
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**7)
n,q=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]
px=[list(map(int,input().split())) for i in range(q)]







ne=[[] for i in range(n+1)]
for i in range(n-1):
    ne[ab[i][0]].append(ab[i][1])
    ne[ab[i][1]].append(ab[i][0])

cnt=[0]*(n+1)
for i in range(q):
    cnt[px[i][0]]+=px[i][1]
par=[-1]*(n+1)
stack=deque([1])
while(stack):
    v=stack.pop()
    for i in range(len(ne[v])):
        if ne[v][i]!=par[v]:
            cnt[ne[v][i]]+=cnt[v]
            par[ne[v][i]]=v
            stack.append(ne[v][i])
cnt.remove(0)
print(' '.join(map(str,cnt)))

Solution (Python):





import sys
input=sys.stdin.readline
sys.setrecursionlimit(1000000)

from collections import deque


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

tree=[[]*(N+1) for i in range(N+1)]
point=[0]*(N+1)

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


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

while d:
    node,parent=d.popleft()
    point[node]+=point[parent]
    lis=tree[node]
    for child in lis:
        if child==parent:
            continue
        d.append([child,node])
    

print(*point[1:])

Solution (Python):



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
    



queue = [(1,0)]
while queue:
    x, parent = queue.pop()
    value[x] += value[parent]
    
    for y in graph[x]:
        if y == parent:
            continue
        
        queue.append((y, x))

answer = " ".join(map(str, value[1:]))
print(answer)

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

N,Q=map(int,input().split())
c=[0]*N

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

for i in range(N-1):
    a,b =map(int,input().split())
    a-=1
    b-=1
    to[a].append(b)
    to[b].append(a)
    
    
for i in range(Q):
    p,x =map(int,input().split())
    p-=1
    c[p]+=x
    
q=queue.Queue()
q.put((0,-1))


while not q.empty():
    v,prev=q.get()
    for i in range(len(to[v])):
        v_next=to[v][i]
        if v_next!=prev:
            q.put((v_next,v))
            c[v_next]+=c[v]

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from collections import deque
 
N,Q = map(int,readline().split())
ki = []
for i in range(N-1):
    ki.append(list(map(int,readline().split())))
co = []
for i in range(Q):
    co.append(list(map(int,readline().split())))
 
ki.sort()
 
kid = deque(ki)
ver = []
check = []
for i in range(N):
    ver.append(0)
    check.append(False)
 
for c in co:
    ver[c[0]-1] += c[1]
 
check[0] = True
 
kid2 = deque()
 
while len(kid) != 0:
    k = kid.popleft()
    if check[k[0]-1] == True:
        ver[k[1]-1] += ver[k[0]-1]
        check[k[1]-1] = True
    elif check[k[1]-1] == True:
        ver[k[0]-1] += ver[k[1]-1]
        check[k[0]-1] = True
    else:
        kid2.append(k)
 
while len(kid2) != 0:
    k = kid2.pop()
    if check[k[0]-1] == True:
        ver[k[1]-1] += ver[k[0]-1]
        check[k[1]-1] = True
    elif check[k[1]-1] == True:
        ver[k[0]-1] += ver[k[1]-1]
        check[k[0]-1] = True
    else:
        kid2.appendleft(k)
 
 
print(' '.join(map(str,ver)))

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

from collections import deque

N,Q = map(int,readline().split())
ki = []
for i in range(N-1):
    ki.append(list(map(int,readline().split())))
co = []
for i in range(Q):
    co.append(list(map(int,readline().split())))
 
ki.sort(key=lambda x: x[1])

kid = deque(ki)
ver = []
check = []
for i in range(N):
    ver.append(0)
    check.append(False)
 
for c in co:
    ver[c[0]-1] += c[1]

check[0] = True

kid2 = deque()

while len(kid) != 0:
    k = kid.popleft()
    if check[k[0]-1] == True:
        ver[k[1]-1] += ver[k[0]-1]
        check[k[1]-1] = True
    elif check[k[1]-1] == True:
        ver[k[0]-1] += ver[k[1]-1]
        check[k[0]-1] = True
    else:
        kid2.append(k)

while len(kid2) != 0:
    k = kid2.pop()
    if check[k[0]-1] == True:
        ver[k[1]-1] += ver[k[0]-1]
        check[k[1]-1] = True
    elif check[k[1]-1] == True:
        ver[k[0]-1] += ver[k[1]-1]
        check[k[0]-1] = True
    else:
        kid2.appendleft(k)


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

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

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

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

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

    
    print(*(imos[begin[v]] for v in range(n)))
resolve()

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


from collections import deque

def main():

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

  li=[[] for i in range(n+1)]
  ans=[0]*(n+1)

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

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

  Q=deque([1])
  sumi=set([1])

  while Q:
  
    now=Q.pop()
    for i in li[now]:
      if i in sumi:
        continue
      else:
        sumi.add(i)
        Q.append(i)
        ans[i]+=ans[now]
  print(*ans[1:])
      
if __name__ == "__main__":
  main()

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


from collections import deque

def main():

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

  li=[[] for i in range(n+1)]
  ans=[0]*(n+1)

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

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

  Q=deque([1])
  sumi=set([1])

  while Q:
  
    now=Q.pop()
    for i in li[now]:
      if i in sumi:
        continue
      else:
        sumi.add(i)
        Q.append(i)
        ans[i]+=ans[now]
  print(*ans[1:])
      
if __name__ == "__main__":
  main()

Solution (Python):
from collections import deque

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

li=[[] for i in range(n+1)]
ans=[0]*(n+1)

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

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

Q=deque([1])
sumi=set([1])

while Q:

  now=Q.pop()
  for i in li[now]:
    if i in sumi:
      continue
    else:
      sumi.add(i)
      Q.append(i)
      ans[i]+=ans[now]
print(*ans[1:])

Solution (Python):
from collections import deque

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

li=[[] for i in range(n+1)]
ans=[0]*(n+1)

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

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

Q=deque([1])
sumi=set([1])

while Q:

  now=Q.pop()
  for i in li[now]:
    if i in sumi:
      continue
    else:
      sumi.add(i)
      Q.append(i)
      ans[i]+=ans[now]
print(*ans[1:])

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

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

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) 
  tree_index[b].append(a)
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_index[cur]:
      if chl == par: 
        continue
      ans[chl] += ans[cur] 
      dfs(chl, cur)
dfs(0, 0)
print(*ans)

Solution (Python):
from collections import deque 
n,q=map(int,input().split())

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


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





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


while que: 
	i=que.popleft()
	if color[i]==0:
		for edge in edges[i]:
			if color[edge]==0: 
				nodes[edge]+=nodes[i]
				que.append(edge)
		color[i]=1


for node in nodes:
	print(node,end=' ')

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

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

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

val = [0] * n
for p, w in weights:
    val[p-1] += w

stack, parents = [0], [0] * n
while stack:
    x = stack.pop()
    for y in tree[x]:
        if y == parents[x]:
            continue
        parents[y] = x
        stack.append(y)
        val[y] += val[x]

print(*val)

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())
ABPX = list(map(int,read().split()))
AB = iter(ABPX[:N+N-2])
PX = iter(ABPX[N+N-2:])

graph = [[] for _ in range(N+1)]
for a,b in zip(AB,AB):
    graph[a].append(b)
    graph[b].append(a)

val = [0] * (N+1)
for p,x in zip(PX,PX):
    val[p] += x

stack = [1]

parent = [0] * (N+1)
while stack:
    x = stack.pop()
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
        val[y] += val[x]

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

Solution (Python):
from collections import deque

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

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

def bfs(i):
    s = -1
    q = deque([])
    q.append((i-1,s))
    while q:
        
        num,bef = q.pop()
        for j in E[num]:
            if j == bef:
                continue
            else:
                score[j] += score[num]
                q.append((j,num))
            
    return score
print(*bfs(1))

Solution (Python):
from collections import deque

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

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

def bfs(i):
    s = -1
    q = deque([])
    q.append((i-1,s))
    while q:
        
        num,bef = q.popleft()
        for j in E[num]:
            if j == bef:
                continue
            else:
                score[j] += score[num]
                q.append((j,num))
            
    return score
print(*bfs(1))

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


N, Q = map(int, input().split())
G = [[] for _ in range(N)]
P = [0 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)

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


def dfs(current, parent):
    for child in G[current]:
        if child == parent:
            continue
        P[child] += P[current]
        dfs(child, current)


dfs(0, -1)

print(' '.join([str(p) for p in P]))

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

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

v = [[] for i in range(N)]
score = [0]*N

def dfs(cur,par):
  for chi in v[cur]:
    if chi == par:
      continue
    score[chi] += score[cur]
    dfs(chi, cur)
      
for i in range(N-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  v[a].append(b)
  v[b].append(a)

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

dfs(0,-1)
score_int=[str(a) for a in score]
output= ' '.join(score_int)
print(output)

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

from collections import defaultdict
dic = defaultdict(int)
for _ in range(q):
   p,x = map(int,input().split())
   dic[p-1] += x

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

used = [0 for i in range(n)]
used[0] = 1
quu = [0]
while quu:
   k = quu.pop()
   for l in m[k]:
      if used[l] == 0:
         used[l] = 1
         quu.append(l)
         ans[l] = ans[k] + dic[l]

for i in ans:
   print(i)