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):
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
def main():
  a,b=map(int,input().split())
  e=[[]for i in range(a)]

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

  g=[[]for i in range(a)]
  f=deque()
  f.append((0,-1))
  ans=[0 for _ in range(a)]
  for i in range(b):
    ten,p=map(int,input().split())
    ans[ten-1]+=p
  i=0
  while len(f)!=0:
    i,fr=f.popleft()
    for j in e[i]:
      if j==fr:
        continue
      else:
        ans[j]+=ans[i]
        f.append([j,i]) 
  print(*ans)
  
main()

Solution (Python):
from collections import deque
a,b=map(int,input().split())
e=[[]for i in range(a)]

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

g=[[]for i in range(a)]
f=deque()
f.append((0,-1))
ans=[0 for _ in range(a)]
for i in range(b):
  ten,p=map(int,input().split())
  ans[ten-1]+=p
i=0
while len(f)!=0:
  i,fr=f.popleft()
  for j in e[i]:
    if j==fr:
      continue
    else:
      ans[j]+=ans[i]
      f.append([j,i]) 
print(*ans)

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
input=sys.stdin.readline
sys.setrecursionlimit(1000000)

n,q=map(int,input().split())
g=[[] for _ in range(n+1)]

for _ in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)
c=[0]*(n+1)
que=[1]
done=[0]*(n+1)
done[1]=1
for _ in range(q):
    p,x=map(int,input().split())
    c[p]+=x
while que:
    d=que.pop()
    for node in g[d]:
        if not done[node]:
            c[node]+=c[d]
            que.append(node)
            done[node]+=1
print(*c[1:])

Solution (Python):
import sys
import queue
input = sys.stdin.readline
N, Q = map(int, input().split())
 
tree = [[] for i in range(N+1)]
values = [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())
    values[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]:
            values[next] += values[now]
            visit[next] = True
            q.put(next)
 
values = values[1:]
print(" ".join(map(str, values)))

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):
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):
from collections import deque
def main():
  _map, _input, _int = map, input, int
  n, q = _map(_int, _input().split())
  values = [0] * n
  links = [[] for _ in range(n)]
  for _ in range(n - 1):
    a, b = _map(_int, _input().split())
    if a >= b:
      return
    links[a - 1].append(b - 1)
    links[b - 1].append(a - 1)
  for _ in range(q):
    p, x = _map(_int, _input().split())
    values[p - 1] += x
  d = deque()
  d.append((0, -1))
  while len(d) != 0:
    i, p = d.popleft()
    for j in links[i]:
      if j == p:
        continue
      values[j] += values[i]
      d.append((j, i))
  print(*values)
main()

Solution (Python):
import sys

sys.setrecursionlimit(10**6)

stdin = sys.stdin
na = lambda: map(int, stdin.readline().split())

g = []
counter = []
ans = []

def dfs(cur, par, val):
  global counter, ans, g
  val += counter[cur]
  ans[cur] = val
  for e in g[cur]:
    if e == par:
      continue
    dfs(e, cur, val)

def main():
  global g, ans, counter
  N, Q = na()
  
  g = [[] for _ in range(N)]
  for _ in range(N - 1):
    a, b = na()
    g[a-1] += [b-1]
    g[b-1] += [a-1]

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

  ans = [0] * N
  dfs(0, -1, 0)
  print(*ans)

if __name__ == "__main__":
  main()

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(" "))
    counter[p_i-1] += x_i


parents = deque()
parents.append(0)
visited = set()
while parents:
    parent = parents.popleft()
    if parent in visited:
        continue
    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):
from pprint import pprint
from collections import deque, defaultdict

n, q = map(int, input().strip().split(" "))
edges = [[] for _ in range(n)]
for _ in range(n-1):
    a_i, b_i = map(int, input().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, input().strip().split(" "))
    counter[p_i-1] += x_i


parents = deque()
parents.append(0)
visited = set()
while parents:
    parent = parents.popleft()
    if parent in visited:
        continue
    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):
from collections import deque
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
v = [0]*(n+1)
ans = [0]*(n+1)
for _ in range(n-1):
	a,b = map(int,input().split())
	g[a].append(b)
	g[b].append(a)
for _ in range(q):
	p,x = map(int,input().split())
	v[p] += x
alr = [False]*(n+1)
ans[1] = v[1]
stk = deque()
stk.append(1)
while stk:
	cur = stk.pop()
	alr[cur] = True
	for nxt in g[cur]:
		if not alr[nxt]:
			ans[nxt] = ans[cur] + v[nxt]
			stk.append(nxt)
