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: Runtime Error

Solution code:
N, Q = list(map(int, input().split()))
nodes = {i:[] for i in range(N)}
for i in range(N-1):
  r, e = list(map(int, input().split()))
  r, e = r - 1, e - 1
  nodes[r].append(e)
  nodes[e].append(r)
#print(nodes)

result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i == prev:
      continue
    result[i] += result[r]
    dfs(i, r)

dfs(0)
print(" ".join(map(str, result)))

Submission status: Runtime Error

Solution code:
N, Q = list(map(int, input().split()))
nodes = {i:[] for i in range(1, N+1)}
for i in range(N-1):
  r, e = list(map(int, input().split()))
  nodes[r].append(e)
  nodes[e].append(r)
#print(nodes)

result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i == prev:
      continue
    result[i-1] += result[r-1]
    dfs(i, r)

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

Submission status: Runtime Error

Solution code:
N, Q = [int(i) for i in input().split()]
G = [[] for _ in range(N)]
counter = [0] * N
res = [0] * N
visited = [0] * N

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

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

def dfs(G, v, p):
    visited[v] = 1
    res[v] = res[p]+counter[v]
    for nv in G[v]:
        if visited[nv]: continue
        dfs(G, nv, v)

dfs(G, 0, 0)
print(*res)

Submission status: Runtime Error

Solution code:
import copy
N, Q = list(map(int, input().split()))
nodes = dict()
for i in range(N-1):
  r, e = list(map(int, input().split()))
  if r not in nodes:
    nodes[r] = [e]
  else:
    nodes[r].append(e)
#print(nodes)

result = [0] * N
edges = []
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s
  if r in nodes:
    edges = copy.copy(nodes[r])
  while len(edges) != 0:
    r = edges.pop(0)
    result[r-1] += s
    if r in nodes:
      edges.extend(nodes[r])
      #print(edges, nodes[r])
  #print(result)
print(" ".join(result))

Submission status: Runtime Error

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

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

for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    graph[u].append(v)
    graph[v].append(u)

ans = [0] * n

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


def dfs(cur, par):
    for chl in graph[cur]:
        if chl == par:
            continue
        ans[chl] += ans[cur]
        dfs(chl, cur)


dfs(0, -1)
print(*ans)

Submission status: Runtime Error

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

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

for i in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    graph[u].append(v)
    graph[v].append(u)

ans = [0] * n

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


def dfs(cur, par):
    for chl in graph[cur]:
        if chl == par:
            continue
        ans[chl] += ans[cur]
        dfs(chl, cur)


dfs(0, -1)
print(*ans)

Submission status: Runtime Error

Solution code:
N, Q=map(int, input().split())
T=[[] 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)
P=[0]*N
ans=[0]*N
for i in range(Q):
  p,x=map(int, input().split())
  P[p-1]+=x

def dfs(start, pre_node, pre_val):
  now_val=(pre_val+P[start])
  ans[start]=now_val
  
  for next_node in T[start]:
    if next_node==pre_node:
      continue
    dfs(next_node, start,now_val)
    
dfs(0,-1,0)   
for a in ans:
  print(a, end=" ")

Submission status: Runtime Error

Solution code:
N, Q=map(int, input().split())
T=[[] 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)
P=[0]*N
ans=[0]*N
for i in range(Q):
  p,x=map(int, input().split())
  P[p-1]+=x

def dfs(start, pre_node, pre_val):
  now_val=(pre_val+P[start])
  ans[start]=now_val
  
  for next in T[start]:
    if next==pre_node:
      continue
    dfs(next, start,now_val)
    
dfs(0,0,0)   
for a in ans:
  print(a, end=" ")

Submission status: Runtime Error

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

def dfs(start, pre_node, pre_val):
  now_val=(pre_val+P[start])
  ans[start]=now_val
  
  for next in T[start]:
    dfs(next, start,now_val)
    
dfs(0,0,0)   
for a in ans:
  print(a, end=" ")

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(200000)


def dfs(graph,start,point,mark_dict):
    global tree_point
    mark_dict[start] = True
    tree_point[start] += point
    for target in graph[start]:
        if target not in mark_dict:
            mark_dict[target] = True
            dfs(graph,target,point,mark_dict)


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

tree = [[] for _ in range(n+1)]
tree_point = [0] * (n+1)

