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 heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations
import sys
import bisect
import string
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
mo=10**9+7

n,q=MI()
v=[0]*n
c=[0]*n
g=[[] for i in range(n)]
for i in range(n-1):
    a,b=MI()
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)

for i in range(q):
    p,x=MI()
    c[p-1]+=x




def bfs(x):
    p=deque()
    p.append(x)
    while p:
        cur=p.popleft()
        v[cur]=1
        for i in g[cur]:
            if v[i]==0:
                p.append(i)
                c[i]+=c[cur]
bfs(0)

print(*c)

Solution (Python):

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def dfs(graph, v, start=0):
    n = len(graph)

    dp = v
    visited, finished = [False] * n, [False] * n

    stack = [start]
    while stack:
        start = stack[-1]

        
        if not visited[start]:
            visited[start] = True
            for child in graph[start]:
                if not visited[child]:
                    dp[child] += dp[start]
                    stack.append(child)

        else:
            stack.pop()

            
            

            
            
            
            

            

    return dp


def main():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n)]
    v = [0] * n

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

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

    print(*dfs(tree, v))




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def dfs(v):
    for w in e[v]:
        if root[w]>=0:continue
        root[w]=v
        dfs(w)
def dfs2(v):
    for w in e[v]:
        if f[w]:continue
        f[w]=1
        data[w]+=data[v]
        dfs2(w)
input=sys.stdin.readline
n,q=map(int,input().split())
e=[[]for _ in range(n)]
root=[0]+[-1]*(n-1)
data=[0]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    e[a-1]+=b-1,
    e[b-1]+=a-1,

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

Solution (Python):
import sys
input=sys.stdin.readline
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)
imos=[0]*n
count=[-1]*n
for i in range(q):
    p,x=map(int,input().split())
    imos[p-1]+=x
stack=[(0,0)]
while len(stack)>0:
    now=stack.pop()
    x=now[0]
    y=now[1]
    c=y+imos[x]
    count[x]=c
    for i in G[x]:
        if count[i]==-1:
            stack.append((i,c))
for i in count:
    print(i,end='')
    print(' ',end='')

Solution (Python):
from collections import deque

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

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

dq = deque([(0, -1)])
while dq:
    v, p = dq.popleft()
    for u in edge[v]:
        if u == p:
            continue
        imos[u] += imos[v]
        dq.append((u, v))

print(' '.join([str(x) for x in imos]))

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, Q = MAP()
nodes = [[] for i in range(N)]
for i in range(N-1):
    a, b = MAP()
    nodes[a-1].append(b-1)

ans = [0] * N
for i in range(Q):
    p, x = MAP()
    ans[p-1] += x

def dfs(u, prev, cnt):
    ans[u] += cnt
    for v in nodes[u]:
        if v != prev:
            dfs(v, u, ans[u])
dfs(0, -1, 0)

print(*ans)

Solution (Python):


N, Q = map(int, input().strip().split())
v_list = [set() for i in range(N)]

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

queue = [0]
while queue:
	cur = queue.pop()
	for child in v_list[cur]:
		v_list[child].remove(cur)
		queue.append(child)

x_table = [0] * N

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

values = [None] * N
values[0] = x_table[0]
queue = [0]
while queue:
	cur = queue.pop()
	value = values[cur]
	for child in v_list[cur]:
		values[child] = value + x_table[child]
		queue.append(child)

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

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

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

for i in range(1, n+1):
  c[i] += c[ki[i]]

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

Solution (Python):
from collections import deque
N,Q = map(int,input().split())
edge = [[]for i 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)
tree = [0]*N
for i in range(Q):
    p,x = map(int,input().split())
    tree[p-1] += x
flag = [0]*N
flag[0] = 1
queue = deque([0])
while queue != deque([]):
    p = queue.popleft()
    for i in edge[p]:
        if flag[i] == 0:
            flag[i] = 1
            tree[i] += tree[p]
            queue.append(i)

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

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

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

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


from collections import deque

