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
input = sys.stdin.readline
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
graph = [[] for _ in range(N+1)]
for ab in AB:
    graph[ab[0]].append(ab[1])
    graph[ab[1]].append(ab[0])
val = [0] * (N+1)
for px in PX:
    val[px[0]] += px[1]
parent = [0] * (N+1)
Q  = [1]
while Q:
    q = Q.pop()
    for c in graph[q]:
        if c == parent[q]:
            continue
        parent[c] = q
        Q.append(c)
        val[c] += val[q]
print(*val[1:])

Solution (Python):
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
graph = [[] for _ in range(N+1)]
for ab in AB:
    graph[ab[0]].append(ab[1])
    graph[ab[1]].append(ab[0])
val = [0] * (N+1)
for px in PX:
    val[px[0]] += px[1]
parent = [0] * (N+1)
Q  = [1]
while Q:
    q = Q.pop()
    for c in graph[q]:
        if c == parent[q]:
            continue
        parent[c] = q
        Q.append(c)
        val[c] += val[q]
print(*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

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

parent = [0] * (N+1)
while stack:
    x = stack.popleft()
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
        val[y] += val[x]
print(*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(*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):
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):
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
graph = [[] for _ in range(N+1)]
for ab in AB:
    graph[ab[0]].append(ab[1])
    graph[ab[1]].append(ab[0])
val = [0] * (N+1)
for px in PX:
    val[px[0]] += px[1]
parent = [0] * (N+1)
from collections import deque
Q  = deque([1])
while Q:
    q = Q.popleft()
    for c in graph[q]:
        if c == parent[q]:
            continue
        parent[c] = q
        Q.append(c)
        val[c] += val[q]
print(*val[1:])

Solution (Python):
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))

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


def dfs(v, par):
    for nv in tree[v]:
        if nv != par:
            adds[nv] += adds[v]
            dfs(nv, v)


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

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

Solution (Python):

from collections import deque
n,q = map(int,input().split())
G = [[] for _ in range(n)]
ans = [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())
    ans[p-1]+=x

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

while q:
    v=q.pop()
    visited[v]=True
    for e in G[v]:
        if visited[e]:
            continue
        ans[e]+=ans[v]
        q.append(e)
print(*ans)

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

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


stack=[(0, ct[0], -1)]
ans=[0 for _ in range(n)]
while stack:
  vrt, cost, _from = stack.pop(-1)
  ans[vrt] = cost
  for _to in graph[vrt]:
    if _to ==_from:
      continue
    stack.append((_to, cost+ct[_to],vrt))
print(*ans)