print(*ans[1:])

Solution (Python):
from pprint import pprint
from collections import deque, defaultdict
 
n, q = list(map(int, input().strip().split(" ")))

edges = [[] for _ in range(n)]
for _ in range(n-1):
    a_i, b_i = list(map(int, input().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 = list(map(int, input().strip().split(" ")))
    
    counter[p_i-1] += x_i
 
 
parents = deque()


parents.append(0)
visited = set()
while parents:
    parent = parents.popleft()
    
    if parent in visited:
        continue
    visited.add(parent)
    
    
    
    for child in edges[parent]:
        if child in visited:
            continue
        
        
        counter[child] += counter[parent]
        
        
        parents.append(child)
        
 


print(*counter)

Solution (Python):

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

def dfs(v, p, tree, cnt):
    for u in tree[v]:
        if u != p:
            cnt[u] += cnt[v]
            dfs(u, v, tree, cnt)


def main():
    
    N, Q = tuple(map(int, input().split()))
    A = tuple(tuple(map(int, input().split())) for _ in range(N - 1))
    P = tuple(tuple(map(int, input().split())) for _ in range(Q))
    T = [[] for _ in range(N + 1)]
    cnt = [0] * (N + 1)
    for a, b in A:  
        T[a] += [b]
        T[b] += [a]
    for p, x in P:  
        cnt[p] += x
    dfs(1, -1, T, cnt)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Solution (Python):
from pprint import pprint
from collections import deque

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(" "))
    counter[p_i-1] += x_i


parents = deque()
parents.append(0)
visited = set()
while parents:
    parent = parents.popleft()
    if parent in visited:
        continue
    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(10 ** 7)
 

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

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

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

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







def dfs(nownode,oya=-1):
    for nextnode in T[nownode]:
        if oya == nextnode:
            continue
        X[nextnode] += X[nownode]
        dfs(nextnode,nownode)

dfs(0, -1)

print(*X)

Solution (Python):
from collections import deque
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
v = [0]*(n+1)
ans = [0]*(n+1)
for _ in range(n-1):
	a,b = map(int,input().split())
	g[a].append(b)
	g[b].append(a)
for _ in range(q):
	p,x = map(int,input().split())
	v[p] += x
alr = [False]*(n+1)
ans[1] = v[1]
que = deque()
que.append(1)
while que:
	cur = que.popleft()
	alr[cur] = True
	for nxt in g[cur]:
		if not alr[nxt]:
			ans[nxt] = ans[cur] + v[nxt]
			que.append(nxt)
print(*ans[1:])

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)]
values=[0]*(n+1)

for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

for p,x in PX:
    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):
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)]
values=[0]*(n+1)

for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

for p,x in PX:
    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):

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



N,Q = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

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

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

def dfs(v,parent,add):
    value[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])

dfs(1,0,0)

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

Solution (Python):
import sys
input = sys.stdin.readline
N,Q = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
PX = [tuple(map(int,input().split())) for i in range(Q)]

es = [[] for i in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

xs = [0] * N
for p,x in PX:
    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
input = sys.stdin.readline
N,Q = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
PX = [tuple(map(int,input().split())) for i in range(Q)]

es = [[] for i in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

xs = [0] * N
for p,x in PX:
    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):
(n,q),*t=[map(int,t.split())for t in open(0)]
s=[0]
d=s*n
f=s+[1]*n
e=[[]for _ in d]
for t in t:
 a,b=t
 if~-n:n-=1;e[a-1]+=b-1,;e[b-1]+=a-1,
 else:d[a-1]+=b
while s:
 v=s.pop()
 for w in e[v]:
  if f[w]:d[w]+=d[v];s+=w,;f[w]=0
print(*d)

Solution (Python):
(n,q),*t=[map(int,t.split())for t in open(0)]
s=[0]
d=s*n
f=s+[1]*n
e=[[]for _ in d]
for t in t:
  a,b=t
  if~-n:
    n-=1
    e[a-1]+=b-1,
    e[b-1]+=a-1,
  else:d[a-1]+=b
while s:
  v=s.pop()
  for w in e[v]:
    if f[w]:
      d[w]+=d[v]
      s+=w,
      f[w]=0
print(*d)

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

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

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

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

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

print(*ans)

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

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

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

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

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

print(*ans)

Solution (Python):
(n,q),*t=[list(map(int,t.split()))for t in open(0)]
s=[0]
d=s*n
f=s+[1]*n
e=[[]for _ in d]
for a,b in t:
 if~-n:n-=1;e[a-1]+=b-1,;e[b-1]+=a-1,
 else:d[a-1]+=b
while s:
 v=s.pop()
 for w in e[v]:d[w]+=d[v]*f[w];s+=[w]*f[w];f[w]=0
print(*d)

Solution (Python):
(n,q),*t=[map(int,t.split())for t in open(0)]
s=[0]
d=s*n
f=s+[1]*n
e=[[]for _ in d]
for a,b in t[:-q]:e[a-1]+=b-1,;e[b-1]+=a-1,
for p,x in t[-q:]:d[p-1]+=x
while s:
 v=s.pop()
 for w in e[v]:
  if f[w]:f[w]=0;d[w]+=d[v];s+=w,
print(*d)

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

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

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):
from collections import deque
import sys
input = sys.stdin.readline

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

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


