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



Sample input:
4 3
1 2
2 3
2 4
2 10
1 100
3 1


Sample output:
100 110 111 110

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

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

    for i in range(N-1):
        link[a[i]-1].append([b[i]-1, cost[i]])
        link[b[i]-1].append([a[i]-1, cost[i]])

    def dfs(v, c, p):
        for nv in link[v]:
            if nv[0] == p:
                continue
            cost[nv[0]] = c + cost[nv[0]]
            dfs(nv[0], cost[nv[0]], v)
        return

    dfs(0, cost[0], -1)

    print(*cost)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

N, Q = map(int, input().split())
link = [[] for _ in range(N)]
a = [None] * (N-1)
b = [None] * (N-1)
for i in range(N-1):
    a[i], b[i] = map(int, input().split())

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

for i in range(N-1):
    link[a[i]-1].append([b[i]-1, cost[i]])
    link[b[i]-1].append([a[i]-1, cost[i]])

def dfs(v, c, p):
    for nv in link[v]:
        if nv[0] == p:
            continue
        cost[nv[0]] = c + cost[nv[0]]
        dfs(nv[0], cost[nv[0]], v)
    return

dfs(0, cost[0], -1)

print(*cost)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

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

for i in range(N-1):
    link[a[i]-1].append([b[i]-1, cost[i]])
    link[b[i]-1].append([a[i]-1, cost[i]])

def dfs(v, c, p):
    for nv in link[v]:
        if nv[0] == p:
            continue
        cost[nv[0]] = c + cost[nv[0]]
        dfs(nv[0], cost[nv[0]], v)
    return

dfs(0, cost[0], -1)

print(*cost)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

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

for i in range(N-1):
    link[a[i]-1].append([b[i]-1, cost[i]])
    link[b[i]-1].append([a[i]-1, cost[i]])

nums = [0] * N
def dfs(v, c, p):
    for nv in link[v]:
        if nv[0] == p:
            continue
        nums[nv[0]] += c + cost[nv[0]]
        dfs(nv[0], c + cost[nv[0]], v)
    return

nums[0] = cost[0]
dfs(0, cost[0], -1)

print(*nums)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

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

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

dfs(0, -1)

print(*ans)

Submission status: Time Limit Exceeded

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

tree = {}

for i in range(n-1):
    s,t = map(int,input().split())
    if s-1 not in tree:
        tree[s-1] = [t-1]
    else:
        tree[s-1].append(t-1)
    if t-1 not in tree:
        tree[t-1] = [s-1]
    else:
        tree[t-1].append(s-1)

p_sum = [0]*n

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

res = [0]*n
q = []
checked = [False]*n
q.append(0)
checked[0]=True
res[0]=p_sum[0]

while 1:
    s = q.pop(0)
    for t in tree[s]:
        if not checked[t]:
            q.append(t)
            checked[t] = True
            res[t] = res[s]+p_sum[t]
    if q == []: break
            

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

Submission status: Time Limit Exceeded

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

N,Q = map(int,input().split())
ablist = [[] for _ in range(N)]
anslist = [0 for i in range(N)]

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

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

for q,x in pxlist:
    anslist[q-1] += x

def dfs(nownode,oya=-1):
    for nextnode in ablist[nownode]:
        if oya == nextnode:
            continue
        anslist[nextnode] += anslist[nownode]
        dfs(nextnode,nownode)
dfs(0,-1)
print(*anslist)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
def dfs(i,we):
    global adj,co,res,vis
    we+=co[i]
    res[i]=we
    for j in adj[i]:
        if vis[j]==0:
            vis[j]=1
            adj[j].remove(i)
            dfs(j,we)
            
    
