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

Solution (Python):
from collections import deque
n,q = map(int,input().split())
ki = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    ki[a-1].append(b-1)
    ki[b-1].append(a-1)
score = [0 for i in range(n)]

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


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

ans[0] = score[0]
visited = [False]*n
visited[0] = True
Q = deque([0])

while Q:
    d = Q.pop()
    
    for to in ki[d]:
        if visited[to]:
            continue
        
        ans[to] = ans[d] + score[to]
        
        visited[to] = True
        Q.append(to)
print(*ans)

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

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

graph = defaultdict(list)
for _ in range(n-1):
    p,c = list(map(int, input().split()))
    graph[p].append(c)
    graph[c].append(p)

counter = defaultdict(int)
for _ in range(q):
    i,c = list(map(int, input().split()))
    counter[i] += c

ans = [0] * (n + 1)
visit = [False] * (n + 1)
def dfs(node, total_counter):
    visit[node] = True
    total_counter += counter.get(node, 0)
    
    for child in graph[node]:
        if not visit[child]:
            dfs(child, total_counter)
    ans[node] = total_counter

dfs(1, 0)

print(*ans[1:])

Solution (Python):
from collections import deque


def main():

    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)

    q = deque()
    score = [0] * N
    seen = [0] * N
    for _ in range(Q):
        a, s = map(int, input().split())
        score[a-1] += s

    q.append(0)
    while q:
        tmp = q.pop()
        seen[tmp] += 1
        for v in G[tmp]:
            if seen[v]:
                continue
            q.append(v)
            score[v] += score[tmp]

    print(*score)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
 
N, Q = map(int, input().split())
root = [[] for _ in range(N)]
ans = [0]*N
 
for _ in range(N-1):
  a,b=map(int, input().split())
  a,b=a-1,b-1
  root[a].append(b)
  root[b].append(a)

for _ in range(Q):
  p,x=map(int, input().split())
  ans[p-1]+=x
  
visited=[False]*N
def dfs(v):
  visited[v] = True
  for go in root[v]:
    if visited[go]:
      continue
    ans[go] += ans[v]
    dfs(go)

dfs(0)
print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
 
N, Q = map(int, input().split())
root = [[] for _ in range(N)]
ans = [0]*N
 
for _ in range(N-1):
  a,b=map(int, input().split())
  a,b=a-1,b-1
  root[a].append(b)
  root[b].append(a)

for _ in range(Q):
  p,x=map(int, input().split())
  ans[p-1]+=x
  
visitted=[False]*N
def dfs(v):
  visitted[v] = True
  for go in root[v]:
    if visitted[go]:
      continue
    ans[go] += ans[v]
    dfs(go)

dfs(0)
print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
 
N, Q = map(int, input().split())
root = [[] for _ in range(N)]
ans = [0]*N
 
for _ in range(N-1):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  root[a].append(b)
  root[b].append(a)

for _ in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)

dfs(0)
print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
 
N, Q = map(int, input().split())
root = [[] for i in range(N)]
ans = [0]*N
 
for i in range(N-1):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  root[a].append(b)
  root[b].append(a)

for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)

dfs(0)
print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(2*10**9)
N, Q = map(int, input().split())
A = [[] for _ in range(N)]
for _ in range(N-1):
    i, k = map(int, input().split())
    A[i-1].append(k-1)
    A[k-1].append(i-1)
n_P = [0] * N
for _ in range(Q):
    k, n = map(int, input().split())
    n_P[k-1] += n
ans = [0] * N
visited = [0] * N
def make(num, p):
    ans[num] += n_P[num] 
    ans[num] += p
    visited[num] = 1
    for i in A[num]:
        if visited[i] == 0:
            make(i,ans[num])
    return
make(0, ans[0])
print(*ans)

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

n,q = LI()
root = [[] for _ in range(n+1)]
quest = [0 for _ in range(n+1)]

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

for _ in range(q):
    p,x = LI()
    quest[p] += x

stock = collections.deque([1])
explored = set()

while len(stock) != 0:
    next = stock.pop()
    explored.add(next)
    for i in root[next]:
        if i not in explored:
            stock.append(i)
            quest[i] += quest[next]
print(*quest[1:])

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,q = LI()
    root = [[] for _ in range(n+1)]
    quest = [0 for _ in range(n+1)]

    for _ in range(n-1):
        a,b = LI()
        root[a].append(b)
        root[b].append(a)
    
    for _ in range(q):
        p,x = LI()
        quest[p] += x

    stock = collections.deque([1])
    explored = set()

    while len(stock) != 0:
        next = stock.pop()
        explored.add(next)
        for i in root[next]:
            if i not in explored:
                stock.append(i)
                quest[i] += quest[next]
    print(*quest[1:])