Solution (Python):
n, q = map(int, input().split())
nei_list = [[] for i in range(n+1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    nei_list[a].append(b)
    nei_list[b].append(a)
score = [0] * (n+1)
for i in range(q):
    p, x = map(int, input().split())
    score[p] += x
work_stack = [1]
visited = set()
while work_stack:
    i = work_stack.pop()
    visited.add(i)
    for j in nei_list[i]:
        if j not in visited:
            work_stack.append(j)
            score[j] += score[i]
print(*score[1:])

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
G=[[]*n for i in range(n)]
P=[0]*n
V=[0]*n
for e in s[1:n]:
    a,b=map(int,e.split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for e in s[n:]:
    c,d=map(int,e.split())
    P[c-1]+=d
def dsf(i,p):
    V[i]=1
    P[i]+=p
    for j in G[i]:
        if V[j]!=1:
            dsf(j,P[i])
dsf(0,0)
print(' '.join(map(str,P)))

Solution (Python):
from collections import defaultdict, deque

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

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

visited = [False] * N

queue = deque()
queue.append(0)
while queue:
    parent = queue.popleft()
    visited[parent] = True
    for child in tree[parent]:
        if not visited[child]:
            counter[child] += counter[parent]
            queue.append(child)

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

Solution (Python):
n, q = map(int, input().split())
l = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    l[a - 1].append(b - 1)
    l[b - 1].append(a - 1)
r = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    r[p - 1] += x
stack = [0]
used = [False] * n    
while stack:
    i = stack.pop()
    used[i] = True
    for to in l[i]:
        if not used[to]:
            stack.append(to)
            r[to] += r[i]
for t_r in r:
    print(t_r)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
G=[[]*n for i in range(n)]
P=[0]*n
V=[0]*n
R=[0]*n
for e in s[1:n]:
    a,b=map(int,e.split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for e in s[n:]:
    c,d=map(int,e.split())
    P[c-1]+=d

def dsf(i,p):
    V[i]=1
    R[i]+=p+P[i]
    p=0
    if G[i]!=[]:
        for j in G[i]:
            if V[j]!=1:
                dsf(j,R[i])
dsf(0,0)
print(' '.join(map(str,R)))

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
G=[[]*n for i in range(n)]
P=[0]*n
V=[0]*n
R=[0]*n
for e in s[1:n]:
    a,b=map(int,e.split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for e in s[n:]:
    c,d=map(int,e.split())
    P[c-1]+=d

def dsf(i,p):
    V[i]=1
    R[i]+=p+P[i]
    p=0
    for j in G[i]:
        if V[j]!=1:
            dsf(j,R[i])
dsf(0,0)
print(' '.join(map(str,R)))

Solution (Python):
N,Q=map(int,input().split())
E=[[] for i in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  E[a].append(b)
  E[b].append(a)
  
P=[0]*N
for i in range(Q):
  p,x=map(int,input().split())
  P[p-1]+=x
  
ans=[0]*N
stack=[]

stack.append([0,-1,0])
while stack:
  node=stack.pop()
  v=node[0]
  parent=node[1]
  point=node[2]
  point+=P[v]
  ans[v]=point
  children=E[v]
  for child in children:
    if child==parent:
      continue
    stack.append([child,v,point])
    
print(*ans)

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
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):
n, q = map(int, input().split())
C = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  C[a-1].append(b-1)
  C[b-1].append(a-1)
dist = [-1]*n
dist[0] = 1
A = [0]*n
F = [0]*n
for _ in range(q):
  p, x = map(int, input().split())
  F[p-1] += x
S = [0]
T = [0]
while S:
  v = S.pop()
  for nv in C[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      S.append(nv)
while T:
  v = T.pop()
  A[v] += F[v]
  for nv in C[v]:
    if dist[nv] > dist[v]:
      A[nv] += A[v]
      T.append(nv)
print(*A)

Solution (Python):
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))

Solution (Python):
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))

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

ki = [[]for _ in range(N)]
for a,b in AB:
    a-=1
    b-=1
    ki[a].append(b)
    ki[b].append(a)
point = [0]*(N)
for p,x in PX:
    p-=1
    point[p]+=x
def dfs(now, last ,add):
    point[now]+= add
    for x in ki[now]:
        if (x == last):continue
        dfs(x,now,point[now])
dfs(0,0,0)
print(*point)

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

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

Q = [(0, -1)]
while Q:
    v, p = Q.pop()
    for x in E[v]:
        if x == p: 
            continue
        C[x] += C[v] 
        Q.append((x, v))

print(*C)

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

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

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

used = [False] * N
r = [0] * N

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

stack = [0]
while stack:
  i = stack.pop()
  used[i] = True
  for to in l[i]:
    if not used[to]:
      stack.append(to)
      r[to] += r[i]

print(*r)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,q=map(int,input().split())
    E=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b=map(int,input().split())
        a-=1; b-=1
        E[a].append(b)
        E[b].append(a)

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

    Q=[(0,-1)]
    while(Q):
        v,p=Q.pop()
        for nv in E[v]:
            if(p==nv): continue
            C[nv]+=C[v]
            Q.append((nv,v))

    print(*C)
resolve()

Solution (Python):
from collections import deque
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)]
tree = [[] for _ in range(N)]
ans = [0]*N
for a,b in ab:
  tree[a-1].append(b-1)
  tree[b-1].append(a-1)
  
for p,x in px:
  ans[p-1]+=x
  
q = deque([0])
judge = [False]*N
while q:
  a=q.pop()
  judge[a]=True
  for i in tree[a]:
    if judge[i]:
      continue
    ans[i]+=ans[a]
    q.append(i)
    
print(' '.join(map(str,ans)))

Solution (Python):
import heapq

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

node = [0]*N
edge = [[] for _ in range(N)]
color = [0]*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)

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

stack = [0]
while stack:
    s = stack.pop()
    color[s] = 1
    for t in edge[s]:
        if color[t]==0:
            stack.append(t)
            node[t] += node[s]
    color[s]=2

print(*node)

Solution (Python):
from collections import deque

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

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)

counter = [0] * N
checked = [False] * N

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

queue = deque()
queue.append(0)

