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):
def main():
  import sys
  input = sys.stdin.readline
  from collections import deque
  
  n,q=map(int,input().split())
  ab=[[] for _ in range(n+1)]
  for _ in range(n-1):
    a,b=map(int,input().split())
    ab[a].append(b)
    ab[b].append(a)
  ans=[0]*(n+1)
  for _ in range(q):
    p,x=map(int,input().split())
    ans[p]+=x
  
  l,parent=deque([1]),[0]*(n+1)
  parent[1]=1
  while l:
    x=l.popleft()
    for i in ab[x]:
      if parent[i]:
        continue
      l.append(i)
      ans[i]+=ans[x]
      parent[i]=1
      
  print(*ans[1:])
if __name__=='__main__':
  main()

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

neighbor = defaultdict(list)
counter = [0] * N

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

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

def propagate():
    stack = [(0, None)]
    while stack:
        node, parent = stack.pop()
        for c in neighbor[node]:
            if c != parent:
                counter[c] += counter[node]
                stack.append((c, node))

propagate()
print(*counter)

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

neighbor = defaultdict(list)
counter = [0] * N

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

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

def propagate():
    stack = [(0, None)]
    while stack:
        (node, parent) = stack.pop()
        for c in neighbor[node]:
            if c != parent:
                counter[c] += counter[node]
                stack.append((c, node))

propagate()
print(*counter)

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

neighbor = defaultdict(list)
counter = [0] * N

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

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

def propagate():
    stack = deque([(0, None)])
    while stack:
        (node, parent) = stack.pop()
        if parent is not None:
            counter[node] += counter[parent]
        for c in neighbor[node]:
            if c != parent:
                stack.append((c, node))

propagate()
print(*counter)

Solution (Python):
import sys
from collections import defaultdict
sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())

neighbor = defaultdict(list)
counter = [0] * N

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

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

def solve():
    stack = [(0, None)]
    while stack:
        (node, parent) = stack.pop()
        if parent is not None:
            counter[node] += counter[parent]
        for c in neighbor[node]:
            if c != parent:
                stack.append((c, node))

solve()
print(*counter)

Solution (Python):
import sys
from collections import defaultdict
sys.setrecursionlimit(100000)
N, Q = map(int, input().split())

neighbor = defaultdict(list)
counter = [0] * N
pcounter = [None] * N
depth = {0: 0}

for i in range(N-1):
    a, b = map(int, input().split())
    a_deper, b_deeper = False, False
    if a in depth and b not in depth:
        depth[b] = depth[a] + 1
    if b in depth and a not in depth:
        depth[a] = depth[b] + 1
    if a in depth and b in depth:
        if depth[a] < depth[b]:
            neighbor[a-1].append(b-1)
        else:
            neighbor[b-1].append(a-1)
    else:
            neighbor[a-1].append(b-1)
            neighbor[b-1].append(a-1)

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

def solve():
    stack = [(0, None)]
    while stack:
        (node, parent) = stack.pop()
        pcounter[node] = counter[node]
        if parent is not None:
            pcounter[node] += pcounter[parent]
        for c in neighbor[node]:
            if pcounter[c] is None:
                stack.append((c, node))

solve()
print(*pcounter, sep=' ')

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



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



seen_points = [False for _ in range(N)]
queue = deque([0])
while queue:
    now_index = queue.pop()
    seen_points[now_index] = True
    for next_index in bars[now_index]:
        if not seen_points[next_index]:
            points[next_index] += points[now_index]
            queue.append(next_index)

print(*points)

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

def input():
    return sys.stdin.readline().strip()


N,Q = map(int,input().split())
K = [[] for _ in range(N+1)]
nums = [0 for _ in range(N+1)]

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

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

def dfs(now,parent):
    for i in K[now]:
        if i != parent:
            nums[i] += nums[now]
            dfs(i,now)

dfs(1,0)
print(*nums[1:])

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

def input():
    return sys.stdin.readline().strip()