for _ in range(n-1):
    array = list(map(int, input().split()))
    tree[array[0]].append(array[1])

for _ in range(q):
    array = list(map(int, input().split()))
    dfs(tree,start=array[0],point=array[1],mark_dict={})

for i in range(1, n+1):
    print(tree_point[i], end=' ')

Submission status: Runtime Error

Solution code:
def dfs(graph,start,point,mark_dict):
    global tree_point
    mark_dict[start] = True
    tree_point[start] += point
    for target in graph[start]:
        if target not in mark_dict:
            mark_dict[target] = True
            dfs(graph,target,point,mark_dict)


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

tree = [[] for _ in range(n+1)]
tree_point = [0] * (n+1)

for _ in range(n-1):
    array = list(map(int, input().split()))
    tree[array[0]].append(array[1])

for _ in range(q):
    array = list(map(int, input().split()))
    dfs(tree,start=array[0],point=array[1],mark_dict={})

print(tree_point)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
g=[[] for _ in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
ans=[0 for _ in range(n)]
def dfs(u,par):
    global ans
    if par!=-1:
        ans[u]+=ans[par]
    for v in g[u]:
        if v==par:
            continue
        dfs(v,u)
for _ in range(q):
    p,x=map(int,input().split())
    p-=1
    ans[p]+=x
dfs(0,-1)
print(*ans)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
g=[[] for _ in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
ans=[0 for _ in range(n)]
def dfs(u,par):
    if par!=-1:
        ans[u]+=ans[par]
    for v in g[u]:
        if v==par:
            continue
        dfs(v,u)
for _ in range(q):
    p,x=map(int,input().split())
    p-=1
    ans[p]+=x
dfs(0,-1)
print(*ans)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
g=[[] for _ in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
val=[0 for _ in range(n)]
ans=[0 for _ in range(n)]
def dfs(u,par):
    if par!=-1:
        ans[u]=val[u]+ans[par]
    else:
        ans[u]=val[u]
    for v in g[u]:
        if v==par:
            continue
        dfs(v,u)
for _ in range(q):
    p,x=map(int,input().split())
    p-=1
    val[p]+=x
dfs(0,-1)
print(' '.join(map(str,ans)))

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<queue>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#include<string>// 加法 S.size() s.substr(l,r=s.size()-l) -> l文字目からr文字分
#include<cmath>//三角関数,rad
#include<algorithm>//__gcd()　lower_bound(a+l,a+r,x)-a でindex
// vector -> min/max({vector}) 
// 配列   -> *min_element(a+l,a+r)  *忘れず lとrで[l,r) 
//reverse(a+l,a+r)   配列aの[l,r)を逆順に strはreverse(S.begin(),S.end())
//sort(a+l,a+r,greater <int>()) 配列aの[l,r)昇順sort,大きい順はgreater<type>()
//vectorならv.begin(),v.end()
#include<ctime>//"clock()/CLOCKS_PER_SEC で秒数
#include<vector>//v.push_back(x),v.pop_back()
#include<queue>//q.push(x),front()で先頭を返す,pop()で削除, size(),empty()
#include<functional>//vector,queueと併用でpriority_queue
//宣言は priority_queue< Type, vector<Type>, greater<Type>> Q1; 
// pq.push(x),top()で参照 pop()で削除   greaterで最小 lessで最大がtopに
using namespace std;
typedef pair<int,int> P;
const int inf = 10e7;

int main(){

int n,qq,a,b;
cin>>n>>qq;
vector<vector<int>> edges(n);
vector<vector<int>> rank;
rep(z,n-1){cin>>a>>b;
edges.at(--a).emplace_back(--b);
edges.at(b).emplace_back(a);
}
vector<int> bb(1,0);
rank.emplace_back(bb);
vector<unsigned long long> cnter(n,0ull),res(n+1,0ull);
vector<int> par(n,-1);
queue<int> q;
par.at(0)=n;
q.push(0);
q.push(-1);vector<int> v;
while(!q.empty()){
  int a=q.front();q.pop();
  if (a==-1){
    if(v.empty())break;
    else{rank.emplace_back(v);q.push(-1);v={};}
    }
  else{
    for(int k:edges.at(a)){
      if (par.at(k)==-1){par.at(k)=a;v.emplace_back(k);q.push(k);}
    }}
}
rep(eem,qq){
  unsigned long long r,v;cin>>r>>v;cnter.at(--r)=cnter.at(r)+v;
}
res.at(0)=cnter.at(0);
for(vector<int> vv:rank){for (int jj:vv){res.at(jj)=res.at(par.at(jj))+cnter.at(jj);
                                        }}
rep(u,n-1){cout<<res.at(u)<<' ';}cout<<res.at(n-1)<<'\n';
//rep(i,n){cout<<cnter.at(i)<<'a';}
}

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N - 1)]
P = [list(map(int, input().split())) for _ in range(Q)]

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

def dfs(u, x):
    ctr[u] += x
    for v in tree[u]:
        dfs(v, x)

ctr = [0] * (N + 1)
for p, x in P:
    dfs(p, x)
    
print(*ctr[1:])

Submission status: Runtime Error

Solution code:
def f(root):
  for v in nood[root]:
    nood[v].remove(root)
    f(v)
  
def g(root):
  for v in nood[root]:
    score[v]+=score[root]
    g(v)
  
n,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n-1)]
m=[list(map(int,input().split())) for i in range(q)]

