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



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))
print(*Value[1:])

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

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))
print(*Value[1:])

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 


n,q = [int(i) for i in readline().split()]

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

add = [0]*n
for _ in range(q):
    p,x = [int(i) for i in readline().split()]
    add[p-1] += x

ans = [0]*n


def dfs(v,p,value): 
    value += add[v]
    ans[v] = value
    for c in g[v]:
        if c == p: continue
        dfs(c,v,value)

dfs(0,-1,0)



print(*ans)

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


q = deque([0])
visited[0] = True
while len(q):
    v = q.pop()
    for e in tree[v]:  
        if visited[e]:
            continue
        visited[e] = True
        counter[e] += counter[v]
        q.append(e)

print(*counter)

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


q = deque([0])
visited[0] = True
while len(q):
    v = q.pop()
    for e in tree[v]:  
        if visited[e]:
            continue
        visited[e] = True
        counter[e] += counter[v]
        q.append(e)

print(*counter)

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

tree_list = [input().split() for j in range(1, N)]

query_list = [input().split() for k in range(Q)]
query_list_int = [[int(k) for k in i] for i in query_list]

val_list = [0 for _ in range(N)]

linked_node_list = [[] for _ in range(N)]

for a, b in tree_list:
    a, b = int(a)-1, int(b) -1
    linked_node_list[a].append(b)
    linked_node_list[b].append(a)


for index, val in query_list_int:
    val_list[index-1] += val

stack = [0]
parent = [0] * (N+1)

while True:
    v=stack.pop()
    for child in linked_node_list[v]:
        if child != parent[v]:
            parent[child] = v
            stack.append(child)
            val_list[child] += val_list[v]
    if not stack:
        break

print(*val_list)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
n, q = map(int, input().split())
d = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    d[a].append(b)
    d[b].append(a)
d2 = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    d2[p-1] += x

ans = [0] * n

from collections import deque

def dfs(cur, pre, point):
    stack = deque([[cur, pre, point]])
    while stack:
        cur, pre, point = stack.pop()
        point += d2[cur]
        ans[cur] = point
        for e in d[cur]:
            if e != pre:
                stack.append([e, cur, point])

dfs(0, -1, 0)
print(" ".join(map(str, ans)))

Solution (Python):
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7

    n, q = list(map(int, readline().split()))
    tmd = collections.defaultdict(list)
    for i in range(n-1):
        tma, tmb = list(map(int, readline().split()))
        tmd[tma-1] += [tmb-1]
        tmd[tmb-1] += [tma-1]
    score = [0] * n
    for i in range(q):
        p, x = list(map(int, readline().split()))
        score[p-1] += x

    ql = [[0, 0]]
    ql = collections.deque(ql)
    fq = collections.defaultdict(list)
    fq[0]
    while True:
        if len(ql) != 0:
            cost, tmp = ql.popleft()
            for tmv in tmd[tmp]:
                if tmv not in fq:
                    score[tmv] += score[tmp]
                    ql.append([cost + 1, tmv])
                    fq[tmv]
        else:
            break
    print(*score)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
input=sys.stdin.readline
n,q=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(n-1)]
PX=[list(map(int,input().split())) for _ in range(q)]

es=[[] for _ in range(n)]
for a,b in AB:
  a,b=a-1,b-1
  es[a].append(b)
  es[b].append(a)
  
cnt=[0]*n
for p,x in PX:
  cnt[p-1]+=x

ans=[0]*n
ans[0]=cnt[0]
visited=[0]*n
stack=[0]
while stack:
  v=stack.pop()
  visited[v]=1
  for to in es[v]:
    if visited[to]: continue
    stack.append(to)
    ans[to]=ans[v]+cnt[to]
print(*ans)

Solution (Python):
import collections


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

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

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

    queue = collections.deque([0])
    while queue:
        q = queue.pop()
        graph[q][2] = True
        for v in graph[q][0]:
            if graph[v][2]:
                continue
            graph[v][1] += graph[q][1]
            queue.append(v)

    for i in range(n):
        print(graph[i][1], end=' ')