main()

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

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

adj_list = [[] for _ in range(N)]
counter = [0 for _ in range(N)]
reached = [0 for _ in range(N)]

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

def df(current, score):
    next_nodes = adj_list[current]
    reached[current]=1
    for node in next_nodes:
        if reached[node]==0:
            counter[node]+=score
            df(node, counter[node])

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

Solution (Python):



import sys
import heapq
import math


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


def main():
    N, Q = map(int, input().split())
    tree = [[] for _ in range(N)]

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

    counter = [0] * N

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

    visited = set()
    stack = [0]
    while stack:  
        i = stack.pop()
        visited.add(i)
        for j in tree[i]:
            if not j in visited:  
                counter[j] += counter[i]
                stack.append(j)
    print(*counter)


if __name__ == "__main__":
    main()

Solution (Python):
import collections
N,Q=map(int,input().split())
arr=[[] for i in range(N)]

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

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



vist=[0]*N
d=collections.deque()
d.append(0)
while d:
    a=d.pop()
    vist[a]=1
    
    for j in arr[a]:
        
        if vist[j]==1:
            continue
        lis[j]+=lis[a]
        d.append(j)
 



print(*lis)

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

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

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

q = [0]
s = [-1]*n
s[0] = l[0]
while len(q) > 0:
    r = q.pop()
    for i in tree[r]:
        if s[i] == -1:
            s[i] = s[r] + l[i]
            q.append(i)
print(*s)

Solution (Python):
import sys
sys.setrecursionlimit(4*(10**6))

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

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

visited=set()

def dfs(v):
    global score
    visited.add(v)
    for i in edge[v]:
        if i in visited:
            continue
        score[i]+=score[v]
        dfs(i)

dfs(1)
print(*score[1:])

Solution (Python):
import sys
sys.setrecursionlimit(400000)
n,q=map(int,input().split())
ans=[0]*(n+1) 
score=[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 j in range(q):
    p,x=map(int,input().split())
    score[p]+=x

visited=[0]*(n+1)
def dfs(v):
    visited[v]+=1
    global ans
    ans[v]+=score[v]
    for i in range(len(g[v])):
        if visited[g[v][i]]==0:
            ans[g[v][i]]+=ans[v]
            dfs(g[v][i])
dfs(1)
print(" ".join(map(str, ans[1:])))

Solution (Python):
import sys
sys.setrecursionlimit(400000)
n,q=map(int,input().split())
ans=[0]*(n+1) 
score=[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 j in range(q):
    p,x=map(int,input().split())
    score[p]+=x

visited=[0]*(n+1)
def dfs(v):
    visited[v] += 1
    global ans
    ans[v]+=score[v]
    for i in range(len(g[v])):
        if visited[g[v][i]] == 0:
            ans[g[v][i]]+=ans[v]
            dfs(g[v][i])
dfs(1)
print(" ".join(map(str, ans[1:])))

Solution (Python):
n,q=map(int,input().split())
l=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(lambda x:int(x)-1,input().split())
    l[a].append(b)
    l[b].append(a)
xl=[0 for i in range(n)]
for i in range(q):
    p,x=map(int,input().split())
    p-=1
    xl[p]+=x

que=[0]
s=[-1 for i in range(n)]
s[0]=xl[0]
while len(que)>0:
    r=que.pop()
    for i in l[r]:
        if s[i]==-1:
            s[i]=s[r]+xl[i]
            que.append(i)
print(*s)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7



N, Q = list(map(int, input().split()))
al = [[] for i in range(N)]

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

    al[a-1].append(b-1)
    al[b-1].append(a-1)

visited = [0 for i in range(N)]
seen = [0 for i in range(N)]
score = [0 for i in range(N)]

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

@functools.lru_cache(None)
def dfs_rec(u):
    visited[u] = 1
    for v in al[u]:
        if visited[v] == 0:
            score[v] += score[u]
            dfs_rec(v)

dfs_rec(0)

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

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

def dfs(x):
    seen[x]=1
    for nx in lis[x]:
        if seen[nx]==0:
            psum[nx]=psum[x]+plis[nx]
            dfs(nx)

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

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

for _ in range(n-1):
    ai,bi=map(lambda x:int(x)-1,input().split())
    lis[ai].append(bi)
    lis[bi].append(ai)

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

for _ in range(q):
    pi,xi=map(int,input().split())
    plis[pi-1]+=xi

seen=[0 for _ in range(n)]
psum=[0 for _ in range(n)]
psum[0]=plis[0]

dfs(0)

print(*psum)

Solution (Python):


import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


from collections import defaultdict

N, Q = read_ints()
tree = defaultdict(lambda: [])
for _ in range(N - 1):
    a, b = read_ints()
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)
cnt = [0] * N
for _ in ra(Q):
    q, x = read_ints()
    cnt[q - 1] += x




def dfs(c, u, p):  
    cnt[u] += c
    for nv in tree[u]:
        if nv == p:
            continue
        dfs(cnt[u], nv, u)


dfs(0, 0, -1)

print(*cnt)

Solution (Python):


import sys
from collections import defaultdict
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))