nood=[[]for i in range(n)]
for a,b in l:
  a-=1
  b-=1
  nood[a]+=[b]
  nood[b]+=[a]
  
f(0)

#print(nood)

score=[0 for i in range(n)]
for p,x in m:
  p-=1
  score[p]+=x
  
g(0)
  
for i in score:
  print(i,end=" ")

Submission status: Runtime Error

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

def dfs(v, parent):
    for u in lines[v]:
        if u != parent:
            ans[u] += ans[v]
            dfs(u, v)

dfs(0, -1)
print (*ans)

Submission status: Runtime Error

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

nd = [[] for i in range(n+1)]
ans = [0] * (n+1)

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


def f(pi, xi):
    if c[pi]:
        return c
    else:
        c[pi] = True

    ans[pi] += xi
    for j in nd[pi]:
        f(j, xi)
    return c


for _ in range(q):
    c = [False for ii in range(n+1)]
    p, x = map(int, input().split())

    ans[p] += x
    c[p] = True
    for i in nd[p]:
        c = f(i, x)

print(*ans[1:])

Submission status: Runtime Error

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

nd = [[] for i in range(n+1)]
ans = [0] * (n+1)

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


def f(pi, xi, c):
    if c[pi]:
        return c
    else:
        c[pi] = True

    ans[pi] += xi
    for j in nd[pi]:
        c = f(j, xi, c)
    return c


for _ in range(q):
    c = [False for ii in range(n+1)]
    p, x = map(int, input().split())

    ans[p] += x
    c[p] = True
    for i in nd[p]:
        c = f(i, x, c)

print(*ans[1:])

Submission status: Runtime Error

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

nd = [[] for i in range(n+1)]
ans = [0] * (n+1)

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


def f(pi, xi, c):
    if c[pi]:
        return c
    else:
        c[pi] = True

    ans[pi] += xi
    for j in nd[pi]:
        c = f(j, xi, c)
    return c


for _ in range(q):
    c = [False for ii in range(n+1)]
    p, x = map(int, input().split())

    ans[p] += x
    c[p] = True
    for i in nd[p]:
        c = f(i, x, c)

print(c)
print(*ans[1:])

Submission status: Runtime Error

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

nd = [[] for i in range(n+1)]
ans = [0] * (n+1)

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


def f(pi, xi):
    if c[pi]:
        return
    else:
        c[pi] = True
    ans[pi] += xi
    c[p] = True
    for j in nd[pi]:
        f(j, xi)
    return


for _ in range(q):
    c = [False for ii in range(n+1)]
    p, x = map(int, input().split())

    ans[p] += x
    c[p] = True
    for i in nd[p]:
        f(i, x)

print(*ans[1:])

Submission status: Runtime Error

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

nd = [[] for i in range(n+1)]
ans = [0] * (n+1)

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


def f(pi, xi):
    ans[pi] += xi
    for j in nd[pi]:
        f(j, xi)


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

    ans[p] += x
    for i in nd[p]:
        f(i, x)

print(*ans[1:])

Submission status: Runtime Error

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

nd = {1+i: [] for i in range(n)}
ans = [0] * (n+1)

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


