Problem:
 Score : 400 points    Problem Statement Given is a rooted tree with N vertices numbered 1 to N .
The root is Vertex 1 , and the i -th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i .  Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0 .  Now, the following Q operations will be performed:   Operation j  (1 \leq j \leq Q) : Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j .   Find the value of the counter on each vertex after all operations.      Constraints  2 \leq N \leq 2 \times 10^5  1 \leq Q \leq 2 \times 10^5  1 \leq a_i < b_i \leq N  1 \leq p_j \leq N  1 \leq x_j \leq 10^4  The given graph is a tree.  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  Q  a_1  b_1  :  a_{N-1}  b_{N-1}  p_1  x_1  :  p_Q  x_Q       Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.        Sample Input 1 4 3
1 2
2 3
2 4
2 10
1 100
3 1      Sample Output 1 100 110 111 110  The tree in this input is as follows:   Each operation changes the values of the counters on the vertices as follows:   Operation 1 : Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2 , that is, Vertex 2, 3, 4 . The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10 , respectively.  Operation 2 : Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1 , that is, Vertex 1, 2, 3, 4 . The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110 , respectively.  Operation 3 : Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3 , that is, Vertex 3 . The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110 , respectively.        Sample Input 2 6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10      Sample Output 2 20 20 20 20 20 20

Solution (Python):
def main():
  N, Q = map(int, input().split())
  
  
  graph = [[] for i in range(N)]
  ans = [0 for i in range(N)]
  import sys
  
  sys.setrecursionlimit(10**7)
  
  readline = sys.stdin.readline


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

      graph[a].append(b)
      graph[b].append(a)

  for _ in range(Q):
      
      p, x = map(int, readline().split())
      p -= 1

      ans[p] += x

  def dfs(node, frm=-1):
      for to in graph[node]:
          if to == frm:
              continue
          ans[to] += ans[node]
          dfs(to, node)
  dfs(0)

  
  print(*ans)

if __name__ == '__main__':
  main()

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

def addc(p,b):
  for i in e[p]:
    if(i == b):
      continue
    c[i] += c[p]
    addc(i,p)

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

e = [ [] for i in range(n + 1)]
c = [0]*(n+1)

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

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

addc(1,-1)

print(*c[1:])

Solution (Python):
def main():
  N, Q = map(int, input().split())
  graph = [[] for i in range(200010)]
  ans = [0 for i in range(200010)]
  import sys
  
  sys.setrecursionlimit(10**7)
  
  readline = sys.stdin.readline


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

      graph[a].append(b)
      graph[b].append(a)

  for _ in range(Q):
      
      p, x = map(int, readline().split())
      p -= 1

      ans[p] += x

  def dfs(node, frm=-1):
      for to in graph[node]:
          if to == frm:
              continue
          ans[to] += ans[node]
          dfs(to, node)
  dfs(0)

  print(*ans[:N])

if __name__ == '__main__':
  main()

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

def rec(p, parent, x):
    x += xl[p]
    ans[p] = x
    for num in tl[p]:
        if num == parent: continue
        rec(num, p, x)

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

Solution (Python):
N, Q = map(int, input().split())
graph = [[] for i in range(200010)]
ans = [0 for i in range(200010)]
import sys

sys.setrecursionlimit(10**7)

readline = sys.stdin.readline


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

    graph[a].append(b)
    graph[b].append(a)

for _ in range(Q):
    
    p, x = map(int, readline().split())
    p -= 1

    ans[p] += x

def dfs(node, frm=-1):
    for to in graph[node]:
        if to == frm:
            continue
        ans[to] += ans[node]
        dfs(to, node)
dfs(0)

print(*ans[:N])

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

N, Q = map(int, input().split())
tree = [[] for _ in range(N+1)]
value = [0] * (N+1)
visit = [False] * (N+1)


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


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


q = queue.Queue()
q.put(1)
visit[1] = True
while not q.empty():
    now = q.get()
    for next in tree[now]:
        if not visit[next]:
            value[next] += value[now]
            q.put(next)
            visit[next] = True


s = " ".join(map(str, value[1:]))
print(s)

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

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

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

def rec(node, parent):
    for i in root[node]:
        if i == parent:
            continue
        cnt[i] += cnt[node]
        rec(i, node)

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