ans = M[:]
visited = [False] * (N + 1)
visited[1] = True
Q = deque([1])
while Q:
    a = Q.popleft()
    for e in E[a]:
        if visited[e]:
            continue
        visited[e] = True
        ans[e] += ans[a]
        Q.append(e)

print(*ans[1:])

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
N,Q=map(int,input().split())
L=[[] for _ in range(N)]
for _ in range(N-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    L[a].append(b)
    L[b].append(a)
CL=[0]*N
for _ in range(Q):
    p,x=map(int,input().split())
    CL[p-1]+=x

AL=[0]*N
ed=[0]*N
q=[(0,0)]
while len(q)!=0:
    now,val=q.pop(-1)
    ed[now] = 1
    val += CL[now]
    AL[now] = val
    for p in L[now]:
        if ed[p] == 0:
            q.append((p,val))
print(" ".join(map(str,AL)))

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

ab = []
for i in range(N-1):
    a, b = map(int, input().split())
    ab.append((a, b))
ab = sorted(ab, key=lambda x: x[0])

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

for i in range(N-1):
    a, b = ab[i]
    P[b-1] += P[a-1]

print(*P)

Solution (Python):

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

cnt = [0 for _ in range(n)]

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

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

for v in range(n):
    for u in edge[v]:
        cnt[u] += cnt[v]

print(" ".join([str(x) for x in cnt]))

Solution (Python):
N, Q = map(int, input().split())
L = [list(map(int, input().split())) for _ in range(N - 1)]
P = [list(map(int, input().split())) for _ in range(Q)]
G = [[] for _ in range(N + 1)]
X = [0] * (N + 1)
for a, b in L:
    G[a].append(b)
for p, x in P:
    X[p] += x
for a, A in enumerate(G):
    for b in A:
        X[b] += X[a]
print(" ".join(map(str, X[1:])))

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

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

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

def dfs(num, cur_count):
    cur_count += p_num[num]
    ans[num] = cur_count
    if len(ab[num]) <= 0:
        return
    for v in ab[num]:
        dfs(v, cur_count)

dfs(1, 0)

out = []
for i in range(1, n+1):
    out.append(str(ans[i]))
print(" ".join(out))

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


n,q = m()

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

root = [[] for i in range(n)]

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

for i in range(q):
    a,b = m()
    a -= 1
    count[a] += b
de = cl.deque()

for i in range(len(root[0])):
    count[root[0][i]] += count[0]
    de.append(root[0][i])
while de:
    k = de.popleft()
    for i in range(len(root[k])):
        count[root[k][i]] += count[k]
        de.append(root[k][i])
print(jo(count))

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

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



stack = [0]
while len(stack) > 0:
    now = stack.pop()
    for i in tree[now]:
        ans[i] += ans[now]
        stack.append(i)

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

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

class Node:
    def __init__(self):
        self.count = 0
        self.check = False
        self.link = []

def main():
    
    N, Q = map(int, input().split())
    
    
    
    nodes = [Node() for i in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        nodes[a].link.append(nodes[b])
        nodes[b].link.append(nodes[a])
    for i in range(Q):
        p, x = map(int, input().split())
        p -= 1
        nodes[p].count += x
    
    q = deque()
    q.append([nodes[0], 0])
    while len(q) > 0:
        node, cost = q.popleft()
        node.check = True
        node.count += cost
        cost = node.count
        for nex in node.link:
            if not nex.check:
                q.append([nex, cost])
    
    for i in range(N-1):
        print(nodes[i].count, end=" ")
    print(nodes[-1].count)


if __name__ == "__main__":
    main()

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

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

ans = [0] * N
ans[0] = ct[0]
q = [(0, ct[0])]
while len(q):
  i, c = q.pop()
  for t in edges[i]:
    ans[t] = c + ct[t]
    q.append((t, ans[t]))
print(' '.join(map(str, ans)))

Solution (Python):

from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

def main():
    node_num, q_num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(node_num - 1)]
    q_data = [list(map(int, input().split())) for i in range(q_num)]

    graph_data = defaultdict(set)
    for i in range(node_num - 1):
        a, b = data[i]
        graph_data[a].add(b)
        graph_data[b].add(a)

    ans_list = [0 for i in range(node_num + 1)]
    for i in range(q_num):
        a, b = q_data[i]
        ans_list[a] += b

    parent_node = [0 for i in range(node_num + 1)]
    parent_node[1] = 1
    now_set = set()
    now_set.add(1)
    next_set = set()
    while len(now_set) > 0:
        for now_ele in now_set:
            for next_ele in graph_data[now_ele]:
                if parent_node[next_ele]:
                    continue
                parent_node[next_ele] = now_ele
                next_set.add(next_ele)
        now_set = next_set
        next_set = set()

    visit_flg = [0 for i in range(node_num + 1)]
    visit_flg[1] = 1
    now_set = set()
    now_set.add(1)
    next_set = set()
    while len(now_set) > 0:
        for now_ele in now_set:
            for next_ele in graph_data[now_ele]:
                if visit_flg[next_ele]:
                    continue
                visit_flg[next_ele] = 1
                ans_list[next_ele] += ans_list[now_ele]
                next_set.add(next_ele)
        now_set = next_set
        next_set = set()

    for i in range(1, node_num + 1):
        print(ans_list[i], end='')
        if i == node_num:
            print()
        else:
            print(' ',end='')





if __name__ == '__main__':
    main()

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

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

rs = [0]*N
q = [0]
while q:
  p = q.pop()
  rs[p] += xs[p]
  for c in E[p]:
    rs[c] += rs[p]
    q.append(c)
print(" ".join(map(str, rs)))

Solution (Python):
import sys
input = sys.stdin.readline
n,q=map(int,input().split())
edge=[[] for i in range(n)]
Chk=[False]*n
for i in range(n-1):
  a,b=map(int,input().split())
  edge[a-1]+=[b-1]
  edge[b-1]+=[a-1]
Ans=[0]*n
for i in range(q):
  p,x=map(int,input().split())
  Ans[p-1]+=x
Stack=[0]
Chk[0]=True
while Stack:
  c=Stack.pop(-1)
  ad=Ans[c]
  for ob in edge[c]:
    if Chk[ob]:
      continue
    else:
      Ans[ob]+=ad
      Stack+=[ob]
      Chk[ob]=True
print(*Ans)

Solution (Python):


import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def main():
    n, q = map(int, input().split())
    edge = defaultdict(list)
    counter = [0] * n

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

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

    que = deque()
    que.append(0)

    while len(que) > 0:
        node = que.popleft()
        for next in edge[node]:
            counter[next] += counter[node]
            que.append(next)

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

if __name__ == '__main__':
    main()

Solution (Python):
N, T = map(int, input().split())
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = [0]
while Q:
    i = Q.pop()
    for a in X[i][::-1]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            Q.append(a)

C = [[] for i in range(N)]
for i in range(1, N):
    C[P[i]].append(i)
CT = [0] * N
XX = []
i = 0
while i >= 0:
    if CT[i] >= len(C[i]):
        XX.append(i)
        i = P[i]
    else:
        CT[i], i = CT[i] + 1, C[i][CT[i]]

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

for i in XX[::-1]:
    if i: A[i] += A[P[i]]

print(*A)

Solution (Python):
n, q = list(map(int, input().split()))
edge = []
for i in range(n-1):
    tempa, tempb = list(map(int, input().split()))
    edge.append((tempa-1, tempb-1))

node = [0] * n

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

edge.sort()
for e in edge:
    node[e[1]] += node[e[0]]

for n in node:
    print(n)

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

def dfs(i, d):
  visited[i] = True
  ans[i] = d + pp[i]
  for j in edges[i]:
    if not visited[j]:
      dfs(j, d + pp[i])

n, q = map(int, input().split())
ans = [0 for _ in range(n)]
visited = [False for _ in range(n)]
edges = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  edges[a-1].append(b-1)
  edges[b-1].append(a-1)
pp = [0 for _ in range(n)]
for _ in range(q):
  p, x = map(int, input().split())
  pp[p-1] += x
dfs(0, 0)
print(*ans)

Solution (Python):
from collections import deque, defaultdict

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

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

q = deque()
q.appendleft((1, 1, query[1]))
ans = [0] * (N+1)

while q:
    now, prev, weight = q.pop()
    ans[now] += weight
    for e in edge[now]:
        if e == prev:
            continue
        q.appendleft((e, now, weight+query[e]))

print(*ans[1:])

Solution (Python):
from collections import deque

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

q = deque()
q.append((0, 0, -1))
while len(q):
    p, x, prev = q.pop()
    x += P[p]
    cnt[p] = x
    for b in to[p]:
        if b != prev:
            q.append((b, x, p))
print(*cnt)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


n, q = li()
edge = [list(li_()) for _ in range(n-1)]
query = [list(li()) for _ in range(q)]


num = [0]*n
for pi, xi in query:
    pi -= 1
    num[pi] += xi


tree = [[] for _ in range(n)]
for ai, bi in edge:
    tree[ai].append(bi)
    tree[bi].append(ai)



visited = [False]*n
stack = [0]
visited[0] = True

while stack:
    cur = stack.pop()
    for nex in tree[cur]:
        if visited[nex]:
            continue

        visited[nex] = True
        num[nex] += num[cur]
        stack.append(nex)


print(*num)

Solution (Python):
from collections import deque
N, Q = map(int, input().split())
G = [[] for _ in range(N)]
count = [0]*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)
for i in range(Q):
    p, x = map(int, input().split())
    count[p-1]+=x