def f(pi, xi):
    ans[pi] += xi
    for j in nd[pi]:
        f(j, xi)


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

    ans[p] += x
    for i in nd[p]:
        f(i, x)

print(*ans[1:])

Submission status: Runtime Error

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

nd = {1+i: [] for i in range(n)}
ans = [0] * (n+1)

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


def f(pi, xi):
    ans[pi] += xi
    for j in nd[pi]:
        f(j, xi)


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

    ans[p] += x
    for i in nd[p]:
        f(i, x)

print(ans[1], end='')
for a in ans[2:]:
    print(' ', end='')
    print(a, end='')
print()

Submission status: Runtime Error

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

def DFS(v,i,count):
  global ans
  if v == i:# 探索終了
    #print('ans:{}, v:{}'.format(ans,v_count[v]))
    ans[i] = count + v_count[v]
    #print('tmp:{}'.format(tmp))
  for u in adj[v]:
    DFS(u,i,count + v_count[v])

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

adj = [[] for _ in range(N)]
v_count = [0] * N

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

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

# print(v_count)
ans = [None] * N
for i in range(N):
  # iを探索で見つけ出す, そこまで通った頂点のv_count
  # の総和を出力
  DFS(0,i,0)
print(*ans)

Submission status: Runtime Error

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

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

def dfs(v1, parent, add):
    values[v1] += add
    for v2 in graph[v1]:
        if v2 == parent:
            continue
        dfs(v2, v1, values[v1])


dfs(0, 0, 0)

print(*values)

Submission status: Runtime Error

Solution code:
#python3

def main():
    n, q = map(int, input().split())
    to = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        to[a].append(b)

    node = [0 for _ in range(n)]
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        node[p] += x  
    def dfs(v, p=-1):
        for u in to[v]:
            if u == p:
                continue
            else:
                node[u] += node[v]
                dfs(u, v)
    dfs(0)
    for i in range(n):
        if i == n-1:
            print(node[i])
        else:
            print(node[i], end=' ')
main()

Submission status: Runtime Error

Solution code:
#全体に同じものを足すときは累積和を考えたい

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

graph = [set([]) for _ in range(N+1)]
for a,b in AB:
    graph[a].add(b)
    graph[b].add(a)
#どちらが親か分からないがとりあえずつながっている
#どちらが親かは後で判断

counter = [0 for _ in range(N+1)]
#counter[i]:i番目の頂点のカウンター

for p,x in PX:
    counter[p] += x

def dfs(graph,counter,edge,parent):
    for e in graph[edge]-parent:
        counter[e] += counter[edge]
        dfs(graph,counter,e,set([edge]))

dfs(graph,counter,1,set([]))

print(counter[1],end="")
for i in range(2,N+1):
    print("",counter[i],end="")

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
r = [[] for _ in range(n+1)]
bonus = [0] * (n+1)
score = [0] * (n+1)

for i in range(n-1):
    a,b = map(int,input().split())
    r[a].append(b)
for i in range(q):
    p,x = map(int,input().split())
    bonus[p] += x 

def dfs(d,u):
    s[u] = True
    score[u] += bonus[d]
    
    for v in r[u]:
        if not s[v]:
            dfs(d,v)
    
for i in range(1,n+1):
    s = [False] * (n+1)
    dfs(i,i)
        
print(*score[1:])

Submission status: Runtime Error

Solution code:
def inpl():
    return list(map(int, input().split()))


def dfs(v=0, c=0):
    if visited[v] is True:
        return
    visited[v] = True
    c += counter[v]
    ans[v] = c
    for nv in edge[v]:
        dfs(nv, c)
    return


N, Q = inpl()
AB = [inpl() for i in range(N - 1)]
PQ = [inpl() for i in range(Q)]

edge = [[] for i in range(N)]
for a, b in AB:
    edge[a - 1].append(b - 1)
    edge[b - 1].append(a - 1)
counter = {k: 0 for k in range(N)}
for p, q in PQ:
    counter[p - 1] += q

ans = [0 for i in range(N)]
visited = [False for i in range(N)]

dfs()

print(" ".join(list(map(str, ans))))

Submission status: Runtime Error

