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 OrderedDict
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

N,Q = (int(x) for x in input().split())
connection ={i:set() for i in range(1,N+1)}
for i in range(N-1):
    a,b = (int(x) for x in input().split())
    connection[a].add(b)
    connection[b].add(a)

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

def DFS(now,prev = -1):
    for next in connection[now]:
        if next == prev:
            continue
        point[next] +=point[now] 
        DFS(next,now)

DFS(1)
print(*point.values())

Solution (Python):
n,q = map(int,input().split())
from collections import deque
s = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a = a-1
    b = b-1
    s[a].append(b)
    s[b].append(a)
count = [0 for i in range(n)]
for i in range(q):
    p,x = map(int,input().split())
    count[p-1] += x
ans = [-1 for i in range(n)]
ans[0] = count[0]
d = deque([(0,0)])
while d:
    u,v = d.pop()
    ans[u] = v + count[u]
    for b in s[u]:
        if ans[b] == -1:
            d.append((b,ans[u]))
        else:
            continue
for i in range(n):
    print(ans[i])

Solution (Python):
n,q = map(int,input().split())
from collections import deque
s = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a = a-1
    b = b-1
    s[a].append(b)
    s[b].append(a)
count = [0 for i in range(n)]
for i in range(q):
    p,x = map(int,input().split())
    count[p-1] += x
ans = [-1 for i in range(n)]
ans[0] = count[0]
d = deque([(0,0)])
while d:
    u,v = d.popleft()
    ans[u] = v + count[u]
    for b in s[u]:
        if ans[b] == -1:
            d.append((b,ans[u]))
        else:
            continue
for i in range(n):
    print(ans[i])

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n,q=LI()
ab=[LI() for _ in range(n-1)]
px=[LI() for _ in range(q)]
ans=[0]*(n+1)
graph={i:collections.deque() for i in range(1,n+1)}
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
for p,x in px:
    ans[p]+=x
seen=[0]*(n+1)
stack=[]
def dfs():
    seen[1]=1
    stack.append(1)
    while stack:
        s=stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            gno=graph[s].popleft()
            if seen[gno]:
                continue
            seen[gno]=1
            stack.append(gno)
            ans[gno]+=ans[s]
dfs()
print(*ans[1:])

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n,q=LI()
ab=[LI() for _ in [None]*(n-1)]
px=[LI() for _ in [None]*q]
ans=[0]*(n+1)
graph={i:collections.deque() for i in range(1,n+1)}
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
for p,x in px:
    ans[p]+=x
seen=[0]*(n+1)
stack=[]
def dfs():
    seen[1]=1
    stack.append(1)
    while stack:
        s=stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            gno=graph[s].popleft()
            if seen[gno]:
                continue
            seen[gno]=1
            stack.append(gno)
            ans[gno]+=ans[s]
dfs()
print(*ans[1:])

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

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

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


def dfs(curr, pare):
    for chi in g[curr]:
        if chi == pare: continue
        pl[chi] += pl[curr]
        dfs(chi, curr)

def main():
    dfs(0,-1)
    print(*pl)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

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

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

stack = deque([0])
visited = [False] * N
visited[0] = True
while stack:
    cur = stack.pop()
    for target in tre[cur]:
        if visited[target]:
            continue
        visited[target] = True
        stack.append(target)
        val[target] += val[cur]

print(*val)

Solution (Python):


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

children = [[] for _ in range(N)]
points = [0] * N

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

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

stack = []
stack.append(0)
visited = [False] * N
while stack:
    key = stack.pop()
    visited[key] = True
    for child in children[key]:
        if visited[child] == False:
            points[child] += points[key]
            stack.append(child)

print(*points)

Solution (Python):


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

children = [[] for _ in range(N)]
points = [0] * N

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

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

stack = []
stack.append(0)
visited = [False] * N
while stack:
    key = stack.pop()
    for child in children[key]:
        if visited[child] == True:
            continue
        points[child] += points[key]
        visited[key] = True
        stack.append(child)

print(*points)

Solution (Python):
N,Q = [int(x) for x in input().split()]
ki = [0]*N
tr = [[] for _ in range(N)]

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

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1] += val

stack =[0]
visited = set()
visited.add(0)

while stack:
    i = stack.pop()
    for j in tr[i]:
        if j in visited:
            continue
        visited.add(j)
        ki[j] += ki[i]
        stack.append(j)

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

Solution (Python):
from collections import deque
n,q = map(int,input().split())
ad_ls = [[] for _ in range(n+1)]
count_ls = [0] * (n+1)
done_ls = [0] * (n+1)
for i in range(n-1):
    a,b = map(int,input().split())
    ad_ls[a].append(b)
    ad_ls[b].append(a)