def main():
    N,Q = map(int,input().split())
    K = [[] for _ in range(N+1)]
    nums = [0 for _ in range(N+1)]

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

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

    def dfs(now,parent):
        for i in K[now]:
            if i != parent:
                nums[i] += nums[now]
                dfs(i,now)

    dfs(1,0)
    print(*nums[1:])

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
n, q = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = list(map(lambda x: int(x)-1, input().split()))
    edges[a].append(b)
    edges[b].append(a)

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

queue = deque([0])
visited = [0]*n
while queue:
    v = queue.pop()
    if visited[v]:
        continue
    visited[v] = 1
    for v2 in edges[v]:
        if visited[v2]:
            continue
        values[v2] += values[v]
        queue.append(v2)
print(*values)

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
n, q = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = list(map(lambda x: int(x)-1, input().split()))
    edges[a].append(b)
    edges[b].append(a)

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

queue = deque([0])
visited = [0]*n
while queue:
    v = queue.pop()
    if visited[v]:
        continue
    visited[v] = 1
    for v2 in edges[v]:
        if visited[v2]:
            continue
        values[v2] += values[v]
        queue.append(v2)
print(*values)

Solution (Python):
import sys
input = sys.stdin.readline
n, q = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = list(map(lambda x: int(x)-1, input().split()))
    edges[a].append(b)
    edges[b].append(a)

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

stack = [0]
visited = [0]*n
while stack:
    v = stack.pop()
    if visited[v]:
        continue
    visited[v] = 1
    for v2 in edges[v]:
        if visited[v2]:
            continue
        values[v2] += values[v]
        stack.append(v2)
print(*values)

Solution (Python):
import sys
input = sys.stdin.readline
n, q = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = list(map(lambda x: int(x)-1, input().split()))
    edges[a].append(b)
    edges[b].append(a)

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

stack = [0]
visited = [0]*n
while stack:
    v = stack.pop()
    if visited[v]:
        continue
    visited[v] = 1
    for v2 in edges[v]:
        if visited[v2]:
            continue
        values[v2] += values[v]
        stack.append(v2)
print(*values)

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

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

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

def dfs(v, parent, add):
    nodes[v] += add
    for v2 in edges[v]:
        if v2 == parent:
            continue
        dfs(v2, v, nodes[v])

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

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

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

counter = [0 for _ in range(n)]
E = [[] for _ in range(n)]



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

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

res = 0

color = [-1 for _ in range(n)]


stack = [0]
color[0] = counter[0]
while stack:
  u = stack.pop()
  for v in E[u]:
    if color[v] == -1:
      stack.append(v)
      color[v] = color[u] + counter[v]

ans = [str(color[i]) for i in range(n)]
print(" ".join(ans))

Solution (Python):
from collections import deque

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



q=deque() 
q.append(1)
parent=[-1]*(N+1)


while q:
    now=q.popleft()
    for k in L[now]:
        if parent[now]==k:
            continue
        count[k]+=count[now]
        parent[k]=now
        q.append(k)
        

print(*count[1:])

Solution (Python):
from collections import deque

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



q=deque() 
q.append(1)
parent=[-1]*(N+1)


while q:
    now=q.popleft()
    for k in L[now]:
        if parent[now]==k:
            continue
        count[k]+=count[now]
        parent[k]=now
        q.append(k)
        

print(*count[1:])

Solution (Python):



def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

from collections import deque

N, Q = MAP()
counter = [0]*N
tree = [[] for _ in range(N)]
parent = [-1]*N
for _ in range(N-1):
    a, b = MAP()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

for _ in range(Q):
    p, x = MAP()
    counter[p-1] += x

q = deque()
q.append(0)

while q:
    index = q.pop()
    pindex = parent[index]
    for i in tree[index]:
        if i == pindex:
            continue
        counter[i] += counter[index]
        parent[i] = index
        q.append(i)

print(*counter)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

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

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

    def dfs(now, parent):
        for i in edges[now]:
            if i != parent:
                nums[i] += nums[now]
                dfs(i, now)

    dfs(1, 0)
    for n in nums[1:]:
        print(n)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

