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
f=lambda:map(int,sys.stdin.readline().split())
n,q=f()
g=[[] for _ in range(n)]
for i in range(n-1):
  a,b=f()
  g[a-1]+=[b-1]
  g[b-1]+=[a-1]
c=[0]*n
for i in range(q):
  v,x=f()
  c[v-1]+=x
b=[0]+[1]*(n-1)
s=[0]
while s:
  t=s.pop()
  for i in g[t]:
    if b[i]:
      c[i]+=c[t]
      s+=[i]
      b[i]=0
print(*c)

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
que = deque([0])
used = [0]*N
used[0] = 1
while que:
  q = que.popleft()
  for p in AB[q]:
    if used[p] == 1:
      continue
    count[p] += count[q]
    used[p] = 1
    que.append(p)
print(*count)

Solution (Python):
N,Q=map(int,input().split())
edge=[[] for _ in range(N)]
cnt=[0]*N
for _ in range(N-1):
  a,b=map(int,input().split())
  edge[a-1]+=(b-1),
  edge[b-1]+=(a-1),
for _ in range(Q):
  p,x=map(int,input().split())
  cnt[p-1]+=x
done=[False]*N
done[0]=True
stack=[0]
while stack:
  par=stack.pop()
  for child in edge[par]:
    if not done[child]:
      done[child]=True
      cnt[child]+=cnt[par]
      stack.append(child)
print(*cnt)

Solution (Python):
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

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

    Graph = tuple(set() for _ in range(N))
    for _ in range(N - 1):
        a, b = (int(x) - 1 for x in input().split())
        Graph[a].add(b)
        Graph[b].add(a)

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

    root = 0
    visited = [False] * N
    visited[root] = True

    dq = deque()
    dq.append(root)

    while dq:
        v = dq.pop()
        x = ctr[v]
        for u in Graph[v]:
            if visited[u]:
                continue
            visited[u] = True
            ctr[u] += x
            dq.append(u)

    print(*ctr)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys

    sys.setrecursionlimit(10 ** 7)

    input = sys.stdin.readline

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

    Graph = tuple(set() for _ in range(N))
    for _ in range(N - 1):
        a, b = (int(x) - 1 for x in input().split())
        Graph[a].add(b)
        Graph[b].add(a)

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

    root = 0
    visited = [False] * N
    visited[root] = True

    def dfs(v):
        x = ctr[v]
        for u in Graph[v]:
            if visited[u]:
                continue
            visited[u] = True
            ctr[u] += x
            dfs(u)

    dfs(root)

    print(*ctr)


if __name__ == '__main__':
    main()

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

def resolve():
    def dfs(v, parent):
        for nv in G[v]:
            if nv == parent:
                continue
            value[nv] += value[v]
            dfs(nv, v)

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

    value = [0]*(N+1)
    for _ in range(Q):
        v, score = map(int, inp().split())
        value[v] += score

    dfs(1, 0)
    return print(*value[1:])

if __name__ == "__main__":
    resolve()

Solution (Python):
n, q = map(int, input().split())
node = [[] for _ in range(n)]
ans = [0] * n

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

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

not_yet = [True] * n
not_yet[0] = False
stack = [0]
while stack:
    parent = stack.pop()
    for child in node[parent]:
        if not_yet[child]:
            ans[child] += ans[parent]
            not_yet[child] = False
            stack.append(child)

print(*ans)

Solution (Python):
N,Q=map(int,input().split())
edge=[[] for _ in range(N)]
cnt=[0]*N
for _ in range(N-1):
  a,b=map(int,input().split())
  edge[a-1]+=(b-1),
  edge[b-1]+=(a-1),
for _ in range(Q):
  p,x=map(int,input().split())
  cnt[p-1]+=x
done=[False]*N
done[0]=True
stack=[0]
while stack:
  par=stack.pop()
  for child in edge[par]:
    if not done[child]:
      done[child]=True
      cnt[child]+=cnt[par]
      stack.append(child)
print(*cnt)

Solution (Python):
n,q=map(int,input().split())
edge=[[] for _ in range(n)] 
cnt=[0]*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):
  p,x=map(int,input().split())
  p-=1
  cnt[p]+=x
done=[False]*n 
par=0 
done[par]=True
stack=[par]
while stack:
  par=stack.pop()
  for chi in edge[par]:
    if done[chi]:
      None
    else:
      done[chi]=True
      cnt[chi]+=cnt[par]
      stack.append(chi)
    
print(*cnt)

Solution (Python):
n,q=map(int,input().split())
edge=[[] for _ in range(n)] 
cnt=[0]*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):
  p,x=map(int,input().split())
  p-=1
  cnt[p]+=x