Solution (Python):

import bisect
import collections
import itertools


def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

class Graph:
    def __init__(self, node_count, edge_count):
        self.node_count = node_count
        self.lst = [-1 for _ in range(node_count)]
        self.nxt = [0 for _ in range(edge_count)]
        self.to = [0 for _ in range(edge_count)]
        self.m = 0

    def add_edge(self, u, v, w):  
        m = self.m
        self.nxt[m], self.lst[u], self.to[m] = self.lst[u], m, (v, w)
        self.m += 1

    def traverse(self, u):
        m = self.lst[u]
        while m != -1:
            yield self.to[m]
            m = self.nxt[m]


n,q=getints()
values = [0]*n
gr = Graph(n,2*n)

for _ in range(n-1):
    a,b=getints()
    a,b=a-1,b-1
    gr.add_edge(a,b,1)
    gr.add_edge(b,a,1)

for _ in range(q):
    u, p=getints()
    values[u-1] += p

def distribute(u, par, prev):
    stk = []
    stk.append((u, par, 0))
    while stk:
        u,par,prev=stk[-1]
        stk.pop()
        values[u] += prev
        for v, _ in gr.traverse(u):
            if v == par: continue
            stk.append((v,u,values[u]))

distribute(0, -1, 0)
print(" ".join(map(str, values)))

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
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
N,Q = map(int,input().split())
ABList = [list(map(int,list(input().split()))) for i in range(N-1)]  
PXList = [list(map(int,list(input().split()))) for i in range(Q)]  
 

graph=[[] for _ in range(N+1)]
values=[0]*(N+1)
 
for a,b in ABList:
    graph[a].append(b)
    graph[b].append(a)
    
    
    
for p,x in PXList:
    values[p]+=x
    
    
 
def dfs(x,parent,add):
    
    values[x]+=add
    for i in graph[x]:
        
        
        if i==parent:
            continue
        dfs(i,x,values[x])
 
dfs(1,0,0)
 
print(' '.join(map(str,values[1:])))

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

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

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

    parent_nodes = deque()
    parent_nodes.append(0)
    visited_nodes = set()
    while parent_nodes:
        parent = parent_nodes.popleft()
        if parent in visited_nodes:
            continue
        visited_nodes.add(parent)
        for child in edges[parent]:
            if child in visited_nodes:
                continue
            counts[child] += counts[parent]
            parent_nodes.append(child)

    print(' '.join(list(map(str, counts))))

if __name__ == "__main__":
    main()

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

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

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

    parent_nodes = deque()
    parent_nodes.append(0)
    visited_nodes = set()
    while parent_nodes:
        parent = parent_nodes.popleft()
        if parent in visited_nodes:
            continue
        visited_nodes.add(parent)
        for child in edges[parent]:
            if child in visited_nodes:
                continue
            counts[child] += counts[parent]
            parent_nodes.append(child)

    print(' '.join(list(map(str, counts))))

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200005)
n, q = map(int, input().split())

to = [[] for i in range(n+1)]


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

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


def down_tree(index, parent):
    for i in to[index]:
        if i == parent:
            continue
        score[i] += score[index]
        down_tree(i, index)



down_tree(1, 1)

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

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())
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)

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



def dfs(pre,now,w):
    ans[now] += w
    for i in tree[now]:
        if i != pre:
            dfs(now,i,ans[now])

dfs(-1,0,0)

print(*ans)

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

ct = [0] * (n+1)
o = [0] * (n+1)
g = [[] for _ in range(n+1)]

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

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

todo = [1]
seen = [False] * (n+1)
while todo:
  p = todo.pop(-1)
  ct[p] += o[p]
  seen[p] = True
  for i in g[p]:
    if seen[i]:
      ct[p] += ct[i]
    else:
      todo.append(i)

print(*ct[1:])

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

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

for _ in range(N-1):
    a,b=map(int,input().split())
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    px = list(map(int,input().split()))
    ans[px[0]] += px[1]

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

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

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

ct = [0] * (n+1)
o = [0] * (n+1)
g = [[] for _ in range(n+1)]

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

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

