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
sys.setrecursionlimit(10**7)

n,q=map(int,input().split())
t=[[] for _ in range(n)]
c=[0 for _ in range(n)]

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

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

def dsf(ci,pi):
  for i in t[ci]:
    if i == pi:
      continue
    c[i] += c[ci]
    dsf(i, ci)

dsf(0,-1)

print(*c)

Submission status: Time Limit Exceeded

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

n,q=map(int,input().split())
t=[[] for _ in range(n)]
c=[0 for _ in range(n)]

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

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

def dsf(ci,pi):
  for i in t[ci]:
    if i == pi:
      continue
    c[i] += c[ci]
    dsf(i, ci)

dsf(0,-1)

print(*c)

Submission status: Time Limit Exceeded

Solution code:

if __name__ == "__main__":
    n, q = map(int, input().split())
    incidenceGraph = [[] for j in range(0, n)]
    for i in range(0, n - 1):
        a, b = map((lambda x: int(x) - 1), input().split())
        incidenceGraph[a].append(b)
        incidenceGraph[b].append(a)

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

    visited = [False]*n
    stack = [0]
    while stack:
        i = stack.pop(0)
        ans[i] += operationTuples[i]
        visited[i] = True
        for j in incidenceGraph[i]:
            if visited[j] is False:
                ans[j] += ans[i]
                stack.append(j)
    print(*ans)

Submission status: Time Limit Exceeded

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

point={}

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

ans=[0]*N
stack=[]
stack.append([0,0,-1])
while len(stack)>0:
  nodes=stack.pop()
  node=nodes[0]
  sump=nodes[1]
  parent=nodes[2]
  if node in point:
    sump=sump+point[node]
  ans[node]=sump
  if node in tree:
      childs=tree[node]
      for child in childs:
        if child!=parent:
          stack.append([child,sump,node])

print(*ans)

Submission status: Time Limit Exceeded

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

point={}

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

ans=[0]*N
stack=[]
stack.append([0,0,-1])
while len(stack)>0:
  nodes=stack.pop()
  node=nodes[0]
  sump=nodes[1]
  parent=nodes[2]
  if node in point:
    sump=sump+point[node]
  ans[node]=sump
  if node in tree:
      childs=tree[node]
      for child in childs:
        if child==parent:
          continue
        stack.append([child,sump,node])

print(*ans)

Submission status: Time Limit Exceeded

Solution code:
"""
testing code here
 
https://codeforces.com/blog/entry/70304
"""
 
from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
  
# 'global' variables
n,m = map(int, input().split())
d = []
for i in range(n + 5):
  d.append([])
l = [0]*(n+1)
visted = [False]*(n+1)
  
def dfs(u,v):
    visted[v] = True
    l[v] += l[u]
    for i in d[v]:
        if visted[i] == False:
            dfs(v,i)
    return 
            
for i in range(n-1):
    a,b = map(int, input().split())
    d[a].append(b);d[b].append(a)

for i in range(m):
    a,b = map(int,input().split())
    l[a] += b
dfs(0,1)
print(*l[1:])

Submission status: Time Limit Exceeded

Solution code:
"""
testing code here
 
https://codeforces.com/blog/entry/70304
"""
 
from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
  
# 'global' variables
n,m = map(int, input().split())
d = defaultdict(list)
l = [0]*(n+1)
visted = [False]*(n+1)
  
def dfs(u,v):
    visted[v] = True
    l[v] += l[u]
    for i in d[v]:
        if visted[i] == False:
            dfs(v,i)
    return 
            
for i in range(n-1):
    a,b = map(int, input().split())
    d[a].append(b);d[b].append(a)
 
for i in range(m):
    a,b = map(int,input().split())
    l[a] += b