done=[False]*n 
par=0 
done[par]=True
stack=[par]
while stack:
  par=stack.pop()
  for chi in edge[par]:
    if done[chi]:
      None
    else:
      done[chi]=True
      cnt[chi]+=cnt[par]
      stack.append(chi)
    
print(*cnt)

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10**9)


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

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

    visited = [False]*N
    visited[0] = True
    q = deque([0])
    while q:
        v = q.pop()
        for n in edges[v]:
            if visited[n]:
                continue
            visited[n] = True
            cnt[n] += cnt[v]
            q.append(n)
    print(*cnt)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
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)

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


def bfs(tree, start, n, query):
    visited = [False] * n
    visited[start] = True
    que = deque([start])
    while que:
        node = que.popleft()
        cost = query[node]
        for n in tree[node]:
            if not visited[n]:
                query[n] += cost
                visited[n] = True
                que.append(n)

bfs(tree, 0, n, query)
print(*query)

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

def resolve():
    N, Q = map(int, inp().split())

    AB = [[int(x) for x in inp().split()] for _ in range(N - 1)]
    PX = [[int(x) for x in inp().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):
        for i in graph[v]:
            if i == parent:
                continue
            value[i] += value[v]
            dfs(i, v)

    dfs(1, 0)
    ans = ' '.join(map(str, value[1:]))
    return print(ans)

if __name__ == "__main__":
    resolve()

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

edges = [[] for _ in range(N)]
for _ in range(N - 1):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)

ans = [0] * (N + 1)

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

st = [(0, N)]
while st:
    now, pr = st.pop()

    ans[now] += ans[pr]
    for to in edges[now]:
        if to == pr:
            continue
        st.append((to, now))
print(*ans[:N])

Solution (Python):



import sys
sys.setrecursionlimit(10**6)

input = sys.stdin.readline
N, Q = map(int, input().split())

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

counter = [0]*N

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

visited = [False]*N


def dfs(v=0, x=0):
    visited[v] = True
    counter[v] += x
    for u in G[v]:
        if visited[u]:
            continue
        dfs(u, counter[v])


dfs()
print(*counter)

Solution (Python):
def examA():
    def calc_L(x1,y1,x2,y2):
        return ((x2-x1)**2 + (y2-y1)**2)**0.5
    N = I()
    X = [LI()for _ in range(N)]
    ans = 0
    for x in itertools.permutations(X):
        cur = 0
        for i in range(1,N):
            cur += calc_L(x[i-1][0],x[i-1][1],x[i][0],x[i][1])
        
        ans += cur
    for i in range(1,N+1):
        ans /= i
    print(ans)
    return

def examB():
    def judge(n):
        sn = 0
        for i in n:
            sn += int(i)
        if int(n)%sn==0:
            return "Yes"
        return "No"
    N = SI()
    ans = judge(N)
    print(ans)
    return

def examC():
    N, K = LI()
    P = LI()
    ans = 0
    S = [0]*(N+1)
    for i in range(N):
        S[i+1] = S[i]+(P[i]+1)/2
    for i in range(N-K+1):
        cur = S[i+K] - S[i]
        if cur>ans:
            ans = cur
    print(ans)
    return

def examD():
    def bfs(n,V,s,W):
        visited = [False]*n
        que = deque()
        que.append(s)
        while(que):
            now = que.pop()
            visited[now] = True
            for ne in V[now]:
                if visited[ne]:
                    continue
                W[ne] += W[now]
                que.append(ne)
        return W
    N, Q = LI()
    V = [[]for _ in range(N)]
    for _ in range(N-1):
        a, b = LI()
        a -= 1; b -= 1
        V[a].append(b)
        V[b].append(a)
    W = [0] * N
    for _ in range(Q):
        p, x = LI()
        W[p-1] += x
    ans = bfs(N,V,0,W)
    print(" ".join(map(str,ans)))
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examD()

"""

"""

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

work_q = deque([1])
used = set()
used.add(1)
ans[1] = cost[1]
cnt = 0
while work_q:
    here = work_q.popleft()
    cnt = ans[here]
    for i in edge[here]:
        if i not in used:
            work_q.append(i)
            used.add(i)
            ans[i] = cnt + cost[i]
print(*ans[1:])

Solution (Python):
n,q=map(int,input().split())
G=[[] for i 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)

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

from collections import deque
visited=[False]*n
q=deque()
q.append(0)
while q:
    cur=q.popleft()
    visited[cur]=True
    for nx in G[cur]:
        if visited[nx]:continue
        c[nx]+=c[cur]
        q.append(nx)
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)