N, Q = map(int,input().split())
counter = [0]*N
tree = [[] for _ in range(N)]
parent = [-1]*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

q  = deque()
q.append(0)

while q:
  index = q.pop()
  pindex = parent[index]
  for i in tree[index]:
    if i == pindex: continue
    counter[i] += counter[index]
    parent[i] = index
    q.append(i)
    
print(*counter)

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


seen = [0]*N
todo = deque([0])
seen[0] = 1
while todo:
    v = todo.pop()
    cost = counter[v]
    for nv in G[v]:
        if seen[nv]:
            continue
        seen[nv] = 1
        counter[nv] += cost
        todo.append(nv)

print(*counter)

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


seen = [0]*N
todo = [0]
seen[0] = 1
while todo:
    v = todo.pop()
    cost = counter[v]
    for nv in G[v]:
        if seen[nv]:
            continue
        seen[nv] = 1
        counter[nv] += cost
        todo.append(nv)

print(*counter)

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

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

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

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

def dfs(now, prev):
    
    
    for next in g[now]:
        if next != prev:
            chk[next] += chk[now]
            dfs(next, now)

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

Solution (Python):
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]




from collections import deque

def main():

    
    N, Q = mi()
    x = li2(N-1)

    cnt = [0]*N
    for i in range(Q):
        p0, p1 = mi()
        cnt[p0-1] += p1

    
    adj = [[] for i in range(N)]
    for i in range(N-1):
        adj[x[i][0]-1].append(x[i][1]-1)
        adj[x[i][1]-1].append(x[i][0]-1)

    que = deque()
    ans = [0]*N

    
    que.append((0, 0))
    num = 0
    while que!=deque():
        v = que.popleft()
        ans[v[1]] = ans[v[0]] + cnt[v[1]]
        for next in adj[v[1]]:
            if next != v[0]:
                que.append((v[1], next))

    print(*ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]




from collections import deque

def main():

    
    N, Q = mi()
    x = li2(N-1)

    cnt = [0]*N
    for i in range(Q):
        p0, p1 = mi()
        cnt[p0-1] += p1

    
    adj = [[] for i in range(N)]
    for i in range(N-1):
        x[i][0] -= 1
        x[i][1] -= 1
        adj[x[i][0]].append(x[i][1])
        adj[x[i][1]].append(x[i][0])

    que = deque()
    ans = [0]*N

    
    que.append((0, 0))
    num = 0
    while que!=deque():
        v = que.popleft()
        ans[v[1]] = ans[v[0]] + cnt[v[1]]
        for next in adj[v[1]]:
            if next != v[0]:
                que.append((v[1], next))

    for k in ans:
        print(k, '', end='')

if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]





def main():

    
    N, Q = mi()
    x = li2(N-1)

    cnt = [0]*N
    for i in range(Q):
        p0, p1 = mi()
        cnt[p0-1] += p1

    
    adj = [[] for i in range(N)]
    for i in range(N-1):
        
        
        adj[x[i][0]-1].append(x[i][1]-1)
        adj[x[i][1]-1].append(x[i][0]-1)

    
    global ans
    ans = [0]*N

    def dfs(fr, nx):
        ans[nx] += ans[fr] + cnt[nx]
        for v in adj[nx]:
            if v != fr:
                dfs(nx, v)

    dfs(0, 0)
    '''
    for i in range(N):
        print(ans[i], '', end='')
    '''
    print(*ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]






N, Q = mi()
x = li2(N-1)

cnt = [0]*N
for i in range(Q):
    p0, p1 = mi()
    cnt[p0-1] += p1


adj = [[] for i in range(N)]
for i in range(N-1):
    
    
    adj[x[i][0]-1].append(x[i][1]-1)
    adj[x[i][1]-1].append(x[i][0]-1)

global ans
ans = [0]*N