def main():
    def dfs(v, p):
        for u in T[v]:
            if u != p:
                cnt[u] += cnt[v]
                dfs(u, v)

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


if __name__ == "__main__":
    main()

Solution (Python):

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

def dfs(v, p, tree, cnt):
    for u in tree[v]:
        if u != p:
            cnt[u] += cnt[v]
            dfs(u, v, tree, cnt)


def main():
    N, Q = tuple(map(int, input().split()))
    A = tuple(tuple(map(int, input().split())) for _ in range(N - 1))
    P = tuple(tuple(map(int, input().split())) for _ in range(Q))
    T = [[] for _ in range(N + 1)]  
    cnt = [0] * (N + 1)
    for a, b in A:  
        T[a] += [b]
        T[b] += [a]
    for p, x in P:  
        cnt[p] += x
    dfs(1, -1, T, cnt)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Solution (Python):
I=lambda:map(int,input().split());n,q=I();s=[0];d=s*n;f=s+[1]*n;e=[[]for _ in d]
while~-n:n-=1;a,b=I();e[a-1]+=b-1,;e[b-1]+=a-1,
while q:q-=1;p,x=I();d[p-1]+=x
while s:
 v=s.pop()
 for w in e[v]:d[w]+=d[v]*f[w];s+=[w]*f[w];f[w]=0
print(*d)

Solution (Python):
n,q,*t=map(int,open(0).read().split());s=[0];d=s*n;f=s+[1]*n;e=[[]for _ in d]
for a,b in zip(*[iter(t[:-q-q])]*2):e[a-1]+=b-1,;e[b-1]+=a-1,
for p,x in zip(*[iter(t[-q-q:])]*2):d[p-1]+=x
while s:
 v=s.pop()
 for w in e[v]:d[w]+=d[v]*f[w];s+=[w]*f[w];f[w]=0
print(*d)

Solution (Python):
n,q,*t=map(int,open(0).read().split())
e=[[]for _ in[0]*n]
s=[0]
d=s*n
f=[1]+s*n
for a,b in zip(*[iter(t[:-q-q])]*2):
  e[a-1]+=b-1,
  e[b-1]+=a-1,
for p,x in zip(*[iter(t[-q-q:])]*2):d[p-1]+=x
while s:
  v=s.pop()
  for w in e[v]:
    if f[w]:continue
    f[w]=1
    d[w]+=d[v]
    s+=w,
print(*d)

Solution (Python):
from collections import deque
def main():
  _map, _input, _int = map, input, int
  n, q = _map(_int, _input().split())
  values = [0] * n
  links = [[] for _ in range(n)]
  for _ in range(n - 1):
    a, b = _map(_int, _input().split())
    links[a - 1].append(b - 1)
    links[b - 1].append(a - 1)
  for _ in range(q):
    p, x = _map(_int, _input().split())
    values[p - 1] += x
  d = deque()
  d.append((0, -1))
  while len(d) != 0:
    i, p = d.popleft()
    for j in links[i]:
      if j == p:
        continue
      values[j] += values[i]
      d.append((j, i))
  print(*values)
main()

Solution (Python):
from collections import deque
def main():
  _map, _input, _int = map, input, int
  n, q = _map(_int, _input().split())
  values = [0] * n
  links = [[] for _ in range(n)]
  for _ in range(n - 1):
    a, b = _map(_int, _input().split())
    links[a - 1].append(b - 1)
    links[b - 1].append(a - 1)
  for _ in range(q):
    p, x = _map(_int, _input().split())
    values[p - 1] += x
  d = deque()
  d.append((0, -1))
  while len(d) != 0:
    i, p = d.popleft()
    for j in links[i]:
      if j == p:
        continue
      values[j] += values[i]
      d.append((j, i))
  print(*values)