d = collections.defaultdict(int)

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

q = collections.deque()

r = [-1]*N
r[0] = d[0]
q.append(0)
while(q):
    x = q.popleft()
    c = r[x]
    for y in G[x]:
        if r[y] < 0:
            r[y] = c+d[y]
            q.append(y)

print(*r)

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

n,q = map(int,input().split())
from collections import defaultdict
dic = defaultdict(list)
for _ in range(n-1):
    a,b = map(int,input().split())
    dic[a-1].append(b-1)
    dic[b-1].append(a-1)

from collections import defaultdict
dicp = defaultdict(int)

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


ansl = [0 for i in range(n)]
used = [0 for i in range(n)]
def dfs(pos, point):
    point += dicp[pos]
    used[pos] = 1
    ansl[pos] = point
    for p in dic[pos]:
        if used[p] == 0:
            dfs(p, point)

dfs(0, 0)

for a in ansl:
    print(a)

Solution (Python):
n, q = map(int, input().split())
G = [[] for i 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)
C = [0 for i in range(n)]
for j in range(q):
    p, x = map(int, input().split())
    C[p-1] += x
s = 0
Q = [s]
V = [False for i in range(n)]
while Q:
    x = Q.pop()
    V[x] = True
    for y in G[x]:
        if not V[y]:
            Q.append(y)
            C[y] += C[x]
print(*C)

Solution (Python):
from collections import deque

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

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

ans = [-1] * N
que = deque([0])
ans[0] = cnt[0]
while que:
    v = que.popleft()
    for w in edge[v]:
        if ans[w] < 0:
            ans[w] = ans[v] + cnt[w]
            que.append(w)

print(*ans)

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

n,q = inpl()
g = [[] for _ in range(n)]
for i in range(n-1):
    a,b = inpl()
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)
c = [0] * n
for i in range(q):
    a,b = inpl()
    c[a-1] += b
q = deque()
q.append(0)
fin = [False] * n
fin[0] = True
while q:
    v = q.popleft()
    for nv in g[v]:
        if fin[nv]:continue
        c[nv] += c[v]
        fin[nv] = True
        q.append(nv)
print(*c)

Solution (Python):
from collections import deque


def main():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        tree[a].append(b)
        tree[b].append(a)
    counter = [0 for _ in range(n)]
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        counter[p] += x
    q = deque([0])
    checked = [False for _ in range(n)]
    checked[0] = True
    while q:
        now = q.pop()
        for next_v in tree[now]:
            if not checked[next_v]:
                counter[next_v] += counter[now]
                checked[next_v] = True
                q.append(next_v)
    print(" ".join(map(str, counter)))


if __name__ == '__main__':
    main()

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

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

seen = [False]*(n+1)
def dfs(v):
  seen[v] = True
  for nv in T[v]:
    if seen[nv]:
      continue
    dp[nv] += dp[v]
    dfs(nv)
dfs(1)
print(*dp[1:])

Solution (Python):
import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline
def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))



from collections import deque

def main():
	n,q=MI()
	E=[[] for _ in range(n+1)]
	X=[0]*(n+1)

	for _ in range(n-1):
		a,b=MI()
		E[a].append(b)
		E[b].append(a)
	for _ in range(q):
		p,x=MI()
		X[p]+=x
	

	Q=deque([1])

	while Q:
		
		now=Q.popleft()
		
		if E[now]!=[]:
			for i in E[now]:
				if E[i]!=[]:
					Q.append(i)
					X[i]+=X[now]
			E[now]=[]
	print(*X[1:])


if __name__ == "__main__":
	main()

Solution (Python):
import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline
def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))



from collections import deque

def main():
	n,q=MI()
	E=[[] for _ in range(n+1)]
	X=[0]*(n+1)

	for _ in range(n-1):
		a,b=MI()
		E[a].append(b)
		E[b].append(a)
	for _ in range(q):
		p,x=MI()
		X[p]+=x
	

	Q=deque([1])

	while Q:
		
		now=Q.popleft()
		
		if E[now]!=[]:
			for i in E[now]:
				if E[i]!=[]:
					Q.append(i)
					X[i]+=X[now]
			E[now]=[]
	print(*X[1:])


if __name__ == "__main__":
	main()

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

N, Q = map(int, input().split())
ab=[]
for _ in range(N-1):
    ab.append(tuple(map(int, input().split())))
px=[]
for _ in range(Q):
    px.append(tuple(map(int, input().split())))

cnt=[0]*(N+1)
for el in px:
    cnt[el[0]] += el[1]