for i in range(q):
    p,x = map(int,input().split())
    count_ls[p] += x
def turn(ver):
    if ad_ls[ver]:
        for ad in  ad_ls[ver]:
            if not done_ls[ad]:
                count_ls[ad] += count_ls[ver]
                q.append(ad)
    done_ls[ver] = 1
q = deque()
q.append(1)
while len(q) > 0:
    target = q.popleft()
    turn(target)
count_ls = list(map(str, count_ls))
print(' '.join(count_ls[1:]))

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

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

from collections import deque
todo = deque([(1,0)])

while len(todo) > 0:
    child,parent = todo.pop()
    value[child] += value[parent]
    for next in tree[child]:
        if next == parent:
            continue
        else:
            todo.append((next,child))

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

Solution (Python):
from sys import stdin
from collections import deque
def main():
    
    readline=stdin.readline
    n,q=map(int,readline().split())
    G=[[] for _ in range(n+1)]
    for i in range(n-1):
        a,b=map(int,readline().split())
        G[a].append(b)
        G[b].append(a)
    px=[0]*(n+1)
    for i in range(q):
        p,x=map(int,readline().split())
        px[p]+=x

    cnt=[0]*(n+1)
    flags=[False]*(n+1)
    d=deque([[1,px[1]]])
    while len(d)>0:
        now=d.pop()
        flags[now[0]]=True
        cnt[now[0]]=now[1]
        for nex in G[now[0]]:
            if flags[nex]==False:
                d.append([nex,now[1]+px[nex]])

    print(*cnt[1:])

if __name__=="__main__":
    main()

Solution (Python):

import numpy as np
from collections import deque
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
N, Q = list(map(int, input().split()))
tree = [[] for i 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)
counter = [0]*N
p_lis = [0] * Q
x_lis = [0] * Q
for i in range(Q):
    p, x = map(int, input().split())
    counter[p-1] += x
stack = deque([0])
visited = [0]*N
visited[0] = 1  
while stack:
    node = stack.pop()
    for child in tree[node]:
        if visited[child] == 1:
            continue
        stack.append(child)
        visited[node] = 1
        counter[child] += counter[node]


print(*counter)

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

def solve():
    N, Q = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(N-1):
        A, B = map(int, input().split())
        A, B = A-1, B-1
        adjL[A].append(B)
        adjL[B].append(A)

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

    useds = [False] * N
    def dfs(vNow, x):
        useds[vNow] = True
        anss[vNow] += x
        for v2 in adjL[vNow]:
            if not useds[v2]:
                dfs(v2, anss[vNow])

    dfs(0, 0)

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

solve()

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

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

anss = [0] * N
for p, x in querys:
    anss[p-1] += x

def bfs(vSt):
    useds = [False] * (N)
    useds[vSt] = True
    Q = deque([vSt])
    while Q:
        vNow = Q.popleft()
        ans = anss[vNow]
        for v2 in adjL[vNow]:
            if not useds[v2]:
                anss[v2] += ans
                Q.append(v2)
                useds[v2] = True

bfs(0)

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

Solution (Python):
import sys

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

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

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

stack = [1]
visited = [False for _ in range(N + 1)]
visited[1] = True

while stack:
    i = stack.pop()
    for j in E[i]:
        if visited[j]:
            continue
        visited[j] = True
        stack.append(j)
        X[j] += X[i]

print(*X[1:])

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


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


def dfs(now, prev=-1):
    for next in tree[now]:
        
        if next == prev:
            continue

        
        point[next] += point[now]

        
        dfs(next, now)


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):
    p, x = map(int, input().split())
    point[p-1] += x

dfs(0)
print(*point)

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

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

anss = [0] * N
for p, x in querys:
    anss[p-1] += x

def bfs(vSt):
    useds = [False] * (N)
    useds[vSt] = True
    Q = deque([vSt])
    while Q:
        vNow = Q.popleft()
        ans = anss[vNow]
        for v2 in adjL[vNow]:
            if not useds[v2]:
                anss[v2] += ans
                Q.append(v2)
                useds[v2] = True

bfs(0)

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

Solution (Python):
import sys
from collections import deque

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

N, Q = [int(_) for _ in input().split()]

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

P = [0] * (N + 1)

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

que = deque()
que.append((1, 0))

while que:
    v, parent = que.popleft()
    P[v] += P[parent]
    for next_v in edges[v]:
        if next_v == parent: continue
        que.append((next_v, v))