todo = [1]
seen = [False] * (n+1)
while todo:
  p = todo.pop(-1)
  ct[p] += o[p]
  seen[p] = True
  for i in g[p]:
    if seen[i]:
      ct[p] += ct[i]
    else:
      todo.append(i)

print(*ct[1:])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    ans[now]+=(h[now]+ans[prev])
    
    for next in g[now]:
        if next != prev:
            dfs(next, now)
 
 
n, q = map(int, input().split())
g = [[] * n for i in range(n)]
for i in range(n-1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
 
h=[0]*n
for i in range(q):
    u, v = map(int, input().split())
    u -= 1
    h[u]+=v
 
ans=[0]*n
dfs(0,0)
 
print(' '.join(map(str, ans)))

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

def dfs(now, prev):
    ans[now]+=(h[now]+ans[prev])
    
    for next in g[now]:
        if next != prev:
            if memo[next]==True:
                
                return False
            else:
                memo[next] = True
                dfs(next, now)
 

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

h=[0]*n
for i in range(q):
    u, v = map(int, input().split())
    u -= 1
    h[u]+=v
 

ans=[0]*n
memo = [False for i in range(n)]
 

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

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

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

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

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

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




visited = [False] * (N + 1)
visited[1] = True
ans = [0] * (N + 1)

def dfs(fro, p):
    visited[fro] = True
    now = counter[fro] + p
    for to in edge[fro]:
        if not visited[to]:
            dfs(to, now)
    ans[fro] = now
    return

dfs(1, 0)

print(*ans[1:])

Solution (Python):
from collections import deque
N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
operations = [list(map(int, input().split())) for _ in range(Q)]

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

for edge in edges:
    a = edge[0]
    b = edge[1]
    edge_map[a-1].append(b-1)
    edge_map[b-1].append(a-1)


counters = [0]*N

for operation in operations:
    p = operation[0]
    x = operation[1]
    counters[p-1] += x


res_list = [counters[i] for i in range(N)]
calculated = [False for i in range(N)]

target = [0]
while len(target) > 0:
    t = target.pop()
    calculated[t] = True
    for child in edge_map[t]:
        if calculated[child]:
            continue
        target.append(child)
        res_list[child] += res_list[t]

print(*res_list)

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

ct = [0] * (n + 1)
e = [[] for _ in range(n + 1)]

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

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

target = [1]
visited = [0] * (n + 1)
while len(target):
    t = target.pop()
    visited[t] = 1
    ct[t] += qs[t]
    for nt in e[t]:
        if visited[nt] == 0:
            target.append(nt)
            ct[nt] += ct[t]

print(*ct[1:])

Solution (Python):
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)]
 
l = [[] for i in range(n+1)]
for a, b in ab:  
    l[a].append(b)
    l[b].append(a)

value = [0 for i in range(n+1)]
for p, x in px:
    value[p] += x

stack = [1]
flag = [False]*(n+1)
while stack:
    v = stack.pop()
    flag[v] = True
    for e in l[v]:
        if flag[e]:
            continue
        value[e] += value[v]
        stack.append(e)
        
print(*value[1:])

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

ct = [0] * (n + 1)
e = [[] for _ in range(n + 1)]

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

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

target = [1]
visited = set()
while len(target):
    t = target.pop()
    visited.add(t)
    ct[t] += qs[t]
    for nt in e[t]:
        if nt not in visited:
            target.append(nt)
            ct[nt] += ct[t]

print(*ct[1:])

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


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

for _ in range(N-1):
  parent,child = map(int,input().split())
  trees[parent].append(child)
  trees[child].append(parent)

for _ in range(Q):
  node_num,value = map(int,input().split())
  values[node_num] += value

def recursive_func(i,parent):
  for child in trees[i]:
    if child == parent:
      continue
    values[child] += values[i]
    recursive_func(child,i)

recursive_func(1,-1)
print(*values[1:])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())

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

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


def dfs2(i, parent):
    for j in trees[i]:
        if j == parent: continue
        values[j] += values[i]
        dfs2(j, i)

dfs2(1, -1)
print(*values[1:])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    ans[now]+=(h[now]+ans[prev])
    
    for next in g[now]:
      	if next == prev:
        	continue
      	else:
          dfs(next, now)
 

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