lst=[[] for _ in range(N+1)]
for el in ab:
    lst[el[0]].append(el[1])
    lst[el[1]].append(el[0])


seen = [False] * (N + 1)
def dfs(n):
    seen[n] = True
    for child in lst[n]:
        if seen[child] == False:
            cnt[child]+=cnt[n]
            dfs(child)

dfs(1)

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

Solution (Python):
N,Q=list(map(int,input().split()))
edge=[[] for _ in range(N)]

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

visit=[0]*N
pts=[0]*N
ans=['']*N

for _ in range(Q):
    p,x=list(map(int,input().split()))

    pts[p-1]+=x

stack=[[0,0]]

while len(stack)>0:
    u,p=stack.pop()
    visit[u]=1
    p=p+pts[u]
    ans[u]=str(p)

    for v in edge[u]:
        if visit[v]==1:
            continue

        stack.append([v,p])

print(' '.join(ans))

Solution (Python):
from collections import deque

def main():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    
    ans = [0]*n
    for _ in range(q):
        p, x = map(int, input().split())
        ans[p-1] += x
    
    not_yet = deque([0])
    already = [False]*n
    already[0] = True
    
    while not_yet:
        key = not_yet.popleft()
        for value in tree[key]:
            if already[value]:
                continue
            already[value] = True
            not_yet.append(value)
            ans[value] += ans[key]
    
    print(*ans)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

def main():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    
    ans = [0]*n
    for _ in range(q):
        p, x = map(int, input().split())
        ans[p-1] += x
    
    not_yet = deque([0])
    already = [False]*n
    already[0] = True
    
    while not_yet:
        key = not_yet.popleft()
        for value in tree[key]:
            if already[value]:
                continue
            already[value] = True
            not_yet.append(value)
            ans[value] += ans[key]
    
    print(*ans)

if __name__ == "__main__":
    main()

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

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

v=0
d=deque()
d.append(v)
seen=[0]*N
seen[0]=1
ans=[0]*N
while len(d)!=0:
    v=d.popleft()
    for j in nondirtree[v]:
        if seen[j]==0:
            dirtree[v].append(j)
            seen[j]=1
            d.append(j)
    
    
v=0
d=deque()
d.append(v)

ans[0]=score[0]
while len(d)!=0:
    v=d.popleft()
    for j in dirtree[v]:
        ans[j]=ans[v]+score[j]
        d.append(j)
    
    
print(*ans,sep=" ")

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
def dfs(G,s,p):
    for nv in G[s]:
        if(nv==p):
            continue
        Count[nv]+=Count[s]
        dfs(G,nv,s)
    
N,Q=map(int,input().split())
Count=[0]*N
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
    G[b].append(a)
for i in range(Q):
    p,x=map(int,input().split())
    Count[p-1]+=x
dfs(G,0,-1)
print(*Count)

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

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

def update(v, p):
    for next in tree[v]:
        if next == p:
            continue
        counter[next] += counter[v]
        update(next, v)

tree = [[] for _ in range(N)]
counter = [0] * N

for _ in range(N-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
for _ in range(Q):
    p, x = map(int, input().split())
    counter[p-1] += x
update(0, -1)
print(" ".join(map(str, counter)))

Solution (Python):
from collections import deque
N,Q=map(int,input().split())
nondirtree=[[] for _ in range(N)]
dirtree=[[] for _ in range(N)]
rootflag=[0]*N
rootflag[0]=1
for i in range(N-1):
    a,b=map(int,input().split())
    nondirtree[a-1].append(b-1)
    nondirtree[b - 1].append(a - 1)
points=[list(map(int,input().split())) for _ in range(Q)]

v=0
d=deque()
d.append(v)
seen=[0]*N
seen[0]=1
score=[0]*N
ans=[0]*N
while len(d)!=0:
    v=d.popleft()
    for j in nondirtree[v]:
        if seen[j]==0:
            dirtree[v].append(j)
            seen[j]=1
            d.append(j)
    
    

for i in range(Q):
    score[points[i][0]-1]=score[points[i][0]-1]+points[i][1]

v=0
d=deque()
d.append(v)
ans[0]=score[0]
while len(d)!=0:
    v=d.popleft()
    for j in dirtree[v]:
        ans[j]=ans[v]+score[j]
        d.append(j)
    
    
print(*ans,sep=" ")

Solution (Python):
from collections import deque
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)

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


def bfs(tree, start, n, query):
    visited = [False] * n
    visited[start] = True
    que = deque([start])
    while que:
        node = que.popleft()
        cost = query[node]
        for n in tree[node]:
            if not visited[n]:
                query[n] += cost
                visited[n] = True
                que.append(n)