print(*[P[i] for i in range(1, N + 1)])

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

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

from collections import deque

todo = deque([(1,0)])
while len(todo) > 0:
    x,parent = todo.pop()
    ans[x] += ans[parent]
    for next in tree[x]:
        if next == parent:
            continue
        else:
            todo.append((next,x))
            
answer = ' '.join(map(str,ans[1:]))
print(answer)

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,Q = LI()
ab = [LI() for _ in [None]*(N-1)]
px = [LI() for _ in [None]*(Q)]
ans = [0]*(N+1) 
graph = {i:collections.deque() for i in range(1,N+1)} 
for a,b in ab:
        graph[a].append(b)
        graph[b].append(a)
for p,x in px:
    ans[p] += x
seen = [0]*(N+1) 
stack = []
def dfs():
    seen[1] = 1
    stack.append(1)
    while stack:
        s = stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            g_NO = graph[s].popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            stack.append(g_NO)
            ans[g_NO] += ans[s]
dfs()
print(*ans[1:])

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,Q = LI()
ab = [LI() for _ in range(N-1)]
px = [LI() for _ in range(Q)]
ans = [0]*(N+1) 
graph = {i:collections.deque() for i in range(1,N+1)} 
for a,b in ab:
        graph[a].append(b)
        graph[b].append(a)
for p,x in px:
    ans[p] += x
seen = [0]*(N+1) 
stack = []
def dfs():
    seen[1] = 1
    stack.append(1)
    while stack:
        s = stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            g_NO = graph[s].popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            stack.append(g_NO)
            ans[g_NO] += ans[s]
dfs()
print(*ans[1:])

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

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

from collections import deque

count = [0]*N

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

queue = deque([0])
checked  = [0]*N
while queue:
    v = queue.pop()
    checked[v] = 1
    x = count[v]
    Vs = query[v]
    for i in Vs:
        if checked[i]:
            continue
        count[i] += x
        queue.append(i)
    
print(*count)

Solution (Python):
from collections import deque
n,q = map(int,input().split())
c = [[] for _ in range(n)]
for i in range(n-1):
    A,B = map(int,input().split())
    c[A-1].append(B)
    c[B-1].append(A)
z = [0] * n
for i in range(q):
    P,X = map(int,input().split())
    z[P - 1] += X


d = [-1] * n
d[0] = 0
q = deque([])
q.append(1)
while(q):
    tp = q.popleft()
    td = d[tp - 1]
    for n2 in c[tp - 1]:
        if(d[n2 - 1] == -1):
            d[n2 - 1] = td + 1
            z[n2 - 1] += z[tp - 1]
            q.append(n2)

print(*z)

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

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

def bfs(x,x_list):
  qu=deque([])
  qu.append(0)
  qu.append(x)
  node_number=1
  while qu:
    pre=qu.popleft()
    node_number = qu.popleft()
    nodes = edge[node_number]
    for i in nodes:
      if pre != i:
        qu.append(node_number)
        qu.append(i)
        x_list[i]+=x_list[node_number]
      else:continue
  return x_list
answer=bfs(1,x_list)[1:]
print(*answer)

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

def bfs(x,counter):
  qu=deque([])
  qu.append(x)
  qu.append(0)
  while qu:
    now=qu.popleft()
    pre=qu.popleft()
    if now!=1:
      counter[now-1]+=counter[pre-1]
    for i in edge[now]:
      if i!=pre:
        qu.append(i)
        qu.append(now)
      else:continue
  return counter
bfs(1,counter)
print(*counter)

Solution (Python):
from collections import deque


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

    
    cnt = [0]*N

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

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

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

    
    while len(stack):
        idx = stack.pop()
        seen[idx] += 1
        for c in g[idx]:
            if seen[c]:
                continue
            cnt[c] += cnt[idx]
            stack.append(c)

    print(*cnt)


if __name__ == '__main__':
    main()

Solution (Python):
from _collections import deque
N, Q = map(int, input().split())
graph = [[] for _ in range(N+1)]
for i in range(N-1):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

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

stack = deque()
stack.append(1)
score = 0
score_array = [0]*(N+1)
is_checked = [0]*(N+1)

while stack:
    node = stack.pop()
    is_checked[node] = 1

    
    score_array[node] += points[node]
    
    for goto in graph[node]:
        if not is_checked[goto]:
            
            stack.append(goto)
            score_array[goto] += score_array[node]

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

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

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