h=[0]*n
for i in range(q):
    u, v = map(int, input().split())
    u -= 1
    h[u]+=v


ans=[0]*n


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

Solution (Python):
from pprint import pprint
from collections import deque, defaultdict
 
import sys
 

n, q = map(int, sys.stdin.readline().strip().split(" "))
edges = [[] for _ in range(n)]
for _ in range(n-1):
    
    a_i, b_i = map(int, sys.stdin.readline().strip().split(" "))
    edges[a_i-1].append(b_i-1)
    edges[b_i-1].append(a_i-1)
 
counter = [0] * n
p = [0] * n
for _ in range(q):
    
    p_i, x_i = map(int, sys.stdin.readline().strip().split(" "))
    p[p_i-1] += x_i
    
 
 
parents = deque()
parents.append(0)
visited = set()
while parents:
    parent = parents.popleft()
    if parent in visited:
        continue
    counter[parent] += p[parent]
    visited.add(parent)
    for child in edges[parent]:
        if child in visited:
            continue
        counter[child] += counter[parent]
        
        
        parents.append(child)
 
print(" ".join(list(map(str, counter))))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())

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

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

def dfs(i, parent):
    for j in trees[i]:
        if j == parent: continue
        values[j] += values[i]
        dfs(j, i)

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())

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

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


def dfs2(i, parent):
    for j in trees[i]:
        if j == parent: continue
        values[j] += values[i]
        dfs2(j, i)

dfs2(0, -1)
print(*values)

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


N,Q=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(N-1)]
PLUS=[list(map(int,input().split())) for i in range(Q)]

SCORE=[0]*(N+1)
for p,x in PLUS:
    SCORE[p]+=x

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

for x,y in EDGE:
    ELIST[x].append(y)
    ELIST[y].append(x)


from collections import deque
Q=deque([1])

USED=[0]*(N+1)
USED[1]=1




while Q:
    x=Q.pop()

    for to in ELIST[x]:
        if USED[to]==0:
            USED[to]=1
            SCORE[to]+=SCORE[x]
            Q.append(to)

print(*SCORE[1:])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())

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

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


def dfs2(i, parent):
    for j in trees[i]:
        if j == parent: continue
        values[j] += values[i]
        dfs2(j, i)

dfs2(1, -1)
print(*values[1:])

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

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

nodes = [[] for _ in range(N)]
values = [0 for _ in range(N)]

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

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

def dfs(i, p):
  for c in nodes[i]:
    if c != p:
      values[c] += values[i]
      dfs(c, i)

dfs(0, None)
print(*values)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())

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

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


def dfs2(i, parent):
    for j in trees[i]:
        if j == parent: continue
        values[j] += values[i]
        dfs2(j, i)

dfs2(1, -1)
print(*values[1:])

Solution (Python):
n, q = [int(x) for x in input().split()]
vs   = []
	
for _ in range(0, n + 1):
	vs.append([])
	
xs    = [0]     * (n + 1)
fs    = [False] * (n + 1)
us    = [0]     * n
fs[1] = True
us[0] = 1
ui    = 0

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

for _ in range(0, q):
	p, x   = [int(x) for x in input().split()]
	xs[p] += x
	
for i in range(0, n):
	x = xs[us[i]]
	for v in vs[us[i]]:
		if fs[v] == False:
			xs[v] += x
			ui    += 1
			us[ui] = v
			fs[v]  = True

print(" ".join(str(x) for x in xs[1:]))

Solution (Python):
import sys
input=sys.stdin.readline
n,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n-1)]
c=[[] for i in range(n)]
for i in range(n-1):
  c[l[i][0]-1].append(l[i][1]-1)
  c[l[i][1]-1].append(l[i][0]-1)

l2=[list(map(int,input().split())) for i in range(q)]
L4=[0]*n
for i in range(q):
  L4[l2[i][0]-1]+=l2[i][1]
ans=[0]*n
ans[0]=L4[0]