if __name__ == '__main__':
    main()

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

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

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

    queue = [0]
    while queue:
        q = queue.pop()
        graph[q][2] = True
        for v in graph[q][0]:
            if graph[v][2]:
                continue
            graph[v][1] += graph[q][1]
            queue.append(v)

    for i in range(n):
        print(graph[i][1], end=' ')


if __name__ == '__main__':
    main()

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

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

adjacent_list = [[] for i in range(n+1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)
tree_value = [0]*(n+1)
for i in range(q):
    p, x = map(int, input().split())
    tree_value[p] += x
searched = set()
"""
def dfs(node, count):
    searched.add(node)
    tree_value[node] += count
    for i in adjacent_list[node]:
        if i not in searched:
            dfs(i, tree_value[node])
"""
used = [0 for i in range(n+1)]
que = Queue()
que.put(1)
while que.qsize() != 0:
    m = que.get()
    used[m] = 1
    for i in adjacent_list[m]:
        if not used[i]:
            tree_value[i] += tree_value[m]
            que.put(i)
for i in range(1,n+1):
    print("{} ".format(tree_value[i]), end = "")

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

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

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


stack = [1]

parent = [0] * (N+1)



while stack:
    
    x = stack.pop()
    
    for y in graph[x]:
        
        if y == parent[x]:
            
            pass
        
        else:
            
            parent[y] = x
            
            stack.append(y)
            
            val[y] += val[x]

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

Solution (Python):

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

graph = [[] for _ in range(N+1)]

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

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

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

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

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

N,Q = map(int,readline().split())
ABPX = list(map(int,read().split()))
AB = iter(ABPX[:N+N-2])
PX = iter(ABPX[N+N-2:])

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

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

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

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

Solution (Python):
from collections import deque
N,Q=map(int,input().split())
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(*count)

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)
ans = ' '.join(map(str,value[1:]))
print(ans)

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

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

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

count = [0 for i in range(N + 1)]
visited = [False for i in range(N+1)]

stack = [(1, 0)]
while stack:
    root, curCount = stack.pop()
    
    visited[root] = True
    curCount += tally[root]
    count[root] = curCount
    for neighbour in edges[root]:
        if visited[neighbour]: continue
        stack.append((neighbour, curCount))

print(*count[1:])

Solution (Python):
N,Q = map(int,input().split())
G = [[]for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
C = [0]*(N)
for i in range(Q):
    p,x = map(int,input().split())
    C[p-1] += x
from collections import deque
d=deque()
d.append(0)
t = [False]*(N)
t[0] = True
while d:
    a = d.popleft()
    for b in G[a]:
        if not t[b]:
            t[b] = True
            d.append(b)
            C[b] += C[a]
print(*C)

Solution (Python):
from collections import deque

n, q = map(int,input().split())
g = [[] for _ in range(n)] 
count = [0 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())
    count[p-1] += x

que = deque()
que.append(0)

watch = [False] * n
watch[0] = True

while que:
    i = que.popleft()
    watch[i] = True
    for e in g[i]:
        if watch[e] == True: continue
        count[e] += count[i]
        que.append(e)

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

Solution (Python):
N, Q = map(int, input().split())
tree = {}
ans = []
ans.extend([0 for _ in range(N)])

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

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

sta = [(0, 0)]
while sta:
    k, p = sta.pop()
    for c in tree.get(k, []):
        if c == p:
            continue
        ans[c] += ans[k]
        sta.append((c, k))

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

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

N, Q = map(int ,input().split())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
	at,bt = map(int ,input().split())
	E[at].append(bt)
	E[bt].append(at)
cnt = [0 for _ in range(N+1)]
for _ in range(Q):
	pt,xt = map(int, input().split())
	cnt[pt] += xt

flg = [0 for _ in range(N+1)]
q = [[1,-1]]
while q:
	cp, root = q.pop()
	for np in E[cp]:
		if np == root:
			continue
		cnt[np] += cnt[cp]
		q.append([np,cp])

cnt.pop(0)
for c in cnt:
	print(c, end = " ")
print()

Solution (Python):
from collections import deque

N,Q=map(int,input().split())
out=[set() for i in range(N+1)];out[0]=None 
for j in range(N-1):
    a,b=map(int,input().split())
    out[a].add(b)
    out[b].add(a)


a=[0 for i in range(N+1)];a[0]=None

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



q=deque()
q.append(1)
while q:
    u=q.popleft()
    for v in out[u]:
        a[v]+=a[u]
        q.append(v)
        out[v].remove(u)
        
    out[u].clear()

print(*a[1:])

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

    def bfs(s):
        G_color = ['WHITE' for _ in range(len(G))]
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    cost[v] += cost[u]
                    Q.append(v)
            G_color[u] = 'BLACK'

    bfs(0)
    print(*cost)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)



INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, q = LI()
G = [[] for _ in range(n)]
for a, b in LIR(n - 1):
    G[a - 1] += [b - 1]
    G[b - 1] += [a - 1]

D = [0] * n
for p, x in LIR(q):
    D[p - 1] += x


que = deque([0])
visited = [0] * n
visited[0] = 1
while que:
    u = que.popleft()
    for v in G[u]:
        if visited[v] == 0:
            visited[v] = 1
            D[v] += D[u]
            que += [v]


print(*D)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)



INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, q = LI()
G = [[] for _ in range(n)]
for a, b in LIR(n - 1):
    G[a - 1] += [b - 1]
    G[b - 1] += [a - 1]

D = [0] * n
for p, x in LIR(q):
    D[p - 1] += x


que = [0]
visited = [0] * n
visited[0] = 1
while que:
    u = que.pop()
    for v in G[u]:
        if visited[v] == 0:
            visited[v] = 1
            D[v] += D[u]
            que += [v]


print(*D)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)



INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, q = LI()
G = [[] for _ in range(n)]
for a, b in LIR(n - 1):
    G[a - 1] += [b - 1]
    G[b - 1] += [a - 1]

D = [0] * n
for p, x in LIR(q):
    D[p - 1] += x


que = [0]
par = [-1] * n
par[0] = 0
while que:
    u = que.pop()
    for v in G[u]:
        if par[v] == -1:
            par[v] = u
            D[v] += D[u]
            que += [v]


print(*D)

Solution (Python):

import sys














def main():
    n, q = [int(x) for x in sys.stdin.readline().split()]
    AB = [[] for _ in range(n+1)]
    for _ in range(n-1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        AB[a].append(b)
        AB[b].append(a)
    

    ans = [0 for _ in range(n+1)]
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        ans[p] += x
    
    stack = [1]
    parent = [0 for _ in range(n+1)]
    while stack:
        x = stack.pop()
        for y in AB[x]:
            if y != parent[x]: 
                parent[y] = x 
                stack.append(y)
                ans[y] += ans[x]
 
    
    print(' '.join(map(str, ans[1:])))

if __name__ == "__main__":
    main()

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

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

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

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

def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        else:
            ans[u] += ans[v]
            dfs(u, v)

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

Solution (Python):
N,Q = map(int,input().split())
G = [[]for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
C = [0]*(N)
for i in range(Q):
    p,x = map(int,input().split())
    C[p-1] += x
from collections import deque
d=deque()
d.append(0)
t = [False]*(N)
t[0] = True
while d:
    a = d.popleft()
    for b in G[a]:
        if not t[b]:
            t[b] = True
            d.append(b)
            C[b] += C[a]
print(*C)

Solution (Python):
N, Q = map(int, input().split())
g = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)
counter = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    counter[p] += x
used = [False] * N
stack = [0]
used[0] = True
while stack:
    a = stack.pop()
    for b in g[a]:
        if not used[b]:
            used[b] = True
            counter[b] += counter[a]
            stack.append(b)
print(" ".join(map(str, counter)))

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

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

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

unused = [1]*n
que = Queue()
que.put(0)
while que.qsize():
    m = que.get()
    unused[m] = 0
    for v in link[m]:
        if unused[v]:
            weight[v] += weight[m]
            que.put(v)
print(*weight)

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

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

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


def my_add(v,par):
    for vn in g[v]:
        if vn==par:
            continue
        res[vn]+=res[v]
        my_add(vn,v)

my_add(0,-1)
print(*res)

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

def main():
    n, q = map(int, input().split())
    g = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        g[a].append(b); g[b].append(a)
    node = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        node[p] += x

    def dfs(v, p=-1):
        for u in g[v]:
            if u==p:
                continue
            node[u] += node[v]
            dfs(u, v)
    dfs(0)
    print(*node)

if __name__ == "__main__":
    main()

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

n, q = map(int, input().split())
g = [list() for i in range(n)]
node = [0] * 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)

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

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