while queue:
    root = queue.pop()
    checked[root] = True
    for e in edges[root]:
        if checked[e] == False:
            queue.append(e)
            counter[e] += counter[root]

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

Solution (Python):
from collections import deque

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

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

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

d = deque()
d.append(1)
chked = set()
while d:
    stat = d.pop()
    chked.add(stat)
    for v in t[stat]:
        if v not in chked:
            d.append(v)
            cnt[v] += cnt[stat]

print(*cnt[1:])

Solution (Python):
import sys
input = sys.stdin.readline
inputs = lambda:map(int, input().split())
n, q = inputs()
e = [[] for _ in range(n)]
counter = [0] * n
m = n-1
for _ in range(m):
    v1, v2 = inputs()
    v1 -= 1
    v2 -= 1
    e[v1] += v2,
    e[v2] += v1,
for _ in range(q):
    p, x = inputs()
    p -= 1
    counter[p] += x
used = [False] * n
s = 0
used[s] = True
stack = [s]
while stack:
    s = stack.pop()
    for c in e[s]:
        if used[c]:
            None
        else:
            used[c] = True
            counter[c] += counter[s]
            stack += c,
print(*counter)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy


from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return sorted([list(map(int, input().split())) for _ in range(n)])
def sortx(x,n,k):
    if k == 0:x.sort(key=lambda y:y[1,n])
    else:x.sort(reversed=True, key=lambda y:y[1,n])
def graph():
    n,m=inpm()
    g=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b=inpm()
        a-=1
        b-=1
        g[a].append(b)
        g[b].append(a)
    return n,m,g
 
def main():
    n,q=inpm()
    g=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b=inpm()
        a-=1
        b-=1
        g[a].append(b)
        g[b].append(a)
    tuple(g)
    px=[]
    for _ in range(q):
        px.append(tuple(inpl()))
    ans=[0 for _ in range(n)]
    went=[False for _ in range(n)]
    went[0]=True
    for i in range(q):
        ans[px[i][0]-1]+=px[i][1]
    que=deque()
    que.append(0)
    while que:
        par = que.pop()
        for chi in g[par]:
            if went[chi]:
                continue
            else:
                went[chi]=True
                ans[chi]+=ans[par]
                que.append(chi)
    
    print(*ans)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
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)]


class Node:
    def __init__(self, val):
        self.val = val
        self.child_list = []
        self.cnt = 0

class my_tree:
    def __init__(self, tree_list):
        self.node_list = []

        for i in range(N):
            self.node_list.append(Node(i+1))

        for a, b in tree_list:
            a, b = int(a), int(b)

            child_node = self.node_list[b-1]
            parent_node = self.node_list[a-1]
            self.node_list[a-1].child_list.append(child_node)
            self.node_list[b-1].child_list.append(parent_node)
        
    def adding(self, query_list):
        for a, data in query_list:
            a, data = int(a), int(data)
            self.node_list[a-1].cnt += data

        stack = deque([self.node_list[0]])
        parent_node_list = [self.node_list[0]]*(N + 1)

        while True:
            v = stack.pop()
            for child in v.child_list:
                if child != parent_node_list[v.val -1]:
                    child.cnt += v.cnt
                    parent_node_list[child.val -1] = v
                    stack.append(child)
            
            if not stack:
                break


ins = my_tree(tree_list)
ins.adding(query_list)
print(*[node.cnt for node in ins.node_list])

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    
    n,q=map(int,input().split())
    tree=[[] for _ in range(n)]
    for i in range(n-1):
        a,b=map(int,input().split())
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        p[i],x[i] = map(int,input().split())
        
    px=[[] for i in range(n)]
    for pp,xx in zip(p,x):
        px[pp-1].append(xx)
    px=[sum(i) for i in px]
    c=[0]*n
    flag=[True]*n
     
    from collections import deque
    q = deque([(-1,0,0)])
    while q:
        parent,child,point = q.popleft()
        if parent!=-1:
            flag[parent]=False
        point+=px[child]
        c[child]=point
        for y in tree[child]:
            if y == parent:
                continue
            if flag[y]:
                q.append((child,y,point))
    print(*c)
if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

def dfs(i, V, E, x):
    S = [i]
    while(len(S) > 0):
        vi = S.pop()
        p = x[vi]
        for j in E[vi]:
            if V[j] == 0:
                V[j] = 1
                x[j] += p
                S.append(j)
    return V, x

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

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