def ints():
    return list(map(int, read().split()))


N, Q = ints()
tree = defaultdict(lambda: [])
for _ in range(N - 1):
    a, b = mina(*ints())
    tree[a].append(b)
    tree[b].append(a)
cnt = [0] * N
for _ in ra(Q):
    q, x = ints()
    cnt[q - 1] += x
cnt.append(0)  



def dfs(u, p):  
    S_args = [(u, p)]  
    S_cmd = [0]  

    def into(args):
        '''入るときの処理'''
        u, p = args
        cnt[u] += cnt[p]

    def nxt(args):
        S_args.append(args)  
        S_cmd.append(1)
        '''今の引数からみて次の引数を列挙'''
        u, p = args
        for nx in tree[u]:
            if nx == p:
                continue
            _stack(nx, u)

    def outof(args):
        '''抜けるときの処理'''
        pass

    def _stack(*args):  
        S_args.append(args)
        S_cmd.append(0)

    while S_cmd:
        now_args = S_args.pop()
        cmd = S_cmd.pop()
        if cmd == 0:
            into(now_args)
            nxt(now_args)  
        else:
            outof(now_args)


dfs(0, -1)

print(*cnt[:-1])

Solution (Python):


import sys
from collections import defaultdict
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))


def ints():
    return list(map(int, read().split()))


N, Q = ints()
tree = defaultdict(lambda: [])
for _ in range(N - 1):
    a, b = mina(*ints())
    tree[a].append(b)
    tree[b].append(a)
cnt = [0] * N
for _ in ra(Q):
    q, x = ints()
    cnt[q - 1] += x
cnt.append(0)  












def dfs(u, p):  
    S_args = [(u, p)]  
    S_cmd = [0]  

    def into(args):
        '''入るときの処理'''
        u, p = args
        cnt[u] += cnt[p]

    def nxt(args):
        S_args.append(args)  
        S_cmd.append(1)
        '''今の引数からみて次の引数を列挙'''
        u, p = args
        for nx in tree[u]:
            if nx == p:
                continue
            _stack(nx, u)

    def outof(args):
        '''抜けるときの処理'''
        pass

    def _stack(*args):  
        S_args.append(args)
        S_cmd.append(0)

    while S_cmd:
        now_args = S_args.pop()
        cmd = S_cmd.pop()
        if cmd == 0:
            into(now_args)
            nxt(now_args)  
        else:
            outof(now_args)


dfs(0, -1)

print(*cnt[:-1])

Solution (Python):


import sys
from collections import defaultdict
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))


def ints():
    return list(map(int, read().split()))


N, Q = ints()
tree = defaultdict(lambda: [])
for _ in range(N - 1):
    a, b = mina(*ints())
    tree[a].append(b)
    tree[b].append(a)
cnt = [0] * N
for _ in ra(Q):
    q, x = ints()
    cnt[q - 1] += x
cnt.append(0)  




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


dfs(0, -1)

print(*cnt[:-1])

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

def DFS(node):
    if not checked[node]:
        global point
        checked[node] = True
        point += pointlist[node]
        
        for child in neighborlist[node]:
            if not checked[child]:
                DFS(child)
        
        anslist[node] += point
        point -= pointlist[node]

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

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

checked = [False]*(N+1)
anslist = [0]*(N+1)
point = 0

DFS(1)

print(*anslist[1:])

Solution (Python):
import sys
sys.setrecursionlimit(400000)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)] 
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
    paths[b].append(a)

px = [list(map(int, input().split())) for _ in range(Q)]

ans = [0]*(N+1) 

counters = [0]*(N+1) 
for x in px:
    counters[x[0]] += x[1]

visit = [0]*(N+1) 
def dfs(node, score, ans):
    visit[node] = 1 
    score += counters[node] 
    ans[node] = score 
    for loof in paths[node]: 
        if visit[loof] == 0: 
            dfs(loof, score, ans) 