bfs(tree, 0, n, query)
print(*query)

Solution (Python):
from collections import deque

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

adj_list = [[] for _ in range(n + 1)]
val = [0] * (n + 1)
visited = [False] * (n + 1)

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

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

dq = deque()
prev = -1
cur = 1
dq.append([cur, prev])

while len(dq) > 0:

    cur, prev = dq.popleft()
    if prev != -1:
        val[cur] += val[prev]
    visited[cur] = True

    for next in adj_list[cur]:
        if visited[next] is False:
            dq.append([next, cur])

print(*val[1:])

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

def dfs(G,v,p):
    for next in G[v]:
        if next == p:
            continue
        counter[next]+=counter[v]
        dfs(G,next,v)
    

N,Q = map(int, input().split())
counter = [0]*N
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)


for _ in range(Q):
    p,x = map(int, input().split())
    p-=1
    counter[p]+=x
dfs(G,0,-1)
counter = map(str,counter)
print(' '.join(counter))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())
V = [[] for _ in range(N)]
X = [0 for _ in range(N)]
def input():
    return sys.stdin.readline()[:-1]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    V[a].append(b)
    V[b].append(a)

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

def dfs(p, n):
    for i in V[n]:
        if i == p:
            continue
        X[i] += X[n] 
        dfs(n, i)
dfs(-1,0)
print(*X)

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):

from collections import deque
n, q = map(int, input().split())
edge = [[] for _ in range(n)]
counter  = [0]*n
for _ in range(n-1):
    u, v = map(int, input().split())
    edge[u-1].append(v-1)
    edge[v-1].append(u-1)
for _ in range(q):
    u, v = map(int, input().split())
    counter[u-1] += v
que = deque([0])
check = [0]*n
while que:
    k = que.pop()
    check[k] = 1
    for i in edge[k]:
        if check[i]: continue
        counter[i] += counter[k]
        que.append(i)
print(*counter)

Solution (Python):
from collections import deque


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

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

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

    counter = [-1] * n
    d = deque([(-1, 0, 0)])
    while len(d) > 0:
        p, i, v = d.pop()
        total = operations[i] + v
        for c in edges[i]:
            if c != p:
                d.append((i, c, total))
        counter[i] = total

    print(*counter)


main()

Solution (Python):
import sys
from collections import deque
stdin=sys.stdin

ns=lambda:input().rstrip()
ni=lambda:int(input().rstrip())
nm=lambda:map(int,input().split())
nl=lambda:list(map(int,input().split()))

N,Q=nm()
AB=[nl() for i in range(N-1)]
PQ=[nl() for i in range(Q)]
root=[[] for i in range(N)]
N_val=[0]*N
ans_val=[0]*N
d=[0]*N

def bfs():
  que=deque([])
  que.append(0)
  while que:
    p=que.popleft()
    for i in root[p]:
       if(d[i]==0):
         ans_val[i]=ans_val[p]+N_val[i]
         que.append(i)
         d[i]=1
      
def dfs(x,d,s):
  for i in root[x]:
    if(d[i]==0):
      s+=N_val[i]
      ans_val[i]=s
      d[i]=1
      dfs(i,d,s)
      s-=N_val[i]
      

for i in range(N-1):
  root[AB[i][0]-1]+=[AB[i][1]-1]
  root[AB[i][1]-1]+=[AB[i][0]-1]
  
for i in range(Q):
  N_val[PQ[i][0]-1]+=PQ[i][1]

  
ans_val[0]=N_val[0]
d[0]=1

bfs()
print(*ans_val)

Solution (Python):
from _collections import deque

N,Q = map(int,input().split())
K = [[] for _ in range(N)]
point = [0]*N

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

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

ans = [0]*N
q = deque()
q.append((0,-1))
while(len(q)>0):
    a,parent = q.popleft()
    ans[a] = ans[parent] + point[a]
    for i in K[a]:
        if i != parent:
            q.append((i,a))

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

Solution (Python):
def main():
    def dfs_rec(start,prev):
        ans[start] = cnt[start]
        if start!=0:
            ans[start] +=ans[prev]
        for node in tree[start]:
            if node != prev:
                dfs_rec(node,start)
        return
    import sys
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline

    n,q = map(int,input().split())
    tree = [[] for i in range(n)]
    cnt = [0] *n
    ans = [0] *n

    for i in range(n-1):
        a,b = map(int,input().split())
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    for i in range(q):
        p,x = map(int,input().split())
        cnt[p-1]+=x
    dfs_rec(0,0)
    print(*ans)
if __name__=="__main__":
    main()