Solution code:
#python3
import sys
sys.setrecursionlimit(2000)

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)
    op = []
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        op.append((p, x))
    
    cntr = [0 for _ in range(n)]
    
    def dfs(node, p):
        lng = len(tree[node])
        cntr[node] += p
        if lng == 0:
            return
        for i in range(lng):
            next_node = tree[node][i]
            dfs(next_node, p)

    for node, p in op:
        dfs(node, p)
    for i in range(n):
        if i == n-1:
            print(cntr[i])
        else:
            print(cntr[i], end=' ')
main()

Submission status: Runtime Error

Solution code:

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)
    op = []
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        op.append((p, x))
    
    cntr = [0 for _ in range(n)]
    
    def dfs(node, p):
        lng = len(tree[node])
        cntr[node] += p
        if lng == 0:
            return
        for i in range(lng):
            next_node = tree[node][i]
            dfs(next_node, p)

    for node, p in op:
        dfs(node, p)
    for i in range(n):
        if i == n-1:
            print(cntr[i])
        else:
            print(cntr[i], end=' ')
main()

Submission status: Runtime Error

Solution code:
#python3

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)
    op = []
    for _ in range(q):
        p, x = map(int, input().split())
        p -= 1
        op.append((p, x))
    
    cntr = [0 for _ in range(n)]
    
    def dfs(node, p):
        lng = len(tree[node])
        cntr[node] += p
        if lng == 0:
            return
        for i in range(lng):
            next_node = tree[node][i]
            dfs(next_node, p)

    for node, p in op:
        dfs(node, p)
    for i in range(n):
        if i == n-1:
            print(cntr[i])
        else:
            print(cntr[i], end=' ')
main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

def main():
    n, q = map(int, input().split())
    adj = [[] for i in range(n)]
    for i in range(n - 1):
        a, b = map(int, input().split())
        adj[a - 1].append(b - 1)
        adj[b - 1].append(a - 1)
    add_node = [0 for i in range(n)]
    for i in range(q):
        p, x = map(int, input().split())
        add_node[p - 1] += x
    counter = [0 for i in range(n)]
    visited = [False for i in range(n)]
    dfs(0, adj, visited, counter, 0, add_node)
    print(" ".join(str(x) for x in counter))

def dfs(i, adj, visited, counter, add, add_node):
    visited[i] = True
    add += add_node[i]
    counter[i] += add
    for j in adj[i]:
        if not visited[j]:
            dfs(j, adj, visited, counter, add, add_node)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#全体に同じものを足すときは累積和を考えたい

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

graph = [set([]) for _ in range(N+1)]
for a,b in AB:
    graph[a].add(b)
    graph[b].add(a)
#どちらが親か分からないがとりあえずつながっている
#どちらが親かは後で判断

counter = [0 for _ in range(N+1)]
#counter[i]:i番目の頂点のカウンター

for p,x in PX:
    counter[p] += x

def cal(graph,counter,edge,parent):
    if len(graph[edge]) > 1 or edge == 1:
        for e in graph[edge]:
            if e != parent:
                counter[e] += counter[edge]
                cal(graph,counter,e,edge)

cal(graph,counter,1,None)

ans = " ".join(map(str,counter[1:]))
print(ans)

Submission status: Runtime Error

Solution code:
#全体に同じものを足すときは累積和を考えたい

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

graph = [set([]) for _ in range(N+1)]
for a,b in AB:
    graph[a].add(b)
    graph[b].add(a)
#どちらが親か分からないがとりあえずつながっている
#どちらが親かは後で判断

counter = [0 for _ in range(N+1)]
#counter[i]:i番目の頂点のカウンター

for p,x in PX:
    counter[p] += x

def cal(graph,counter,edge,parent):
    for e in (graph[edge]):
        if e != parent:
            counter[e] += counter[edge]
            cal(graph,counter,e,edge)

cal(graph,counter,1,None)

ans = " ".join(map(str,counter[1:]))
print(ans)

Submission status: Runtime Error

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

N, Q = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
counts = [0] * N
operations = [list(map(int, input().split())) for _ in range(Q)]

counts = [0] * N
for op in operations:
    p, x = op
    counts[p-1] += x
    
all_connected_vertices = {i: [] for i in range(1, 1+N)}
for a, b in edges:
    all_connected_vertices[a].append(b)
    all_connected_vertices[b].append(a)
    