L=[0]
L2=[]
L22=[-1]*n
L3=[]
while len(L2)!=n and len(L)!=0:
  for j in range(len(L)):
    x=L[j]
    L22[x]=1
    for k in range(len(c[x])):
      if L22[c[x][k]]==-1:
        ans[c[x][k]]=ans[x]+L4[c[x][k]]
        L3.append(c[x][k])
  L=L3
  L3=[]
for i in range(n):
  print(ans[i])

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
 
n,q = map(int, input().split())

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

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

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

st = [(0, 0, -1)] 
 




while st:
    
    l = st.pop()  
    ans[l[0]] = l[1] + queries[l[0]]
    for v in adj[l[0]]:
        if v != l[2]:
            st.append((v, ans[l[0]], l[0]))
 
print(*ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())
 
ki = [[] for i in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    ki[a].append(b)
    ki[b].append(a)

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

 
 
def d(y, parent):
    for i in ki[y]:
        if i == parent:
            continue
        cnt[i] += cnt[y]
        d(i, y)
 
 
d(1, -1)
print(*cnt[1:])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())

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


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





def d(y, parent):
    for i in ki[y]:
        if i == parent:
            continue
        cnt[i] += cnt[y]
        d(i, y)


d(1, -1)
print(*cnt[1:])

Solution (Python):
import sys
入力 = sys.stdin.readline
範囲, 出力 = range, print
真, 偽 = True, False
何もしない = None
一行に複数個の入力 = lambda:map(int, 入力().split())
頂点の数, 操作回数 = 一行に複数個の入力()
辺 = [[] for _ in 範囲(頂点の数)]
カウンター = [0] * 頂点の数
辺の数 = 頂点の数-1
for _ in 範囲(辺の数):
    頂点1, 頂点2 = 一行に複数個の入力()
    頂点1 -= 1
    頂点2 -= 1
    辺[頂点1] += 頂点2,
    辺[頂点2] += 頂点1,
for _ in 範囲(操作回数):
    頂点, 足す値 = 一行に複数個の入力()
    頂点 -= 1
    カウンター[頂点] += 足す値
到達済み = [偽] * 頂点の数
親 = 0
到達済み[親] = 真
スタック = [親]
while スタック:
    親 = スタック.pop()
    for 子 in 辺[親]:
        if 到達済み[子]:
            何もしない
        else:
            到達済み[子] = 真
            カウンター[子] += カウンター[親]
            スタック += 子,
出力(*カウンター)

Solution (Python):
MAX = 200005
G = [[] for i in range(MAX)]
adds = [0] * MAX
ans = [0] * MAX








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


stack = [(0, -1, adds[0])]
while len(stack) != 0:
    cur, par, count = stack.pop()
    ans[cur] += count
    for i in range(len(G[cur])):
        if G[cur][i] != par:
            stack.append((G[cur][i], cur, count + adds[G[cur][i]]))
            
for i in range(n):
    print(ans[i])

Solution (Python):
import sys

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

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

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

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

Q = [0]

while Q:
    current = Q.pop()

    for i in M[current]:
        ans[i] += ans[current]
        M[i].remove(current)

    Q += M[current]

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

Solution (Python):
import sys
input=sys.stdin.readline
n,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n-1)]
l2=[list(map(int,input().split())) for i in range(q)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)
L4=[0 for i in range(n)]
for i in range(q):
  L4[l2[i][0]-1]+=l2[i][1]
distance=[0 for i in range(n)]
distance[0]=L4[0]

L=[0]
L2=[]
L22=[-1]*n
L3=[]
while len(L2)!=n and len(L)!=0:
  for j in range(len(L)):
    L22[L[j]]=1
    for k in range(len(connection[L[j]])):
      if L22[connection[L[j]][k]]==-1:
        distance[connection[L[j]][k]]=distance[L[j]]+L4[connection[L[j]][k]]
        L3.append(connection[L[j]][k])
  L=L3
  L3=[]
for i in range(len(distance)):
  print(distance[i])

Solution (Python):
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())

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

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

ans = [0]*N
ans[0] = xs[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] + xs[to]

print(*ans)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

ans = [0]*n
for _ in range(q):
    p,x = LI()
    ans[p-1] += x

def dfs(start,par):
    for v in g[start]:
        if v != par:
            ans[v] += ans[start]
            dfs(v,start)
dfs(0,-1)
print(*ans)