n,q=map(int,input().split())
adj=[set() for i in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    adj[a-1].add(b-1)
    adj[b-1].add(a-1)
co=[0]*n
for i in range(q):
    a,b=map(int,input().split())
    co[a-1]+=b
res=[0]*n
vis=[0]*n
vis[0]=1
dfs(0,0)
for i in range(n):
    print(res[i],end=" ")
print()

Submission status: Time Limit Exceeded

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

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

def dps(i,parent): 
    for j in graph[i]:
        if j == parent:
            continue
        val[j] += val[i]
        dps(j,i)
                
dps(0,-1)
print(*val)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
N,Q =  map(int,input().split())
graph = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

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

def dps(i,parent): 
    for j in graph[i]:
        if j == parent:
            continue
        val[j] += val[i]
        dps(j,i)
                
dps(0,0)
print(*val[0:])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
def dfs(i,we):
    global adj,co,res
    we+=co[i]
    res[i]=we
    for j in adj[i]:
        if vis[j]==0:
            vis[j]=1
            dfs(j,we)
            
    
n,q=map(int,input().split())
adj=[set() for i in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    adj[a-1].add(b-1)
    adj[b-1].add(a-1)
co=[0]*n
for i in range(q):
    a,b=map(int,input().split())
    co[a-1]+=b
res=[0]*n
vis=[0]*n
vis[0]=1
dfs(0,0)
for i in range(n):
    print(res[i],end=" ")
print()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

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

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

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

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

addc(1,-1)

c.pop(0)

print(*c)

Submission status: Time Limit Exceeded

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

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

def dps(i,parent): 
    for j in graph[i]:
        if j == parent:
            continue
        val[j] += val[i]
        dps(j,i)
                
dps(0,0)
print(*val[0:])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

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

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

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

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

addc(1,-1)

c.pop(0)

print(*c)

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
#graph = [[] for i in range(200010)]
#ans = [0 for i in range(200010)]
graph = [set() for i in range(N)]
ans = [0 for i in range(N)]
import sys
#sys.setrecursionlimit(100000)
sys.setrecursionlimit(10**7)


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

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

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

    ans[p] += x

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

print(*ans)

Submission status: Time Limit Exceeded

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

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

def dps(i,parent): 
    for j in graph[i]:
        if j == parent:
            continue
        val[j] += val[i]
        dps(j,i)
                
dps(0,0)
for i in val:
    print(i,end=" ")

Submission status: Time Limit Exceeded

Solution code:
import queue

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

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

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

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

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

Submission status: Time Limit Exceeded

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

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

def dps(i,parent): 
    for j in graph[i]:
        if j != parent:
            val[j] += val[i]
            dps(j,i)
                
dps(0,0)
for i in val:
    print(i,end=" ")

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
#graph = [[] for i in range(200010)]
#ans = [0 for i in range(200010)]
graph = [set() for i in range(N)]
ans = [0 for i in range(N)]
import sys
#sys.setrecursionlimit(100000)
sys.setrecursionlimit(10**7)


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

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

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

    #ans[p] += x
    ans[p-1] += x

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

print(*ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

def addc(p,e,c,total,check):
  c[p] += total
  total = c[p]
  check.append(p)
  if(len(e[p]) == 0):
    return 0

  else:
    for i in e[p]:
      if(i not in check):
        addc(i,e,c,total,check)

  return 0

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

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

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

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

_ = addc(1,e,c,0,[])

c.pop(0)

print(*c)

Submission status: Time Limit Exceeded

Solution code:
import queue

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

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
sons_of_root = []
edges = {}
for _ in range(N-1):
    a,b = map(int,input().split())
    if a==1:
    	sons_of_root.append(b)
    
    if a in edges.keys():
        edges[a] = edges[a]+[b]
    else:
        edges[a] = [b]
            
    if b in edges.keys():
        edges[b] = edges[b]+[a]
    else:
        edges[b] = [a]
        
counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
        
queue = [sons_of_root]
cqueue = [[counts[1]]*len(sons_of_root)]
ignores = [1]
results = [0]*(N+1)
results[1] = counts[1]
while queue != []:
    vals = queue.pop(0)
    cvals = cqueue.pop(0)
    for x,c in zip(vals,cvals):
        results[x] = counts[x]+c
        sons = [val for val in edges[x] if val not in ignores]
        queue.append(sons)
        cqueue.append([results[x]]*len(sons))
        ignores.append(x)
for i in range(1,N+1):
    print(results[i], end=' ')

Submission status: Time Limit Exceeded

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


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

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

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

    ans[p] += x

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

print(*ans[:N])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

quu = [1]
visited = []
while len(quu) > 0:
    now = quu.pop(0)
    visited += [now]
    child = root[now]
    for i in range(len(child)):
        if not child[i] in visited:
            quu += [child[i]]
            cnt[child[i]] += cnt[now]

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

Submission status: Time Limit Exceeded

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

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

def dps(i): 
    visited[i] = False
    for j in graph[i]:
        if visited[j]:
            val[j] += val[i]
            dps(j)
                
dps(0)
for i in val:
    print(i,end=" ")

Submission status: Time Limit Exceeded

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

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

def dps(i): 
    visited[i] = False
    if sum(visited) == 0 :
        return 1
    else :
        for j in graph[i]:
            if visited[j]:
                val[j] += val[i]
                dps(j)
                
dps(0)
for i in val:
    print(i,end=" ")

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
sons = {1:[]}
edges = {}
for _ in range(N-1):
    a,b = map(int,input().split())
    if a==1:
    	sons[a] += [b]
    if a in edges.keys():
        edges[a] = edges[a]+[b]
    else:
        edges[a] = [b]
            
    if b in edges.keys():
        edges[b] = edges[b]+[a]
    else:
        edges[b] = [a]

counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
        
stack = sons[1]
count_stack = [counts[1]]*len(stack)
ignores = set([1])
results = [0]*(N+1)
results[1] = counts[1]
next_stack = []
next_cstack = []
while stack != []:
    next_stack = []
    next_cstack = []
    for x,c in zip(stack,count_stack):
        results[x] = counts[x]+c
        sons[x] = set(edges[x])-ignores
        next_stack += sons[x]
        next_cstack += [results[x]]*len(sons[x])
        ignores = ignores | set([x])
    stack = next_stack
    count_stack = next_cstack
print(' '.join([str(results[i]) for i in range(1,N+1)]))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

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

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

check = []
def rec(node, checked):
    checked.append(node)
    for k in root[node]:
        if not k in checked:
            cnt[k] += cnt[node]
            rec(k, checked)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

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

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

check = []
def rec(node, checked):
    for k in root[node]:
        if not k in checked:
            cnt[k] += cnt[node]
            if not node in checked:
                checked.append(node)
            rec(k, checked)

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

Submission status: Time Limit Exceeded

Solution code:

import bisect
import collections
import itertools


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

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

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

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


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

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

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

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

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

Submission status: Time Limit Exceeded

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

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


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

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


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



down_tree(1, 1)

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

Submission status: Time Limit Exceeded

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

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


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

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


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



down_tree(1, 1)

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

Submission status: Time Limit Exceeded

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

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


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

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


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


down_tree(1, 1)

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

Submission status: Time Limit Exceeded

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

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


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

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


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


down_tree(1, 1)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n,q = map(int,input().split())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)

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

#入力

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

dfs(-1,0,0)

print(*ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- condig:utf-8 -*-

import sys
sys.setrecursionlimit(100000000)

def dfs(p, parents, count):
  parents.append(p)
  count += o.get(p, 0)
  for i in g[p]:
    if not i in parents:
      dfs(i, parents, count)
  counter[p] = counter.get(p, 0) + count
  parents.remove(p)
 

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

g = {}
o = {}
counter = {}
for i in range(n-1):
  a,b= map(int, input().split())
  if not g.get(a, False):
    g[a] = [b]
  else:
    if not b in g[a]:
      g[a].append(b)
  if not g.get(b, False):
    g[b] = [a]
  else:
    if not a in g[b]:
      g[b].append(a)

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

dfs(1, [], 0)
for i in range(1, n+1):
  print(counter.get(i, 0), end=' ')

Submission status: Time Limit Exceeded

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

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


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

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


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


down_tree(1, 1)

print(*score[1:])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n,q = map(int,input().split())
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    tree[a]+=[b]
    tree[b]+=[a]

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

#入力

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

dfs(-1,0,0)

print(*ans)

Submission status: Time Limit Exceeded

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

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


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

score = [0]*(n+1)

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


def down_tree(index, parent=-1):
    global score
    if parent != -1:
        score[index] += score[parent]
    for i in to[index]:
        if i == parent:
            continue
        down_tree(i, index)
    return


down_tree(1)

print(*score[1:])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n,q = map(int,input().split())
tree = [[] for i 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)

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

#入力

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

dfs(-1,0,0)

print(*ans)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

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

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

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

Submission status: Time Limit Exceeded

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

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


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

score = [0]*(n+1)

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


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


down_tree(1)

for i in range(1,n+1):
    print(score[i])

Submission status: Time Limit Exceeded

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

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


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

score = [0]*(n+1)

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


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


down_tree(1)

print(*score[1:])

Submission status: Time Limit Exceeded

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

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


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

score = [0]*n

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


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


down_tree(0)

print(*score)