def dfs(fr, nx):
    ans[nx] += ans[fr] + cnt[nx]
    for v in adj[nx]:
        if v != fr:
            dfs(nx, v)

dfs(0, 0)
'''
for i in range(N):
    print(ans[i], '', end='')
'''
print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]






N, Q = mi()
x = li2(N-1)

cnt = [0]*(N+1)
for i in range(Q):
    p0, p1 = mi()
    cnt[p0] += p1


adj = [[] for i in range(N+1)]
for i in range(N-1):
    
    
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

global ans
ans = [0]*(N+1)

def dfs(fr, nx):
    ans[nx] += ans[fr] + cnt[nx]
    for v in adj[nx]:
        if v != fr:
            dfs(nx, v)

dfs(1, 1)

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

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]






N, Q = mi()
x = li2(N-1)

cnt = [0]*(N+1)
for i in range(Q):
    p0, p1 = mi()
    cnt[p0] += p1


adj = [[] for i in range(N+1)]
for i in range(N-1):
    
    
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

global ans
ans = [0]*(N+1)

def dfs(fr, nx):
    ans[nx] += ans[fr] + cnt[nx]
    for v in adj[nx]:
        if v != fr:
            dfs(nx, v)

dfs(1, 1)

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

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
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import bisect 

'''
from collections import deque
N, Q = mi()
D = li2(N-1)
px = li2(Q)
que = deque()

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

for i in range(N-1):
    adj[D[i][0]].append(D[i][1])

ans = [0]*(N+1)
for j in range(Q):
    ind, a = px[j][0], px[j][1]
    que.append(ind)
    while(que!=deque()):
        v = que.popleft()
        ans[v] += a
        for e in adj[v]:
            que.append(e)


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

from collections import deque
sys.setrecursionlimit(10**9)


N, Q = mi()
x = li2(N-1)


cnt = [0]*N
for i in range(Q):
    p0, p1 = mi()
    cnt[p0-1] += p1


adj = [[] for i in range(N)]
for i in range(N-1):
    x[i][0] -= 1
    x[i][1] -= 1
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

que = deque()
ans = [0]*N


que.append((0, 0))
num = 0
while que!=deque():
    v = que.popleft()
    ans[v[1]] = ans[v[0]] + cnt[v[1]]
    for next in adj[v[1]]:
        if next != v[0]:
            que.append((v[1], next))

for k in ans:
    print(k, '', end='')

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

def input():
    return sys.stdin.readline()

N, Q = map(int, input().split())
adj = [[] for _ in range(N+1)]
for _ in range(N-1):
    x, y = map(int, input().split())
    adj[x].append(y)
    adj[y].append(x)

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

def dfs(now, prev):
    for target in adj[now]:
        if target == prev:
            continue
        score[target] += score[now]
        dfs(target, now)

dfs(1, 0)

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

Solution (Python):
I=lambda:map(int,input().split());
n,q=I();r=[0]*n;e=[[]for _ in range(n)];y=[0]+[1]*n;s=[0]
for _ in range(n-1):a,b=I();e[a-1]+=b-1,;e[b-1]+=a-1,;
for _ in range(q):p,x=I();r[p-1]+=x;
while s:
  u=s.pop();v=r[u];
  for c in e[u]:
    if y[c]:y[c]=0;r[c]+=v;s+=c,;
print(*r)

Solution (Python):
I=lambda:map(int,input().split());
n,q=I();r=[0]*n;e=[[]for _ in range(n)];y=[0]+[1]*n;s=[0]
for _ in range(n-1):a,b=I();e[a-1]+=b-1,;e[b-1]+=a-1,;
for _ in range(q):p,x=I();r[p-1]+=x;
while s:
  u=s.pop();v=r[u];
  for c in e[u]:
    if y[c]:y[c]=0;r[c]+=v;s+=c,;
print(*r)

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

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

N,Q = [int(i) for i in input().split()] 
aaabbb = [[int(i) for i in input().split()] for j in range(N-1)] 
pppxxx = [[int(i) for i in input().split()] for j in range(Q)] 

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

for a,b in aaabbb:
    tree[a].append(b)
    tree[b].append(a)



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

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



def dfs(pos,parent,add):
    counter[pos] += add
    for next_pos in tree[pos]:
        if not next_pos == parent:
            dfs(next_pos,pos,counter[pos])

dfs(1,0,0)



print(" ".join([str(i) for i in counter[1:]]))

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

N,Q = map(int,input().split())
AB = [[int(i) for i in input().split()] for _ in range(N-1)]
PX = [[int(j) for j 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)
    
counters = [0] * (N+1)
for p,x in PX:
    counters[p] += x
    
def dfs(v,parent,add):
    counters[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,counters[v])

dfs(1,0,0)

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

Solution (Python):
from collections import deque

N, Q = map(int, input().split())
graph = [[] for _ in range(N+1)]
ans = [0] * (N+1)

for i in range(N-1):
  x, y = map(int, input().split())
  graph[x].append(y)
  graph[y].append(x)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p] += x
  
flag = [0] * (N+1)
flag[1] = 1
q = deque([1])
while q:
  parent = q.popleft()
  for child in graph[parent]:
    if(flag[child] == 0):
      ans[child] += ans[parent]
      flag[child] = 1
      q.append(child)
      
ans = ans[1:]
print(*ans)

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

def resolve():
    N, Q = map(int, input().split())
    edge = [[] for _ in range(N)]
    for i 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 i in range(Q):
        p, x = map(int, input().split())
        cnt[p-1] += x

    stk = []
    stk.append((0, -1))
    while len(stk):
        v, par = stk.pop()
        if v>0:
            cnt[v] += cnt[par]
        for i in edge[v]:
            if i!=par:
                stk.append((i, v))

    print(*cnt)

if __name__ == '__main__':
    resolve()

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

def resolve():
    N, Q = map(int, input().split())
    edge = [[] for _ in range(N)]
    for i 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 i in range(Q):
        p, x = map(int, input().split())
        cnt[p-1] += x

    stk = []
    
    stk.append((0, -1))
    while len(stk):
        
        v, par = stk.pop()
        
        
        if v>0:
            cnt[v] += cnt[par]
        for i in edge[v]:
            if i!=par:
            
                stk.append((i, v))
        

    print(*cnt)

if __name__ == '__main__':
    resolve()

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

def resolve():
    N, Q = map(int, input().split())
    edge = [[] for _ in range(N)]
    for i 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 i in range(Q):
        p, x = map(int, input().split())
        cnt[p-1] += x

    def dfs(v, par):
        if v>0:
            cnt[v] += cnt[par]
        for i in edge[v]:
            if i!=par:
                dfs(i, v)

    dfs(0, -1)

    print(*cnt)

if __name__ == '__main__':
    resolve()

Solution (Python):
from collections import deque
n,q = map(int,input().split())
v = [0]*n
g = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for _ in range(q):
    p,x = map(int,input().split())
    v[p-1] += x
que = deque([(0, -1)])
while que:
    now, pre = que.pop()
    for j in g[now]:
        if j==pre:
            continue
        else:
            v[j] += v[now]
            que.append((j, now))
print(*v)

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

n,q = map(int,input().split())
tree = [[] for _ in range(n)]
point = [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):
    a,b = map(int,input().split())
    a -= 1
    point[a] += b
    
def dfs(now, prev = -1):
    for next in tree[now]:
        if next == prev:
            continue
        point[next] += point[now]
        
        dfs(next, now)
        
dfs(0)
print(*point)

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

n,q = map(int,input().split())
tree = [[] for _ in range(n)]
point = [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):
    a,b = map(int,input().split())
    a -= 1
    point[a] += b
    
def dfs(now, prev = -1):
    for next in tree[now]:
        if next == prev:
            continue
        point[next] += point[now]
        
        dfs(next, now)
        
dfs(0)
print(*point)

Solution (Python):
f=lambda:map(int,input().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=[1]*n
s=[0]
b[0]=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)