q = deque([0])
flag = [False]*N
while q:
    v = q.pop()
    flag[v] = True
    for e in G[v]:
        if flag[e]:
            continue
        count[e]+=count[v]
        q.append(e)
print(' '.join(list(map(str, count))))

Solution (Python):
from collections import deque


n, q = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n-1)]
p = [list(map(int, input().split())) for i in range(q)]

tree = [[] for i in range(n)]
for i in range(n-1):
    tree[info[i][0] - 1].append(info[i][1] - 1)
    tree[info[i][1] - 1].append(info[i][0] - 1)

memo = {}
for i in range(n):
    memo[i] = 0
for i in range(q):
    memo[p[i][0] - 1] += p[i][1]

dp = [0] * n
visited = [False]*n
q = deque([0])
visited[0] = True
dp[0] = memo[0]

while q:
    pos = q.popleft()
    for next_pos in tree[pos]:
        if visited[next_pos]:
            continue
        visited[next_pos] = True
        dp[next_pos] += dp[pos] + memo[next_pos]
        q.append(next_pos)
print(*dp)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from itertools import *

N, Q = map(int, input().split())
es = [[] for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    es[a].append(b)
cnt = [0] * (N+1)
for i in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x
def EulerTour(v, p, c):
    for u in es[v]:
        if u != p:
            EulerTour(u, v, c+cnt[v])
    cnt[v] += c
EulerTour(1, 0, 0)
print(*cnt[1:])

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N,Q = inpl()

lines = defaultdict(set)
for _ in range(N-1):
    a,b = inpl()
    a,b = a-1,b-1
    lines[a].add(b)
    lines[b].add(a)

adds = [0]*N
for _ in range(Q):
    p,x = inpl()
    adds[p-1] += x

visited = [False]*N
ans = [0]*N

q = queue.LifoQueue()
q.put([0,0])

while not q.empty():
    x,cnt = q.get()
    visited[x] = True
    cnt += adds[x]
    ans[x] += cnt
    for t in lines[x]:
        if not visited[t]:
            q.put([t,cnt])

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

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()


n,q = na()
g = [[] for i in range(n)]
c = [0]*n

for i in range(n-1):
    a,b = na()
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for i in range(q):
    p,x = na()
    c[p-1] += x

def f(b,a,r):
    r += c[a]
    c[a] = r
    for i in g[a]:
        if i != b:
            f(a,i,r)

f(-1,0,0)
print(' '.join([str(s) for s in c]))

Solution (Python):
from collections import deque
import sys
readline = sys.stdin.readline
N, Q = map(int, readline().split())
G = [[] for i in range(N)]
C = [0] * N
for i in range(N-1):
    a, b = map(int, readline().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for i in range(Q):
    p, x = map(int, readline().split())
    C[p-1] += x
used = [0]*N
used[0] = 1
que = deque([0])
while que:
    v = que.popleft()
    c = C[v]
    for w in G[v]:
        if used[w]:
            continue
        used[w] = 1
        C[w] += c
        que.append(w)
print(*C)

Solution (Python):
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)
li = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  li[p - 1] += x
s = [0]
done = [0] * n
done[0] = 1
while s:
  d = s.pop()
  for node in g[d]:
    if done[node] == 0:
      li[node] += li[d]
      s.append(node)
      done[node] = 1
print(' '.join(map(str, li)))

Solution (Python):
import sys
        
def getpar(Edge, p):
    N = len(Edge)
    par = [0]*N
    par[0] = -1
    par[p]  -1
    stack = [p]
    visited = set([p])
    while stack:
        vn = stack.pop()
        for vf in Edge[vn]:
            if vf in visited:
                continue
            visited.add(vf)
            par[vf] = vn
            stack.append(vf)
    return par

def topological_sort_tree(E, r):
    Q = [r]
    L = []
    visited = set([r])
    while Q:
        vn = Q.pop()
        L.append(vn)
        for vf in E[vn]:
            if vf not in visited:
                visited.add(vf)
                Q.append(vf)
    return L

def getcld(p):
    res = [[] for _ in range(len(p))]
    for i, v in enumerate(p[1:], 1):
        res[v].append(i)
    return res


        

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

P = getpar(Edge, 0)
L = topological_sort_tree(Edge, 0)
C = getcld(P)

dp1 = [0]*N

for q in range(Q):
    p, x = map(int, sys.stdin.readline().split())
    dp1[p-1] += x

for l in L:
    d = dp1[l]
    for c in C[l]:
        dp1[c] += d

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

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

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

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

add_list = [0]*N

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

count_list = [0]*N
count_list[0] = add_list[0]

vi = 0 
from collections import deque
Q = deque([vi])
checked_list = [False for i in range(N)]
checked_list[vi]=True
while len(Q)>0:
    v = Q.pop()
    for v1 in e_list[v]:
        if not checked_list[v1]:
            checked_list[v1]=True
            Q.append(v1)
            count_list[v1] = count_list[v]+add_list[v1]

print(*count_list)

Solution (Python):
from collections import deque
import sys
readline = sys.stdin.readline
N, Q = map(int, readline().split())
G = [[] for i in range(N)]
C = [0] * N
for i in range(N-1):
    a, b = map(int, readline().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for i in range(Q):
    p, x = map(int, readline().split())
    C[p-1] += x
used = [0]*N
used[0] = 1
que = deque([0])
while que:
    v = que.popleft()
    c = C[v]
    for w in G[v]:
        if used[w]:
            continue
        used[w] = 1
        C[w] += c
        que.append(w)
print(*C)

Solution (Python):


from collections import defaultdict, Counter, deque
from operator import itemgetter
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
def inpl(): return list(map(int, input().split()))

N, Q = inpl()
G = [[] for _ in range(N)]

for _ in range(N-1):
    a, b = inpl()
    G[a-1].append(b-1)
    G[b-1].append(a-1)

S = [0]*N
for _ in range(Q):
    x, d = inpl()
    S[x-1] += d

searched = [0]*N
searched[0] = True
Q = deque([0])

while Q:
    p = Q.popleft()
    for q in G[p]:
        if searched[q]:
            continue
        searched[q] = True
        S[q] += S[p]
        Q.append(q)

print(*S)

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

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

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

memo = [0] * n

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

def dfs(v, par):
    for nv in g[v]:
        if nv == par:
            continue
        memo[nv] += memo[v]
        dfs(nv, v)

dfs(0, -1)

print(*memo)