def dfs(point, pre):
    connected_vertices = all_connected_vertices[point]
    for c in children:
        if c == pre:
            continue
        counts[c-1] += counts[point-1]
        dfs(c, point)
    
dfs(1, -1)
    
print(*counts)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
n,q = map(int, input().split())
e = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    e[a-1].append(b-1)
    e[b-1].append(a-1)
ans = [0]*n
for i in range(q):
    p,x = map(int,input().split()) 
	ans[p-1] += x

def dfs(i = 0, r = -1):
    for j in e[i]:
        if j != r:
            ans[j] += ans[i]
            dfs(j, i)
dfs()
print(*ans)

Submission status: Runtime Error

Solution code:
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)

score = [0] * n
for i in range(q):
  a, b = map(int, input().split())
  score[a-1] += b
def dfs(now):
  for i in ki[now]:
    score[i] += score[now]
    dfs(i)
dfs(0)
print(*score)

Submission status: Runtime Error

Solution code:
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)

def ki_root(ki, ne, i, n):
  if n == 0:
    for j in ki[i]:
      n += 1
      ki_root(ki, ne, j, n)
  else:
    ki[ne].extend(ki[i])
    for j in ki[i]:
      n += 1
      ki_root(ki, ne, j, n)
for i in range(n):
  ki_root(ki, i, i, 0)
import numpy as np
ans = np.array([0] * n)
for i in range(q):
  a, b = map(int, input().split())
  ans[ki[a-1]] += b
  ans[a-1] += b
print(*ans)

Submission status: Runtime Error

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

n,q=map(int,input().split())
G=[[] for i in range(n)] #隣接配列
P=[0]*n #親のポイント
Ga_append=G[a].append
Gb_append=G[b].append
for i in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    Ga_append(b)
    Gb_append(a)
for i in range(q):
    p,x=map(int,input().split())
    p-=1
    P[p]+=x
    

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

dfs(0)
print(*P)

Submission status: Runtime Error

Solution code:
def find_desc(done, v=1):
    global desc, rels
    for d in rels[v]:
        if d in done:
            continue
        done.append(d)
        desc[v].add(d)
        find_desc(done, d)


N, Q = map(int, input().split())
rels = [None] + [set() for _ in range(N)]
for _ in range(N - 1):
    ai, bi = map(int, input().split())
    rels[ai].add(bi)
    rels[bi].add(ai)

desc = [None] + [set() for _ in range(N)]
find_desc([1], 1)

counter = [None] + [0] * N
for _ in range(Q):
    pi, xi = map(int, input().split())
    counter[pi] += xi
for p in range(1, N + 1):
    for d in desc[p]:
        counter[d] += counter[p]

print(*counter[1:])

Submission status: Runtime Error

Solution code:
n,q= [int(i) for i in input().split()]

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

def dfs(now, last, x):
    global node
    node[now]+=x
    for nxt in edge[now]:
        if nxt == last: continue
        if nxt > now: dfs(nxt, now, x)

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

Submission status: Runtime Error

Solution code:
class CountTree:
    def __init__(self, relations):
        self.relations = relations
        self.desc = [set() for _ in range(len(relations))]
        self._find_desc([1])
        self.counter = [0] * len(relations)

    def _find_desc(self, done, v=1):
        for desc in self.relations[v]:
            if desc in done:
                continue
            done.append(desc)
            self.desc[v].add(desc)
            self._find_desc(done, desc)

    def inc(self, p, x):
        self.counter[p] += x
        for d in self.desc[p]:
            self.inc(d, x)

    def p_cnt(self):
        print(*self.counter[1:])


N, Q = map(int, input().split())
relations = [None] + [set() for _ in range(N)]
for _ in range(N - 1):
    ai, bi = map(int, input().split())
    relations[ai].add(bi)
    relations[bi].add(ai)
ct = CountTree(relations)
for _ in range(Q):
    pi, xi = map(int, input().split())
    ct.inc(pi, xi)
ct.p_cnt()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

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)

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

counter = [0 for _ in range(N)]
for p, x in query:
    counter[p - 1] += x


def dfs(v, parent):
    for v_next in edges[v]:
        if v_next != parent:
            counter[v_next] += counter[v]
            dfs(v_next, v)


root = 0
dfs(root, None)

print(*counter)