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

Solution (Python):
N,Q = map(int,input().split())
AB = [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=map(int,input().split())
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
X=[0 for i in range(N)]
for i in range(Q):
    p,x=map(int,input().split())
    X[p-1]+=x
q=[0]
reached=[0 for i in range(N)]
ans=[0 for i in range(N)]

while(len(q)>0):
    r=q.pop()
    ans[r]+=X[r]
    reached[r]=1
    for p in G[r]:
        if reached[p]==0:
            ans[p]+=ans[r]
            q.append(p)
print(*ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
def main():
    N, Q = map( int, input().split())
    E = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map( int, input().split())
        a, b = a-1, b-1
        E[a].append(b)
        E[b].append(a)
    V = [0]*N
    for _ in range(Q):
        p, x = map( int, input().split())
        p -= 1
        V[p] += x
    q = deque([0])
    W = [0]*N
    W[0] = 1
    while q:
        v = q.popleft()
        c = V[v]
        for t in E[v]:
            if W[t] == 1:
                continue
            V[t] += c
            W[t] = 1
            q.append(t)
    print( " ".join( map( str, V)))
if __name__ == '__main__':
    main()

Solution (Python):

import sys
sys.setrecursionlimit(300000)


def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)
    plus = [0] * N
    for i in range(Q):
        plus[p[i] - 1] += x[i]

    ret = [0] * N
    def dfs(idx, cur, visited):
        visited[idx] = True
        cur += plus[idx]
        ret[idx] = cur
        for nex in conn[idx]:
            if not visited[nex]:
                dfs(nex, cur, visited)
        return
    visited = [False] * N
    dfs(0, 0, visited)
    print(' '.join([str(r) for r in ret]))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    Q = int(next(tokens))  
    a = [int()] * (N-1)  
    b = [int()] * (N-1)  
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  
    x = [int()] * (Q)  
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

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):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, Q = list(map(int, sys.stdin.readline().split()))
AB = [list(map(int, sys.stdin.readline().split())) for _ in range(N - 1)]
PX = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

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

counts = defaultdict(int)
for p, x in PX:
    counts[p] += x

ans = [0] * (N + 1)
ans[1] = counts[1]
que = deque([1])
while que:
    v = que.popleft()
    cnt = ans[v]
    for u in graph[v]:
        que.append(u)
        ans[u] = counts[u] + cnt

print(*ans[1:])

Solution (Python):



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


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

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

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

ans = [0]*n


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

dfs(0,-1,0)



print(*ans)

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


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

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

new = [0]
tank = []

s = [[] for _ in range(n)]

while len(new) != 0:
    tank = []
    for e in new:
        for p in edge[e]:
            if used[p]:
                s[e].append(p)
                tank.append(p)
                used[p] = False 

    new = tank


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

new = [0]
tank = []
while len(new) != 0:
    tank = []
    for e in new:
        for p in s[e]:
            res[p] += res[e]
            tank.append(p)
    new = tank
for ans in res:
    print(ans,end = ' ')

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    a = I()
    s = input()
    if a >= 3200:
        print(s)
    else:
        print("red")
    return


def B():
    n = I()
    a = LI()
    ans = 0
    for i in a:
        ans += 1/i
    print(1/ans)
    return


def C():
    n = I()
    v = LI()
    q = []
    for i in v:
        heappush(q,i)
    while q:
        x = heappop(q)
        if not q:
            print(x)
            return
        y = heappop(q)
        heappush(q,(x+y)/2)
    return


def D():
    def dfs(x,d,s):
        for y in v[x]:
            if d[y]:
                s += f[y]
                ans[y] = s
                d[y] = 0
                dfs(y,d,s)
                s -= f[y]
    n,q = LI()
    v = [[] for i in range(n)]
    for i in range(n-1):
        a,b = LI()
        a -= 1
        b -= 1
        v[a].append(b)
        v[b].append(a)
    f = [0]*n
    for i in range(q):
        p,x = LI()
        p -= 1
        f[p] += x
    ans = [0]*n
    d = [1]*n
    d[0] = 0
    ans[0] = f[0]
    s = f[0]
    dfs(0,d,s)
    print(*ans)
    return


def E():
    s = input()
    t = input()

    return


def F():

    return


if __name__ == "__main__":
    D()

Solution (Python):
import sys
input=sys.stdin.readline
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())
    a-=1; b-=1
    Edges[a].append(b)
    Edges[b].append(a)
Q=[0]*n
for _ in range(q):
    p,x=map(int,input().split())
    Q[p-1]+=x
Ans=[0]*n
q=deque([0])
Ans[0]=Q[0]
Visited=[False]*n
Visited[0]=True
while q:
    fr=q.pop()
    for to in Edges[fr]:
        if Visited[to]:
            continue
        Ans[to]=Ans[fr]+Q[to]
        Visited[to]=True
        q.append(to)
print(*Ans)

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


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

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

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

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

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

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

while Q:
    x=Q.pop()

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

print(*SCORE[1:])