main()

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
def main():
  _map = map
  _input = input
  _int = int
  def calc_results(values, links, i, p):
    for j in links[i]:
      if j == p:
        continue
      values[j] += values[i]
      calc_results(values, links, j, i)
  n, q = _map(_int, _input().split())
  values = [0] * (n + 1)
  links = [[] for _ in range(n + 1)]
  for _ in range(n - 1):
    a, b = _map(_int, _input().split())
    links[a].append(b)
    links[b].append(a)
  for _ in range(q):
    p, x = _map(_int, _input().split())
    values[p] += x
  calc_results(values, links, 1, 0)
  print(*values[1:])
main()

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

N,Q = map(int,input().split())
 
e_list = [[] for i in range(N)]
 
for i in range(N-1):
        a,b = 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 = 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]*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.appendleft(v1)
            count_list[v1] = count_list[v]+add_list[v1]
 
print(*count_list)

Solution (Python):
from collections import deque
n, q = map(int, input().split())
t = [[] for _ in range(n)]
for _ in range(n - 1):
  a, b = map(lambda x: int(x) - 1, input().split())
  t[a].append(b)
  t[b].append(a)
c = [0] * n
for _ in range(q):
  p, x = map(int, input().split())
  c[p - 1] += x
stk = deque()
stk.appendleft((0, -1))
while len(stk) != 0:
  i, p = stk.popleft()
  for j in t[i]:
    if j == p:
      continue
    c[j] += c[i]
    stk.appendleft((j, i))
print(*c)

Solution (Python):
N,Q = map(int,input().split())
 
e_list = [[] for i in range(N)]
 
for i in range(N-1):
        a,b = 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 = 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]*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.appendleft(v1)
            count_list[v1] = count_list[v]+add_list[v1]
 
print(*count_list)

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.appendleft(v1)
            count_list[v1] = count_list[v]+add_list[v1]
 
print(*count_list)

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



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

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

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

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

    
    dfs(0, -1)

    print(*ans)


if __name__ == '__main__':
    main()

Solution (Python):
N,Q=list(map(int,input().strip().split(' ')))
gr=[[] for _ in range(N)]
val=[0]*N
for _ in range(N-1):
    a,b=list(map(int,input().strip().split(' ')))
    a-=1
    b-=1
    gr[a]+=[b]
    gr[b]+=[a]
for _ in range(Q):
    p,x=list(map(int,input().strip().split(' ')))
    p-=1
    val[p]+=x
ans=[0]*N
stack=[0]
checked=[0]*N
checked[0]=1
ans[0]=val[0]
while stack:
    x=stack.pop()
    for v in gr[x]:
        if checked[v]==0:
            stack+=[v]
            checked[v]=1
            ans[v]=ans[x]+val[v]
ans=[str(x) for x in ans]

print(" ".join(ans))

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

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

edges = [[] for _ in range(n)]
for _ in range(n-1):
    a_i, b_i = list(map(int, input().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 = list(map(int, input().strip().split(" ")))
    
    counter[p_i-1] += x_i


parents = deque()


parents.append(0)
visited = set()
while parents:
    parent = parents.popleft()
    
    if parent in visited:
        continue
    visited.add(parent)
    
    
    
    for child in edges[parent]:
        if child in visited:
            continue
        
        
        counter[child] += counter[parent]
        
        
        parents.append(child)
        



print(*counter)

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

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

edge_dic = defaultdict(lambda: [])

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

ans_array = [0] * N

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


node_deque = deque()
used_array = [-1] * N
node_deque.append(0)
used_array[0] = 1

while len(node_deque):
    node = node_deque.popleft()
    for n in edge_dic[node]:
        if used_array[n] == 1:
            continue
        ans_array[n] += ans_array[node]
        used_array[n] = 1
        node_deque.append(n)

ans_str = map(str,ans_array)
print(" ".join(ans_str))

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

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

edges = [[] for _ in range(n)]
for _ in range(n-1):
    a_i, b_i = list(map(int, input().strip().split(" ")))
    edges[a_i-1].append(b_i-1)
    edges[b_i-1].append(a_i-1)
    
    



counter = [0] * n
p = [0] * n
x = []
for _ in range(q):
    p_i, x_i = list(map(int, input().strip().split(" ")))
    
    counter[p_i-1] += x_i


parents = deque()


parents.append(0)
visited = set()
while parents:
    parent = parents.popleft()
    
    if parent in visited:
        continue
    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):
from collections import deque
import sys


input = sys.stdin.readline
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)