dfs(0,1)
print(*l[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  4 08:26:06 2019

@author: avina
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
def dfs(u,v, visted,l,d):
    visted[v] = True
    l[v] += l[u]
    for i in d[v]:
        if visted[i] == False:
            dfs(v,i,visted,l,d)
    return 
            
g = defaultdict(list)
n,m = map(int, input().split())
for i in range(n-1):
    a,b = map(int, input().split())
    g[a].append(b);g[b].append(a)
l = [0]*(n+1)
for i in range(m):
    a,b = map(int,input().split())
    l[a] += b
visted = [False]*(n+1)

dfs(0,1,visted,l,g)
print(*l[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  4 08:26:06 2019

@author: avina
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
def dfs(u,v,l,d):
    l[v] += l[u]
    for i in d[v]:
        if i == v:
            continue
        dfs(v,i,l,d)
            
g = defaultdict(list)
n,m = map(int, input().split())
for i in range(n-1):
    a,b = map(int, input().split())
    g[a].append(b);g[b].append(a)
l = [0]*(n+1)
for i in range(m):
    a,b = map(int,input().split())
    l[a] += b

dfs(0,1,l,g)
print(*l[1:])

Submission status: Time Limit Exceeded

Solution code:
# 頂点n個 辺iは頂点aiとbiを結ぶ 各頂点には初期値0のカウンターがある
# 操作j: 頂点pjを根とする部分木に含まれるすべてのカウンターにxj足す
# Q回の操作のあと各カウンターの数値を求める

# 方針: 操作の際に最初に部分木の根にだけ点を加え最後に親の点を子に与えていく
import sys
sys.setrecursionlimit(10**9)

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

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


def dfs(now=0, prev=-1):
    for i in range(len(connect[now])):
        kid = connect[now][i]
        if kid == prev:
            continue
        count[kid] += count[now]
        dfs(kid, now)


dfs()

for i in range(n):
    print(count[i])

Submission status: Time Limit Exceeded

Solution code:
# 頂点n個 辺iは頂点aiとbiを結ぶ 各頂点には初期値0のカウンターがある
# 操作j: 頂点pjを根とする部分木に含まれるすべてのカウンターにxj足す
# Q回の操作のあと各カウンターの数値を求める

# 方針: 操作の際に最初に部分木の根にだけ点を加え最後に親の点を子に与えていく
import sys
sys.setrecursionlimit(10**6)

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

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


def dfs(now=0, prev=-1):
    for i in range(len(connect[now])):
        kid = connect[now][i]
        if kid == prev:
            continue
        count[kid] += count[now]
        dfs(kid, now)


dfs()

for i in range(n):
    print(count[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
N,Q=map(int, input().split())
tree_dist={}
for i in range(N):
    tree_dist[i+1] = []
for i in range(N-1):
    a,b = map(int, input().split())
    tree_dist[a].append(b)
    tree_dist[b].append(a)
    
z=[0]*N
for i in range(Q):
    p,x = map(int, input().split())
    z[p-1] += x

def saiki(i,p):
    L = list(tree_dist[i])
    for l in L:
        if l != p:
            z[l-1] += z[i-1]
            saiki(l,i)
saiki(1,0)
print(" ".join(map(str,z)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
N,Q=map(int, input().split())
tree_dist={}
for i in range(N):
    tree_dist[i+1] = set()
for i in range(N-1):
    a,b = map(int, input().split())
    tree_dist[a].add(b)
    tree_dist[b].add(a)
    
z=[0]*N
for i in range(Q):
    p,x = map(int, input().split())
    z[p-1] += x

def saiki(i,p):
    L = list(tree_dist[i])
    for l in L:
        if l == p:
            continue
        z[l-1] += z[i-1]
        saiki(l,i)
saiki(1,0)
print(" ".join(map(str,z)))

Submission status: Time Limit Exceeded

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

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

def dfs(p,par):
  for i in ab[p-1]:
    if i!=par:
      P[i-1]+=P[p-1]
      dfs(i,p)

dfs(1,-12345)
print(" ".join(map(str,P)))

Submission status: Time Limit Exceeded

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

P, X = [], []
for j in range(Q):
  p, x = [int(px) for px in input().split()]
  P.append(p)
  X.append(x)

Score = [0] * N
for j in range(Q):
  Score[P[j] - 1] += X[j]

Now = [1]
cnt = 1
while cnt < N:
  for nw in Now:
    for nxt in AB[nw - 1]:
      Score[nxt - 1] += Score[nw - 1]
      AB[nxt - 1].remove(nw)
      Now.append(nxt)
      cnt += 1
    Now.remove(nw)

print(*Score)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  4 08:26:06 2019

@author: avina
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
def dfs(u,v, visted,l,d):
    visted[v] = True
    l[v] += l[u]
    for i in d[v]:
        if visted[i] == False:
            dfs(v,i,visted,l,d)
            
g = defaultdict(list)
n,m = map(int, input().split())
for i in range(n-1):
    a,b = map(int, input().split())
    g[a].append(b);g[b].append(a)
l = [0]*(n+1)
for i in range(m):
    a,b = map(int,input().split())
    l[a] += b
visted = [False]*(n+1)

dfs(0,1,visted,l,g)
print(*l[1:])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)


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


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[b].append(a)
    tree[a].append(b)
add = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    add[p] += x
dfs(0, -1)
print(*add)

Submission status: Time Limit Exceeded

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

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


count=[-1]*N
c=0
def dfs(p):
  global c
  c+=P[p-1]
  count[p-1]=c
  for i in ab[p-1]:
    if count[i-1]==-1:
      dfs(i)
  c-=P[p-1]

dfs(1)
print(" ".join(map(str,count)))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    from collections import deque
    
    input=sys.stdin.readline
    N,Q = map(int,input().split())
    AB = [[x for x in map(int,input().split())] for _ in range(N-1)]
    PX = [[x for x in map(int,input().split())] for _ in range(Q)]

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

    cnt = [0]*N
    for p,x in PX:
        cnt[p-1] += x

    color = ["WHITE"] * (N+1)
    stack = []
    stack = deque(stack)

    def dfs(u):
        stack.append(u)
        color[u] = "GRAY"

        while len(stack) > 0:
            u = stack[-1]
            if len(G[u]) == 0:
                color[u] = "BLACK"
                stack.pop()
            else:
                u_next = G[u].pop(0)
            if color[u_next] == "WHITE":
                color[u_next] = "GRAY"
                cnt[u_next-1] += cnt[u-1]
                stack.append(u_next)
    dfs(1)
    print(*cnt)


    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,Q=[int(s) for s in input().split()]
e_list=[[int(s) for s in input().split()] for i in range(N-1)]
q_list=[[int(s) for s in input().split()] for i in range(Q)]
#まずQを頂点ごとに集計
v_list=[0 for s in range(N+1)]
for q in q_list:
  v_list[q[0]]+=q[1]
  
oya_list=[0 for s in range(N+1)] #親のリスト
ko_list=[set() for s in range(N+1)] #子のリスト
de_list=[0 for s in range(N+1)] #訪れたことがあるか？
position=1 #現在の場所
vertex_num=1 #訪れた頂点の数
edge_count=0 #消した辺の数
counter=v_list[1]
ans_list=[0 for s in range(N+1)]
ans_list[1]=v_list[1]

while vertex_num<N:
  #一度も訪れたことがない場合、現在の場所の子をリストアップする
  if de_list[position]==0:
    de_list[position]=1
    i=0
    while True:
      if i+edge_count>=N-1:
        break
      if e_list[i][0]==position:
        ko_list[position].add(e_list[i][1])
        oya_list[e_list[i][1]]=position
        e_list.pop(i)
        edge_count+=1
      elif e_list[i][1]==position:
        ko_list[position].add(e_list[i][0])
        oya_list[e_list[i][0]]=position
        e_list.pop(i)
        edge_count+=1
      else:
        i+=1
  ##子が存在しない->一つ戻る
  while len(ko_list[position])==0:
    position=oya_list[position]
  ##子が存在する->移動
  counter=ans_list[position]  #カウンターを元に戻す
  position=ko_list[position].pop()
  counter+=v_list[position]   #カウンターの更新
  ans_list[position]=counter  
  vertex_num+=1
print(*ans_list[1:])

Submission status: Time Limit Exceeded

Solution code:
N,Q=[int(s) for s in input().split()]
e_list=[[int(s) for s in input().split()] for i in range(N-1)]
q_list=[[int(s) for s in input().split()] for i in range(Q)]
#まずQを頂点ごとに集計
v_list=[0 for s in range(N+1)]
for q in q_list:
  v_list[q[0]]+=q[1]
oya_list=[0 for s in range(N+1)] #親のリスト
ko_list=[set() for s in range(N+1)] #子のリスト
position=1 #現在の場所
reached=[1] #訪れた頂点
vertex_num=1 #訪れた頂点の数
edge_count=0 #消した辺の数
counter=v_list[1]
ans_list=[0 for s in range(N+1)]
ans_list[1]=v_list[1]
while vertex_num<N:
  #現在の場所の子をリストアップする
  i=0
  while True:
    if i+edge_count>=N-1:
      break
    if e_list[i][0]==position:
      ko_list[position].add(e_list[i][1])
      oya_list[e_list[i][1]]=position
      e_list.pop(i)
      edge_count+=1
    elif e_list[i][1]==position:
      ko_list[position].add(e_list[i][0])
      oya_list[e_list[i][0]]=position
      e_list.pop(i)
      edge_count+=1
    else:
      i+=1
  ##子が存在しない->一つ戻る
  while len(ko_list[position])==0:
    position=oya_list[position]
    counter=ans_list[position]
    #print(position,vertex_num)
  ##子が存在する->移動
  if len(ko_list[position])>0:
    position=ko_list[position].pop()
    counter+=v_list[position]
    ans_list[position]=counter
    vertex_num+=1
    #print(position,vertex_num)
#print(oya_list)
#print(ko_list)
print(*ans_list[1:])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
sys.setrecursionlimit(1000000)
# MOD = 10**9 + 7

def solve(f):
    n, q = f.read_int_list()
    g = collections.defaultdict(list)
    for _ in xrange(n-1):
        a, b = f.read_int_list()
        g[a].append(b)
        g[b].append(a)

    c = [0] * (n+1)

    for _ in xrange(q):
        p, x = f.read_int_list()
        c[p] += x

    vis = [False] * (n+1)
    def dfs(v, ci):
        vis[v] = True
        c[v] += ci
        for vi in g[v]:
            if not vis[vi]:
                dfs(vi, c[v])

    dfs(1, 0)

    return " ".join(map(str, c[1:]))


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
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)

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

def dfs(v, par):
    for nv in edges[v]:
        if nv == par:
            continue
        points[nv] += points[v]
        dfs(nv, v)

dfs(0, -1)

print(*points)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
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)

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

def dfs(v, par):
    for nv in edges[v]:
        if nv == par:
            continue
        points[nv] += points[v]
        dfs(nv, v)

dfs(0, -1)

print(*points)

Submission status: Time Limit Exceeded

Solution code:
import queue
import sys
sys.setrecursionlimit(10**7)
N, Q = map(int, input().split())

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

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

def dfs(v, par):
    for nv in edges[v]:
        if nv == par:
            continue
        points[nv] += points[v]
        dfs(nv, v)

dfs(0, -1)

print(*points)

Submission status: Time Limit Exceeded

Solution code:
import queue
import sys
sys.setrecursionlimit(10**7)
N, Q = map(int, input().split())

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

points = [0]*N
PX = [list(map(int, input().split())) for _ in range(Q)]
for px in PX:
    points[px[0]-1] += px[1]

def dfs(v, par):
    for nv in edges[v]:
        if nv == par:
            continue
        points[nv] += points[v]
        dfs(nv, v)

dfs(0, -1)

print(*points)

Submission status: Time Limit Exceeded

Solution code:
import queue
N, Q = map(int, input().split())

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

points = [0]*N
PX = [list(map(int, input().split())) for _ in range(Q)]
for px in PX:
    points[px[0]-1] += px[1]

res = [0]*N
res[0] = points[0]

color = [0]*N
color[0] = 1

#print(edges)
#print(points)
#print(color)
#print(res)

q = queue.Queue()
q.put(0)

while not q.empty():
    current_node = q.get() # 0
#    print(' current:', current_node)
    children_nodes = edges[current_node]
    for node in children_nodes:
        if color[node] == 1: continue
        color[node] = 1
        points[node] += points[current_node]
        q.put(node)
#        print('  color', color)
print(*points)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, q = map(int, input().split(' '))
    import sys
    sys.setrecursionlimit(10**7)

    visit = [True for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    next_visit = [0]
    visit[0] = False
    while len(next_visit) != 0:
        t = next_visit.pop(0)
        for s in dic[t]:
            if visit[s]:
                point[s] += point[t]
                visit[s] = False
                next_visit.append(s)
    print(" ".join(map(str, point)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, q = map(int, input().split(' '))
    import sys
    sys.setrecursionlimit(10**7)

    visit = [True for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    next_visit = [0]
    visit[0] = False
    counter = 1
    while counter < n:
        t = next_visit.pop(0)
        for s in dic[t]:
            if visit[s]:
                point[s] += point[t]
                visit[s] = False
                counter += 1
                next_visit.append(s)
    print(" ".join(map(str, point)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, q = map(int, input().split(' '))
    import sys
    sys.setrecursionlimit(10**7)

    visit = [True for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    next_visit = [[0, dic[0]]]
    visit[0] = False
    counter = 1
    while counter < n:
        t = next_visit.pop(0)
        for s in t[1]:
            if visit[s]:
                point[s] += point[t[0]]
                visit[s] = False
                counter += 1
                next_visit.append([s, dic[s]])
    print(" ".join(map(str, point)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

N, Q = map(int, input().split())
 
graph = [[] for i in range(N)]
ans = [ 0 for i in range(N)]
 
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
 
for _ in range(Q):
    p, x = map(int, input().split())
    ans[p-1] += x
    
def dfs(v, p):
    for u in graph[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u,v)
 
dfs(0, -1)
print(*ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, q = map(int, input().split(' '))
    import sys
    sys.setrecursionlimit(10**7)

    visit = [0 for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    next_visit = [[0, dic[0]]]
    visit[0] = 1
    counter = 1
    while counter < n:
        t = next_visit.pop(0)
        for s in t[1]:
            if visit[s] == 0:
                point[s] += point[t[0]]
                visit[s] = 1
                counter += 1
                next_visit.append([s, dic[s]])
    print(" ".join(map(str, point)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, q = map(int, input().split(' '))
    import sys
    sys.setrecursionlimit(10**7)

    visit = set([])
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    next_visit = [[0, dic[0]]]
    visit.add(0)
    counter = 1
    while counter < n:
        t = next_visit.pop(0)
        for s in t[1]:
            if set([s]) <= visit:
                continue
            else:
                point[s] += point[t[0]]
                visit.add(s)
                counter += 1
                next_visit.append([s, dic[s]])
    print(" ".join(map(str, point)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, q = map(int, input().split(' '))
    import sys
    sys.setrecursionlimit(10**7)

    visit = [0 for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    next_visit = [[0, dic[0]]]
    visit[0] = 1
    counter = 1
    while counter < n:
        for i in range(len(next_visit)):
            t = next_visit.pop(0)
            for s in t[1]:
                if visit[s] == 0:
                    point[s] += point[t[0]]
                    visit[s] = 1
                    counter += 1
                    next_visit.append([s, dic[s]])
    print(" ".join(map(str, point)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, q = map(int, input().split(' '))
    import sys
    sys.setrecursionlimit(10**7)

    visit = [0 for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    next_visit = [[0, dic[0]]]
    visit[0] = 1
    while any(x == 0 for x in visit):
        for i in range(len(next_visit)):
            t = next_visit.pop(0)
            for s in t[1]:
                if visit[s] == 0:
                    point[s] += point[t[0]]
                    visit[s] = 1
                    next_visit.append([s, dic[s]])
    print(" ".join(map(str, point)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, q = map(int, input().split(' '))
    import sys
    sys.setrecursionlimit(10**7)

    visit = [0 for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]

    def add_p(num):
        if visit[num] == 1:
            return 0
        visit[num] = 1
        for i in dic[num]:
            if visit[i] == 0:
                point[i] += point[num]
                add_p(i)
        return 0

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

    add_p(0)

    print(" ".join(map(str, point)))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split(' '))
import sys
sys.setrecursionlimit(10**7)

visit = [0 for _ in range(n)]
point = [0 for _ in range(n)]
dic = [[] for _ in range(n)]

def add_p(num):
    if visit[num] == 1:
        return 0
    visit[num] = 1
    for i in dic[num]:
        if visit[i] == 0:
            point[i] += point[num]
            add_p(i)
    return 0

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

add_p(0)

print(*point)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**7)
    n, q = input().split(' ')
    visit = [0 for _ in range(int(n))]
    point = [0 for _ in range(int(n))]
    dic = [[] for _ in range(int(n))]

    def add_p(num):
        if visit[num] == 1:
            return 0
        visit[num] = 1
        for i in dic[num]:
            if visit[i] == 0:
                point[i] += point[num]
                add_p(i)
        return 0
    for _ in range(int(n)-1):
        a, b = input().split(' ')
        dic[int(a)-1].append(int(b)-1)
        dic[int(b)-1].append(int(a)-1)
    for _ in range(int(q)):
        p, x = input().split(' ')
        point[int(p)-1] += int(x)
    add_p(0)
    print(*point)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**7)
    n, q = map(int, input().split(' '))
    visit = [0 for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]

    def add_p(num):
        if visit[num] == 1:
            return 0
        visit[num] = 1
        for i in dic[num]:
            if visit[i] == 0:
                point[i] += point[num]
                add_p(i)
        return 0

    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    add_p(0)
    print(*point)

main()