from collections import deque
todo = deque([(1,0)])
while len(todo) > 0:
    x,parent = todo.pop()
    value[x] += value[parent]
    for y in graph[x]:
        if y == parent:
            continue
        todo.append((y,x))

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

Solution (Python):


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

def dfs(now , prev = -1):
    for next in graph[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())
graph = [[] for _ in range(N)]
point = [0] * N
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)

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


def dfs(now , prev = -1):
    for next in graph[now]:
        
        if next == prev:
            continue
        
        point[next] += point[now]
        
        dfs(next , now)

dfs(0)
print(*point)

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
    
q = [(1,0)]
while q:
    x,parent = q.pop()
    value[x] += value[parent]
    for y in graph[x]:
        if y == parent:
            continue
        q.append((y,x))
 
answer = ' '.join(map(str,value[1:]))
print(answer)

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

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
    
q = [(1,0)]
while q:
    x,parent = q.pop()
    value[x] += value[parent]
    for y in graph[x]:
        if y == parent:
            continue
        q.append((y,x))
 
answer = ' '.join(map(str,value[1:]))
print(answer)

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

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

from collections import deque
todo = deque([(1,0)])
while len(todo) > 0:
    x,parent = todo.pop()
    value[x] += value[parent]
    for y in graph[x]:
        if y == parent:
            continue
        todo.append((y,x))

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

Solution (Python):
from collections import deque

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

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

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

que = deque([])
que.append(0)
s = [0] * N
while que:
    t = que.popleft()
    s[t] = 1
    for e in g[t]:
        if s[e] == 1: continue
        cnt[e] += cnt[t]
        que.append(e)

print(*cnt)

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

def abc138_d():
    N, _ = map(int, readline().strip().split())
    input_data = list(map(int, read().split()))
    AB = iter(input_data[:N+N-2])
    PX = iter(input_data[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]
    visited = [False] * (N+1)
    visited[1] = True
    while stack:
        x = stack.pop()
        for y in graph[x]:
            if visited[y]: continue
            stack.append(y)
            visited[y] = True
            val[y] += val[x]

    print(*val[1:], sep=' ')

abc138_d()

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

def abc138_d():
    N, _ = map(int, readline().strip().split())
    input_data = [tuple(map(int, ln.strip().split())) for ln in readlines()]

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

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

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

    print(*val[1:], sep=' ')

abc138_d()

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

def abc138_d():
    N, _ = map(int, readline().strip().split())
    input_data = [tuple(map(int, ln.strip().split())) for ln in readlines()]

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

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

    
    stack = [1]
    visited =set([1])
    while stack:
        x = stack.pop()
        for y in graph[x]:
            if y in visited: continue
            stack.append(y)
            visited.add(y)
            val[y] += val[x]

    print(*val[1:], sep=' ')

abc138_d()

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

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

def bfs(x,x_list):
  qu=deque([])
  qu.append(0)
  qu.append(x)
  node_number=1
  while qu:
    pre=qu.popleft()
    node_number = qu.popleft()
    nodes = edge[node_number]
    for i in nodes:
      if pre != i:
        qu.append(node_number)
        qu.append(i)
        x_list[i]+=x_list[node_number]
      else:continue
  return x_list
answer=bfs(1,x_list)[1:]
print(*answer)

Solution (Python):
import sys, re
from math import ceil, floor, sqrt, pi
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left
from functools import reduce
input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_rpw_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 7)
INF = float('inf')
MOD = 10 ** 9 + 7

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

def main():
    n, q = i_map()

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

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

    dfs(0, -1, edges, ans_list)

    print(*ans_list)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10000000)

input=lambda: sys.stdin.readline().rstrip() 

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

lv =[[] for _ in range(n)]
ans=[0] * (n)

def dfs(v,p=-1):
    for u in lv[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u,v)

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

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

dfs(0)

print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)

input=sys.stdin.readline      

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

lv =[[] for _ in range(n)]
ans=[0] * (n)

def dfs(v,p=-1):
    for u in lv[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u,v)

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

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

dfs(0)

print(*ans)

Solution (Python):
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))

 
 
from sys import stdin,setrecursionlimit

 
setrecursionlimit(10**6)
input=stdin.readline
 
def dfs(p,father=-1):
    for i in tree[p]:
        if i == father:
            continue
        counter[i]+=counter[p]
        dfs(i,p)
 
n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
 
for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    
 
dfs(0)
 
print(*counter)

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

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


def dfs(now , prev = -1):
    for next in graph[now]:
        
        if next == prev:
            continue
        
        point[next] += point[now]
        
        dfs(next , now)

dfs(0)
print(*point)