dfs(0)
print(*node)

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

n, q = map(int, input().split())
g = [list() for i in range(n)]
node = [0] * 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)

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

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

dfs(0)
print(*node)

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

readline = stdin.readline

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

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

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


    
    
    
    que = deque([[0, -1]])

    while len(que) > 0:
        node, parent = que.pop()

        
        for child in tree[node]:
            
            if child == parent:
                continue
            
            else:
                cou[child] += cou[node]
                
                que.appendleft([child, node])
    
    
    print(*cou)


if __name__ == '__main__':
    main()

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

n, q = map(int, input().split())
vals = [0] * n
node_idxes = [[] for _ in range(n)]

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

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

def dfs(chi_idx, parent):
	for child in node_idxes[chi_idx]:
		if child == parent:
			continue
		vals[child] += vals[chi_idx]
		dfs(child, chi_idx)

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

Solution (Python):
from sys import stdin
import sys
from itertools import groupby
sys.setrecursionlimit(10**6) 
n,q = [int(x) for x in stdin.readline().rstrip().split()]
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n-1)]
lin = [list(map(int,stdin.readline().rstrip().split())) for _ in range(q)]

kazu = [[]for _ in range(n)]
point = [0]*n

for i in range(n-1):
    kazu[li[i][0]-1].append(li[i][1]-1)
    kazu[li[i][1]-1].append(li[i][0]-1)
for i in range(q):
    point[lin[i][0]-1] += lin[i][1]

def dfs(now,before):
    for i in (kazu[now]):
        if i == before:
            continue
        point[i] += point[now]
        dfs(i,now)
dfs(0,-1)
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):
from sys import stdin
import sys
from itertools import groupby
sys.setrecursionlimit(10**6) 
n,q = [int(x) for x in stdin.readline().rstrip().split()]
li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n-1)]
lin = [list(map(int,stdin.readline().rstrip().split())) for _ in range(q)]

kazu = [[]for _ in range(n)]
point = [0]*n

for i in range(len(li)):
    kazu[li[i][0]-1].append(li[i][1]-1)
    kazu[li[i][1]-1].append(li[i][0]-1)
for i in range(q):
    point[lin[i][0]-1] += lin[i][1]

def dfs(li,li2,now,before):
    for i in (li[now]):
        if i == before:
            continue
        li2[i] += li2[now]
        dfs(kazu,point,i,now)
dfs(kazu,point,0,-1)
print(*point)

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

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

q = deque([0])
judge = [False]*N
while q:
    root = q.pop()
    judge[root] = True
    for i in tre[root]:
        if judge[i]:
            continue
        cnt[i] += cnt[root]
        q.append(i)

print(*cnt)

Solution (Python):
N,Q = map(int, input().split())
ki = [[] for i in range(N+1)]
val = [0 for _ in range(N+1)]

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

q = [(1,0)]
while q:
  c,p = q.pop()
  val[c] += val[p]
  for x in ki[c]:
    if x==p:
      continue
    q.append((x,c))
print(' '.join(map(str, val[1:])))

Solution (Python):
import sys

sys.setrecursionlimit(1000000)


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


n, q = [int(i) for i in input().split()]
ab = [[] for _ in range(n)]
for _ in range(n - 1):
    i, j = [int(i)-1 for i in input().split()]
    ab[i].append(j)
    ab[j].append(i)
p = [0]*n
for _ in range(q):
    i, j = [int(i) for i in input().split()]
    p[i-1] += j

al = [False]*n
pp = [0]*n


def a(i=0, j=0):
    al[i] = True
    j += p[i]
    pp[i] = str(j)
    for k in ab[i]:
        if not al[k]:
            a(k, j)


a()
print(" ".join(pp))