dfs(1, 0, ans)
    

for i in range(1, N+1):
    print(ans[i])

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

N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)

Solution (Python):
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
d = [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())
    d[p] += x
ans = [0]*(n+1)
seen = [False]*(n+1)
from collections import deque
q = deque([(1,d[1])])
while q:
    s,cost = q.popleft()
    seen[s] = True
    ans[s] += cost
    for e in g[s]:
        if not seen[e]:
            q.append((e,cost+d[e]))









res = []
for a in ans[1:]:
    res.append(str(a))

print(' '.join(res))

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

from collections import deque

dist=[-1]*(N+1) 
dist[0]=0
dist[1]=0

D=deque()
D.append(1)

while D:
    V=D.popleft()
    for i in A[V]:
        if dist[i]!=-1:
            continue
        dist[i]=0
        B[i]+=B[V] 
        D.append(i)

print(*B[1:])

Solution (Python):
import copy
import math
import time
import statistics
import math
import itertools
import bisect
import sys
from decimal import *


def get_int():
    return int(input())

def get_string():
    return input()

def get_int_list():
    return [int(x) for x in input().split()]

def get_string_list():
    return input().split()

def get_int_multi():
    return map(int, input().split())

def get_string_char_list():
    return list(str(input()))




'''
while (idx < n) and ():

    idx += 1
'''

def main():

    start = time.time()

    n, q = get_int_multi()

    a = [[] for _ in range(n+1)]
    for _ in range(n-1):
        s, e = get_int_multi()
        a[s].append(e)
        a[e].append(s)

    point = [0] * (n+1)
    for _ in range(q):
        p, x = get_int_multi()
        point[p] += x

    def tasu(now, prev):
        for next in a[now]:
            if next == prev:
                continue
            point[next] += point[now]
            tasu(next, now)

    sys.setrecursionlimit(10 ** 6)
    tasu(1, -1)
    point = point[1:] 
    print(*point) 


if __name__ == '__main__':
    main()

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

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

for i in range(q):
    s,t=map(int,input().split())
    ans[s-1]+=t

Q=[0]

flag=[True]+[False]*(n-1)


while Q:
  v=Q.pop()
  flag[v]=True
  for i in lst[v]:
    if flag[i]:
      continue
    flag[i]=True
    ans[i]+=ans[v]
    Q.append(i)


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

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(10**7)

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

graph = [[] for i in range(N)]
score = [0 for _ in range(N)]

def dfs(now, prev):

    for adj in graph[now]:
        if adj == prev:
            continue
        score[adj] += score[now]
        dfs(adj, now)

    return

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

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

    score[p-1] += x

dfs(0, -1)

for p in score:
    print(p)

Solution (Python):
from collections import defaultdict, deque
dic = defaultdict(list)
N, Q = map(int, input().split())
point = [0 for _ in range(N)]

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

for _ in range(Q):
    p, x = map(int, input().split())
    point[p-1]+=x
    
queue = deque()
for i in dic[1]:
    queue.append(1)
    queue.append(i) 
    
while queue:
    p = queue.popleft()
    c = queue.popleft()
    point[c-1]+=point[p-1] 
    
    for j in dic[c]:
        if j != p:
            queue.append(c)
            queue.append(j)
            
        
print(*point)

Solution (Python):
import math
import sys

sys.setrecursionlimit(200005)

n, q = map(int, input().split(" "))
abL = [list(map(int, input().split(" "))) for _ in range(n - 1)]
pxL = [list(map(int, input().split(" "))) for _ in range(q)]

to = [[] for _ in range(200005)]
ans = [0 for _ in range(n + 1)]


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


for a, b in abL:
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for p, x in pxL:
    p -= 1
    ans[p] += x

dfs(0)

for a in ans[:n]:
    print(a)

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

w = [[] for _ in range(N+1)]
for _ in range(N-1):
    A, B = map(int, input().split())
    w[A].append(B)
    w[B].append(A)

cnt = [0]*(N+1)
for _ in range(Q):
    P, X = map(int, input().split())
    cnt[P] += X

flag = [False]*(N+1)

q = [1]
while q:
    n = q.pop()
    flag[n] = True
    for i in w[n]:
        if flag[i]:
            continue
        flag[i] = True
        cnt[i] += cnt[n]
        q.append(i)

print(" ".join([str(c) for c in cnt[1:]]))

Solution (Python):
from collections import deque
n, q = map(int, input().split())

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

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

for i in range(q):
    idx, v = map(int, input().split())
    cnt[idx - 1] += v