V[1] = 1
dfs(1, V, E, x)
print(*x[1:])

Solution (Python):
from collections import defaultdict

def dfs(i, V, E, x):
    S = [i]
    while(len(S) > 0):
        vi = S.pop()
        p = x[vi]
        for j in E[vi]:
            if V[j] == 0:
                V[j] = 1
                x[j] += p
                S.append(j)
    return V, x

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

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

V[1] = 1
V, x = dfs(1, V, E, x)
print(*x[1:])

Solution (Python):
from collections import defaultdict

def dfs(i, V, E, x):
    S = [i]
    while(len(S) > 0):
        vi = S.pop()
        p = x[vi]
        for j in E[vi]:
            if V[j] == 0:
                V[j] = 1
                x[j] += p
                S.append(j)
    return V, x

n, q = map(int, input().split())
V = [0 for _ in range(n + 1)]
E = defaultdict(list)
for i in range(n - 1):
    a, b = map(int, input().split())
    E[a].append(b)
    E[b].append(a)

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

V[1] = 1
V, x = dfs(1, V, E, x)
print(*x[1:])

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


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


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


used = [False for _ in range(N)]
used[0] = True
buf = [0]
while len(buf) > 0:
    i = buf.pop()
    for child in graph[i]:
        if not used[child]:
            counts[child] += counts[i]
            buf.append(child)
            used[child] = True


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

Solution (Python):
import sys
input = sys.stdin.readline
inputs = lambda:map(int, input().split())
n, q = inputs()
e = [[] for _ in range(n)]
counter = [0] * n
m = n-1
for _ in range(m):
    v1, v2 = inputs()
    v1 -= 1
    v2 -= 1
    e[v1] += v2,
    e[v2] += v1,
for _ in range(q):
    p, x = inputs()
    p -= 1
    counter[p] += x
used = [False] * n
s = 0
used[s] = True
stack = [s]
while stack:
    s = stack.pop()
    for c in e[s]:
        if used[c]:
            None
        else:
            used[c] = True
            counter[c] += counter[s]
            stack += c,
print(*counter)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    inputs = lambda:map(int, input().split())
    n, q = inputs()
    e = [[] for _ in range(n)]
    counter = [0] * n
    m = n-1
    for _ in range(m):
        v1, v2 = inputs()
        v1 -= 1
        v2 -= 1
        e[v1] += v2,
        e[v2] += v1,
    for _ in range(q):
        p, x = inputs()
        p -= 1
        counter[p] += x
    used = [False] * n
    s = 0
    used[s] = True
    stack = [s]
    while stack:
        s = stack.pop()
        for c in e[s]:
            if used[c]:
                None
            else:
                used[c] = True
                counter[c] += counter[s]
                stack += c,
    print(*counter)
main()

Solution (Python):
def main():
    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:
                到達済み[子] = 真
                カウンター[子] += カウンター[親]
                スタック += 子,
    出力(*カウンター)
main()

Solution (Python):
def main():
    from collections import deque
    n, q = map(int, input().split())
    g = [[] for i in range(n+1)]
    point = [0 for i 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())
        point[p] += x

    q = deque([[1,0]])
    for j in range(n):
        now = q.pop()
        point[now[0]] += point[now[1]]
        for i in g[now[0]]:
            if i==now[1]:
                continue
            q.append([i,now[0]])
    for i in range(1, n+1):
        print(point[i], end='')
        if i!=n:
            print(' ', end='')

if __name__ == '__main__':
    main()

Solution (Python):
n,q = map(int,input().split())
L = [0]*(n+1)
graph = [[] for i in range(n+1)]
for i in range(n-1):
    a,b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)
for i in range(q):
    a,b = map(int,input().split())
    L[a] += b
q = [(1,0)]
while len(q) > 0:
    c = q.pop()
    x, par = c[0], c[1]
    L[x] += L[par]
    for child in graph[x]:
        if par == child:
            continue
        q.append((child,x))
print(' '.join(map(str,L[1:])))

Solution (Python):

import math
import heapq
import bisect

MOD = 10**9+7

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

node = [0]*N
edge = [[] for _ in range(N)]
color = [0]*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)

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

stack = [0]
while stack:
    s = stack.pop()
    color[s] = 1
    for t in edge[s]:
        if color[t]==0:
            stack.append(t)
            node[t] += node[s]
    color[s]=2

for n in node:
    print(n)

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