dist = [-1] * n
que = deque()
dist[0] = 0
que.append(0)

while(len(que) != 0):
    
    v = que.pop()
    for r in road[v]:
        if dist[r] != -1:
            continue
        
        dist[r] = dist[v] + 1
        cnt[r] += cnt[v]
        que.append(r)
print(*cnt)

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

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

visited = [False] * N
que = deque([0])

while que:
    now = que.pop()
    visited[now] = True
    for nxt in G[now]:
        if visited[nxt]:
            continue
        count[nxt] += count[now]
        que.appendleft(nxt)

print(*count)

Solution (Python):
from collections import deque












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

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

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

for i in range(q):
    idx,v = map(int,input().split())
    cnt[idx-1] += v

dist = [-1]*n
que  = deque()
dist[0] = 0
que.append(0)

while(len(que)!=0):
    
    v = que.pop()
    for r in road[v]:
        if dist[r]!=-1:
            continue
        dist[r] = dist[v] + 1
        cnt[r] += cnt[v]
        que.append(r)

print(*cnt)

Solution (Python):
from collections import deque
N, Q = map(int, input().split())
G = [[] for _ in range(N)]
count = [0] * N
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

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

que = deque([0])
flag = [False] * N

while que:
    v = que.pop()
    flag[v] = True
    for e in G[v]:
        if flag[e]:
            continue
        count[e] += count[v]
        que.appendleft(e)

print(*count)

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, q = MAP()
graph = [[] for i in range(n)]
point = [0]*n
for i in range(n-1):
    a, b = MAP()
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
for i in range(q):
    p, x = MAP()
    point[p-1] += x



def dfs(now, prev = -1):
    for next in graph[now]:
        
        if next == prev:
            continue
        
        point[next] += point[now]
        
        dfs(next, now)

dfs(0)
print(*point)

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


sys.setrecursionlimit(10**7)








INF = 1 << 50

def dfs(current, node_vals, seen, to):
    for next in to[current]:
        if not seen[next]:
            node_vals[next] += node_vals[current]
            seen[next] = 1
            dfs(next, node_vals, seen, to)


def run():
    N, Q = map(int, sysread().split())
    to = [[] for _ in range(N+1)]
    for i in range(N-1):
        a,b = map(int, sysread().split())
        to[a].append(b)
        to[b].append(a)

    node_vals = [0] * (N+1)

    for i in range(Q):
        p, x = map(int, sysread().split())
        node_vals[p] += x

    seen = [0] * (N+1)
    seen[1] = 1
    dfs(1, node_vals, seen, to)

    node_vals = [str(v) for v in node_vals]
    print(' '.join(node_vals[1:]))

if __name__ == "__main__":
    run()

Solution (Python):
from collections import deque
import copy


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

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

arrive_rireki = [-1] * (n + 1)

score = [0]*n
score_temp = 0


def dfs(v):
    global score_temp
    stack = [v]
    arrive_rireki[v] = 1
    score_temp += q_sum_list[v-1]
    while stack:
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if arrive_rireki[w] < 0:
                arrive_rireki[w] = 1
                stack.append(w)
                score_temp += q_sum_list[w-1]
        else:
            stack.pop()
            score[v-1] = score_temp
            score_temp -= q_sum_list[v-1]
    return score

dfs(1)

print(* score)

Solution (Python):
from collections import deque
import copy


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

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

arrive_rireki = [-1] * (n + 1)

score = [0]*n
score_temp = 0


def dfs(v):
    global score_temp
    
    
    stack = [v]
    arrive_rireki[v] = 1
    score_temp += q_sum_list[v-1]
    while stack:
        
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if arrive_rireki[w] < 0:
                arrive_rireki[w] = 1
                stack.append(w)
                score_temp += q_sum_list[w-1]
        else:
            stack.pop()
            
            score[v-1] = score_temp
            score_temp -= q_sum_list[v-1]
            
            
            
                
    return score

dfs(1)

print(* score)

Solution (Python):
from collections import deque
 
N, Q = map(int, input().split())
Ki = [[] for _ in range(N)]
for _ in range(N-1):
    p, c = map(int, input().split())
    Ki[p-1].append(c-1)
    Ki[c-1].append(p-1)
S = [0]*N
for _ in range(Q):
    node, score = map(int, input().split())
    S[node-1] += score
 
stack = deque([0])
done = [0]*N
done[0] = 1
 
while len(stack) > 0:
    now = stack.pop()
    
    for new in Ki[now]:
        if done[new] == 0:
            done[new] = 1
            stack.append(new)
            S[new] += S[now]
    
print(*S)