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 = [int(i) for i in input().split()]

ab = []
for i in range(n-1):
    ab.append([int(i) for i in input().split()])

px = []
for i in range(q):
    px.append([int(i) for i in input().split()])

tree = {}
for a, b in ab:
    if a in tree.keys():
        tree[a].append(b)
    else:
        tree[a] = [b]

counter = [0] * (n+1)

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

def count(node):
    if node in tree.keys():
        for leaf in tree[node]:
            counter[leaf] += counter[node]
            count(leaf)

count(1)

for counter_i in counter[1:]:
    print(counter_i, end=" ")

Submission status: Runtime Error

Solution code:


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

ab = []
for i in range(n-1):
    ab.append([int(i) for i in input().split()])

px = []
for i in range(q):
    px.append([int(i) for i in input().split()])

tree = {}
for a, b in ab:
    if a in tree.keys():
        tree[a].append(b)
    else:
        tree[a] = [b]

counter = [0] * (n+1)

def count(p, x):
    counter[p] += x
    if p in tree.keys():
        for leaf in tree[p]:
            count(leaf, x)

for p, x in px:
    count(p, x)

for counter_i in counter[1:]:
    print(counter_i, end=" ")

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N,Q = map(int,input().split())
Graph = [[] for i in range(N + 1)]

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

counter = [0] * (N + 1)

for i in range(Q): #counterに値を入れていく．
    a,b = map(int,input().split())
    counter[a] += b

seen = [False] * (N + 1)

def dfs(Graph, seen, v, counter): #s : 根から訪問している頂点までの累積和
    seen[v] = True
    for next_v in Graph[v]:
        if seen[next_v]:
            continue
        counter[next_v] += counter[v]
        dfs(Graph, seen, next_v, counter)
    
    
dfs(Graph, seen, 1, counter)
print(*counter[1:])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N,Q = map(int,input().split())
Graph = [[] for i in range(N + 1)]

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

counter = [0] * (N + 1)

for i in range(Q): #counterに値を入れていく．
    a,b = map(int,input().split())
    counter[a] += b

seen = [False] * (N + 1)

def dfs(Graph, seen, v, s): #s : 根から訪問している頂点までの累積和
    seen[v] = True
    s += counter[v]
    print(str(s) + " ", end = "")
    for next_v in Graph[v]:
        if seen[next_v]:
            continue
        dfs(Graph, seen, next_v, s)
    
    
dfs(Graph, seen, 1, 0)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define HSORT(V,n) sort(V,V+n)
#define RSORT(V) sort((V).rbegin(), (V).rend())
#define INF 1e+9
#define MININF -INF
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

vector<int> to[200010];
vector<int> ans;
void dfs(int v,int f=-1){
    for(int u:to[v]){
        if (u == f) continue;
        ans[u] += ans[v];
        dfs(u,v);
    }
}
int main(){
    int n,q;
    cin >> n >> q;
    int a,b;
    REP(i,n-1){
        cin >> a >> b;
        a--;
        b--;
        to[a].push_back(b);
        to[b].push_back(a);
    }
    ans.resize(n);
    int p,x;
    REP(i,q){
        cin >> p >> x;
        p--;
        ans[p] += x;
    }
    dfs(0);
    REP(i,n){
        PRINT(ans[i]);
    }
}

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
l=[[] for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a].append(b)
lcount=[0 for i in range(n+1)]
def sum(x,i):
  l1=l[i]
  lcount[i]+=x
  if l1==[]:
    pass
  else:
    for j in l1:
      sum(x,j)
for i in range(q):
  p,x=map(int,input().split())
  sum(x,p)
word=""
for i in range(1,n+1):
  word+=str(lcount[i])+" "
print(word)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
l=[[] for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a].append(b)
lcount=[0 for i in range(n+1)]
def sum(x,i):
  l1=l[i]
  if l1==[]:
    pass
  else:
    for j in l1:
      lcount[j]+=x
      sum(x,j)
for i in range(q):
  p,x=map(int,input().split())
  lcount[p]+=x
  sum(x,p)
word=""
for i in range(1,n+1):
  word+=str(lcount[i])+" "
print(word)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# https://atcoder.jp/contests/abc138/tasks/abc138_d

# It's a Python porting of ki.cpp
# Mainly to understand the algorithm by myself

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

tree_link = [None] * 200005
answer = [0] * N


def dfs(v, p=-1):
    for link in tree_link[v]:
        if link == p:  # 親ノードであれば continue
            continue
        answer[link] += answer[v]
        dfs(link, v)


# 双方向の辺を作る
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1

    if tree_link[a]:
        tree_link[a].append(b)
    else:
        tree_link[a] = [b]

    if tree_link[b]:
        tree_link[b].append(a)
    else:
        tree_link[b] = [a]

# 頂点に値を足し込む
for i in range(Q):
    p, x = list(map(int, input().split()))
    answer[p - 1] += x

# 計算
dfs(0)

# 出力
print(" ".join([str(answer[i]) for i in range(N)]))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# https://atcoder.jp/contests/abc138/tasks/abc138_d

# It's a Python porting of ki.cpp
# Mainly to understand the algorithm by myself

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

tree_link = [None] * 100000
answer = [0] * N


def dfs(v, p=-1):
    for link in tree_link[v]:
        if link == p:  # 親ノードであれば continue
            continue
        answer[link] += answer[v]
        dfs(link, v)


# 双方向の辺を作る
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1

    if tree_link[a]:
        tree_link[a].append(b)
    else:
        tree_link[a] = [b]

    if tree_link[b]:
        tree_link[b].append(a)
    else:
        tree_link[b] = [a]

# 頂点に値を足し込む
for i in range(Q):
    p, x = list(map(int, input().split()))
    answer[p - 1] += x

# 計算
dfs(0)

# 出力
print(" ".join([str(answer[i]) for i in range(N)]))

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
l=[[]*N for i in range(N)]
ans=[0]*N


def add(index,value):
    ans[index]+=value
    for i in range(N):
        if l[index][i]==1:
            add(i,value)
    return    

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

for i in range(N-1)[::-1]:
    for item in l[i]:
        l[i]=l[i]+l[item]

print(l)

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

print(*ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import collections




def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * N
    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            add_counter(i,x)

    for pi, xi in zip(p, x):
        add_counter(pi,xi)

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





# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  # type: "List[int]" 
    x = [int()] * (Q)  # type: "List[int]" 
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(200005))

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

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

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

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

dfs(0)
for i in ans:
  print(i)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
Tree = {}
Counter = [0] * N

def addCounter(q, x):
    Counter[q-1] += x
    if q in Tree:
        for j in Tree[q]:
            addCounter(j, x)

for i in range(N-1):
    a, b = map(int, input().split())
    Tree.setdefault(a, []).append(b)
for i in range(Q):
    q, x = map(int, input().split())
    addCounter(q, x)
print(' '.join(map(str, Counter)))

Submission status: Runtime Error

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

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

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

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

dfs(0)
for i in ans:
  print(i)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
a, b = [0]*(N-1), [0]*(N-1)
p, x = [0]*Q, [0]*Q
for n in range(N-1):
  a[n], b[n] = map(int, input().split())
for q in range(Q):
  p[q], x[q] = map(int, input().split())

counter = [0]*N
tree = {n+1:[] for n in range(N)}
for n in range(N-1):
  tree[a[n]].append(b[n])

def ope(num, vert):
  counter[vert-1] += num
  if tree[vert] != []:
    for t in tree[vert]:
      ope(num, t)

for q in range(Q):
  ope(x[q], p[q])

print(*counter)

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)]

dic = {i+1:[] for i in range(n)}
for a,b in ab:
  dic[a].append(b)
  
vs=[0]*n

def plus(v,p):
  vs[v-1]+=p
  for nv in dic[v]:
    plus(nv,p)  

for p,x in px:    
  plus(p,x)    

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

Submission status: Runtime Error

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


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


class Node:
    val = 0
    children = []

    def __init__(self, val, children):
        self.val = val
        self.children = children


# 木を作る
tree = {}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    left_node = tree.get(a, False)
    if left_node:
        left_node.children.append(b)
    else:
        tree[a] = Node(0, [b])

    right_node = tree.get(b, False)
    if not right_node:
        tree[b] = Node(0, [])


def add_val_recurse(node: Node, val):
    if node:
        node.val += val
        [add_val_recurse(tree.get(child, None), val) for child in node.children]


# 木に値を詰める
for i in range(Q):
    node, val = list(map(int, input().split()))
    add_val_recurse(tree.get(node, None), val)


print(" ".join([str(tree[key].val) for key in sorted(list(tree.keys()))]))

Submission status: Runtime Error

Solution code:
from collections import deque

#n,q=4,3
#ab=[[1,2],[2,3],[2,4]]
#px=[[2,10],[1,100],[3,1]]
n,q=map(int, input().split())
#ab=[list(map(int, input().split())) for i in range(n-1)]
#ab=sorted(ab,key=lambda x:(x[0],x[1]))
#max_n=2*10**5
t=[[] for i in range(n+1)]
ans=[0 for i in range(n+1)]
for i in range(n-1):
    ab=[list(map(int, input().split()))]
    t[ab[0]].append(ab[1])
d=deque([])
for i in range(q):
    px=list(map(int, input().split()))
    root = px[0]
    x = px[1]
    ans[root]+=x
    while(1):#再帰呼び出しをwhile
        if len(t[root])!=0:
            for j in range(len(t[root])):
                ans[t[root][j]] += x
                d.append(t[root][j])
        if len(d)==0:
            break
        root=d.popleft()

print(*ans[1:])

Submission status: Runtime Error

Solution code:
def add_x(root,x):
    if len(t[root])!=0:
        for j in range(len(t[root])):
            ans[t[root][j]] += x
            add_x(t[root][j],x)
        return
    else:
        return

#n,q=4,3
#ab=[[1,2],[2,3],[2,4]]
#px=[[2,10],[1,100],[3,1]]
n,q=map(int, input().split())
ab=[list(map(int, input().split())) for i in range(n-1)]
ab=sorted(ab,key=lambda x:(x[0],x[1]))
#max_n=2*10**5
t=[[] for i in range(n+1)]
ans=[0 for i in range(n+1)]
for i in range(len(ab)):
    t[ab[i][0]].append(ab[i][1])
for i in range(q):
    px=list(map(int, input().split()))
    ans[px[0]]+=px[1]
    add_x(px[0],px[1])

ans_str = ""
for i in range(1,len(ans)):
    if i<len(ans)-1:
        ans_str += str(ans[i]) + " "
    else:
        ans_str += str(ans[i])
print(ans_str)

Submission status: Runtime Error

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

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

ans_list = [0] * N

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

ans_list[0] = points[0]
for n in range(1, N):
    ans_list[n] = ans_list[graph_lis[n][0]] + points[n]
print(' '.join(map(str, ans_list)))

Submission status: Runtime Error

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

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

ans_list = [0] * N

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

ans_list[0] = points[0]
for n in range(1, N):
    ans_list[n] = ans_list[graph_lis[n][0]] + points[n]
print(' '.join(map(str, ans_list)))

Submission status: Runtime Error

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

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

ans_list = [0] * N

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

ans_list[0] = points[0]
for n in range(1, N):
    ans_list[n] = ans_list[graph_lis[n][0]] + points[n]
print(*ans_list)

Submission status: Runtime Error

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

graph_lis = [[] for i in range(N)]
for n in range(N - 1):#0-originedに注意
    a, b = map(int, input().split())
    graph_lis[b - 1].append(a - 1)

#print(graph_lis)

ans_list = [0] * N

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

ans_list[0] = points[0]
for n in range(1, N):
    ans_list[n] = ans_list[graph_lis[n][0]] + points[n]
print(*ans_list)

Submission status: Runtime Error

Solution code:
num_list = [0] * (N)
a_b_list = []

for _ in range(N-1):
    a_b_list.append(list(map(int, input().split())))

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

for a_b in a_b_list:
    num_list[a_b[1]-1] += num_list[a_b[0]-1]

print(*num_list)

Submission status: Runtime Error

Solution code:
from array import array
from collections import deque

#sys.setrecursionlimit(200000)

import sys

def input():
    return sys.stdin.readline().rstrip('\n')

s = array('u', list(input()))
t = array('u', list(input()))

c_in_s = {}
for c in 'abcdefghijklmnopqrstuvwxyz':
    c_in_s[c] = []
for i,c in enumerate(s):
    c_in_s[c].append(i)

# find c in s[i:]
def find_char_in_s(c, i):
    idlist = c_in_s[c]
    for id in idlist:
        if i<=id:
            return id
    return -1

# find Longest common subsequence
# returns indexes of last common char in s1 and s2
def find_lcs(s, t, offset_t):
    sid = -1
    tid = offset_t
    for i in range(tid, len(t)):
        c = t[i]
        prev_sid = sid
        sid = find_char_in_s(c, sid+1)
        if sid == -1:
            return prev_sid, i-1
        if i == len(t)-1:
            return sid,i
    return -1,-1

count = 0

i_sdash = -1
offset_t = 0
i_t = 0

while True:
    sid,tid = find_lcs(s,t, offset_t)
    if sid == -1 and i_sdash == -1:
        print(-1)
        sys.exit()
    elif sid == -1 and i_sdash >= 0:
        break
    else:
        i_sdash = len(s)*count + sid
        offset_t = tid+1
        count += 1

print(i_sdash+1)

Submission status: Runtime Error

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

v = [[] for _i in range(n + 10)]

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

add = [0] * (n + 10)

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


def dfs(now, fa):
    add[now] += add[fa]
    for i in v[now]:
        if i == fa:
            continue
        dfs(i, now)


dfs(1, 0)

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

Submission status: Runtime Error

Solution code:
from array import array
from collections import deque

import sys
sys.setrecursionlimit(200000)

input = sys.stdin.readline

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

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

# accumulate

def accumulate(i):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
        accumulate(c)

accumulate(1)
V = [str(P[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Runtime Error

Solution code:
from array import array
from collections import deque

import sys
sys.setrecursionlimit(100000)

input = sys.stdin.readline

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

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

# accumulate

def accumulate(i):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
        accumulate(c)

accumulate(1)
V = [str(P[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Runtime Error

Solution code:
from array import array
from collections import deque

import sys
sys.setrecursionlimit(100000)

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

P = array('q', [0]*(N+1))
for i in range(Q):
    p,x = list(map(int, input().split()))
    P[p] += x

# accumulate

def accumulate(i):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
        accumulate(c)

accumulate(1)
V = [str(P[i]) for i in range(1,N+1)]
print(' '.join(V))

Submission status: Runtime Error

Solution code:
from array import array
from collections import deque

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

A = {}
for i in range(1,N+1):
    A[i] = deque([])

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

P = array('l', [0]*(N+1))
for i in range(Q):
    p,x = list(map(int, input().split()))
    P[p] += x

# accumulate

def accumulate(i):
    children = A[i]
    value = P[i]
    for c in children:
        P[c] += value
        accumulate(c)

accumulate(1)
Plist = list(map(str, P))
Plist.pop(0)
print(' '.join(Plist))

Submission status: Runtime Error

Solution code:
N,Q = input().split()
N = int(N)
Q = int(Q)
#key=leef,value=parent
root_dict = {}
p_x_dict = {}
point_list = [0] * (N)

for i in range(1,N+1):
	p_x_dict[i] = 0

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

#print(root_dict)
#value_list = root_dict.values()

for v in range(Q):
	p,x = input().split()
	p = int(p)
	x = int(x)
	if p in p_x_dict.keys():
		p_x_dict[p] += x
	else:
		p_x_dict[p] = x
	#print(p_x_dict[p])

#print(p_x_dict[1])
#print(p_x_dict[2])
#print(p_x_dict[3])
#print(p_x_dict)

for m in range(1,N+1):
	if m == 1:
		point_list[m-1] = p_x_dict[m]
	else:
		point_list[m-1] = p_x_dict[m] + point_list[root_dict[m]-1]

	#print(point_list[m])
	
for w in point_list:
	#print(point_list)
	print(w, end=" ")

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache
from collections import defaultdict

N, Q = map(int, input().split())
sys.setrecursionlimit(N ** 2)
matrix = defaultdict(dict)
for _ in range(N-1):
    a, b = tuple(map(int, input().split()))
    matrix[a-1][b-1] = 1
    matrix[b-1][a-1] = 1

px_list = []
for _ in range(Q):
    px_list.append(tuple(map(int, input().split())))

parent_relation = defaultdict(dict)
def create_parent_relation(node, parents):
    for child in matrix[node]:
        for p in parents:
            if child not in parents:
                parent_relation[p][child] = 1
        if child not in parents:
            create_parent_relation(child, parents | set([child]))
create_parent_relation(0, set([0]))
counter = [0] * N
for p, x in px_list:
    for i in range(N):
        if parent_relation[p-1].get(i, False):
            counter[i] += x
        if p-1 == i:
            counter[i] += x
            
print(*counter)

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache
from collections import defaultdict

N, Q = map(int, input().split())
sys.setrecursionlimit(N ** 2)
matrix = defaultdict(dict)
for _ in range(N-1):
    a, b = tuple(map(int, input().split()))
    matrix[a-1][b-1] = 1
    matrix[b-1][a-1] = 1

px_list = []
for _ in range(Q):
    px_list.append(tuple(map(int, input().split())))

parent_relation = defaultdict(dict)
def create_parent_relation(node, parents):
    for child in matrix[node]:
        for p in parents:
            parent_relation[p][child] = 1
        if child not in parents:
            create_parent_relation(child, parents | set([child]))
create_parent_relation(0, set([0]))
counter = [0] * N
for p, x in px_list:
    for i in range(N):
        if parent_relation[p-1].get(i, False):
            counter[i] += x

print(*counter)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N, Q = map(int, input().split())
tree_dict = defaultdict(list)
for _ in range(N-1):
    a, b = tuple(map(int, input().split()))
    if a > b:
        tree_dict[b-1].append(a-1)
    else:
        tree_dict[a-1].append(b-1)

px_list = []
for _ in range(Q):
    px_list.append(tuple(map(int, input().split())))



def add_counter(cnt, target_node, value):
    cnt[target_node] += value
    for child_node in tree_dict[target_node]:
        add_counter(cnt, child_node, value)

counter = [0] * N
for p, x in px_list:
    add_counter(counter, p-1, x)

print(*counter)

Submission status: Runtime Error

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

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

graph = [[] for _ in range(N+1)]
point_list = [0] * (N+1)

for a, b in ab_list:
    graph[a].append(b)
    graph[b].append(a)
    
for p, x in px_list:
    point_list[p] += x
    
def add_point(num, root, point):    
    point_list[num] += point   
    for g in graph[num]:
        if g == root:
            continue
        add_point(g, num, point_list[num])

add_point(1, 0, 0)
 
print(*(point_list[1:]))

Submission status: Runtime Error

Solution code:
Connection_table=[]
Int_list=[]

def add_int_to_subtree(top_number,add_number):
    Int_list[top_number]+=add_number
    if len(Connection_table[top_number])!=0:
        for branch in Connection_table[top_number]:
            add_int_to_subtree(branch, add_number)

#最初のNとQの読み込み
Nstr,Qstr=input().split(" ")
N=int(Nstr)
Q=int(Qstr)
#まずデータの受け取り
ablist=[]
for n in range(N-1):
    ablist.append(input())
pxlist=[]
for q in range(Q):
    pxlist.append(input())


#まずデータを入れるリスト作り
Int_list=[0]*N
for n in range(N):
    Connection_table.append([])

#枝の付き方を保存
for n in range(N-1):
    astr,bstr=ablist[n].split(" ")
    a=int(astr)-1
    b=int(bstr)-1
    Connection_table[a].append(b)

#どんどん足していく
for q in range(Q):
    pstr,xstr=pxlist[q].split(" ")
    p=int(pstr)-1
    x=int(xstr)
    add_int_to_subtree(p, x)

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

Submission status: Runtime Error

Solution code:
Connection_table=[]
Int_list=[]

def add_int_to_subtree(top_number,add_number):
    Int_list[top_number]+=add_number
    if len(Connection_table[top_number])!=0:
        for branch in Connection_table[top_number]:
            add_int_to_subtree(branch, add_number)

#最初のNとQの読み込み
Nstr,Qstr=input().split(" ")
N=int(Nstr)
Q=int(Qstr)
#まずデータの受け取り
ablist=[]
for n in range(N-1):
    ablist.append(input())
pxlist=[]
for q in range(Q):
    pxlist.append(input())


#まずデータを入れるリスト作り
Int_list=[0]*N
for n in range(N):
    Connection_table.append([])

#枝の付き方を保存
for n in range(N-1):
    astr,bstr=ablist[n].split(" ")
    a=int(astr)-1
    b=int(bstr)-1
    Connection_table[a].append(b)

#どんどん足していく
for q in range(Q):
    pstr,xstr=pxlist[q].split(" ")
    p=int(pstr)-1
    x=int(xstr)
    add_int_to_subtree(p, x)

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

Submission status: Runtime Error

Solution code:
Connection_table=[[],]
Int_list=[[],]

def add_int_to_subtree(top_number,add_number):
    Int_list[top_number]+=add_number
    if len(Connection_table[top_number])!=0:
        for branch in Connection_table[top_number]:
            add_int_to_subtree(branch, add_number)

#最初のNとQの読み込み
Nstr,Qstr=input().split(" ")
N=int(Nstr)
Q=int(Qstr)

#まずデータを入れるリスト作り
for n in range(N):
    Int_list.append(0)
    Connection_table.append([])

#枝の付き方を保存
for n in range(N-1):
    astr,bstr=input().split(" ")
    a=int(astr)
    b=int(bstr)
    Connection_table[a].append(b)

#どんどん足していく
for q in range(Q):
    pstr,xstr=input().split(" ")
    p=int(pstr)
    x=int(xstr)
    add_int_to_subtree(p, x)

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

Submission status: Runtime Error

Solution code:
Connection_table=[[],]
Int_list=[[],]

def add_int_to_subtree(top_number,add_number):
    # print("top_number",top_number)
    Int_list[top_number]+=add_number
    if len(Connection_table[top_number])!=0:
        for branch in Connection_table[top_number]:
            add_int_to_subtree(branch, add_number)

#最初のNとQの読み込み
N,Q=input().split(" ")
N=int(N)
Q=int(Q)
# print("N,Q",N,Q)

#まずデータを入れるリスト作り

for n in range(N):
    Int_list.append(0)
    Connection_table.append([])
# print("len(Int_list)",len(Int_list))

#枝の付き方を保存
for n in range(N-1):
    a,b=input().split(" ")
    a=int(a)
    b=int(b)
    Connection_table[a].append(b)
# print("len(Connection_table)",len(Connection_table))
# print("Connection_table",Connection_table)

#どんどん足していく
for q in range(Q):
    p,x=input().split(" ")
    p=int(p)
    x=int(x)

    add_int_to_subtree(p, x)

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

Submission status: Runtime Error

Solution code:
Connection_table=[]
Int_list=[]

def add_int_to_subtree(top_number,add_number):
    # print("top_number",top_number)
    Int_list[top_number]+=add_number
    if len(Connection_table[top_number])!=0:
        for branch in Connection_table[top_number]:
            add_int_to_subtree(branch, add_number)
#最初のNとQの読み込み
N,Q=input().split(" ")
N=int(N)
Q=int(Q)
# print("N,Q",N,Q)

#まずデータを入れるリスト作り

for n in range(N):
    Int_list.append(0)
    Connection_table.append([])
# print("len(Int_list)",len(Int_list))

#枝の付き方を保存
for n in range(N-1):
    a,b=input().split(" ")
    a=int(a)-1
    b=int(b)-1
    Connection_table[a].append(b)
# print("len(Connection_table)",len(Connection_table))
# print("Connection_table",Connection_table)

#どんどん足していく
for q in range(Q):
    p,x=input().split(" ")
    p=int(p)-1
    x=int(x)

    add_int_to_subtree(p, x)

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

Submission status: Runtime Error

Solution code:
Connection_table=[]
Int_list=[]

def add_int_to_subtree(top_number,add_number):
    print("top_number",top_number)
    Int_list[top_number]+=add_number
    if len(Connection_table[top_number])!=0:
        for branch in Connection_table[top_number]:
            add_int_to_subtree(branch, add_number)
#最初のNとQの読み込み
N,Q=input().split(" ")
N=int(N)
Q=int(Q)
print("N,Q",N,Q)

#まずデータを入れるリスト作り

for n in range(N):
    Int_list.append(0)
    Connection_table.append([])
print("len(Int_list)",len(Int_list))

#枝の付き方を保存
for n in range(N-1):
    a,b=input().split(" ")
    a=int(a)-1
    b=int(b)-1
    Connection_table[a].append(b)
print("len(Connection_table)",len(Connection_table))
print("Connection_table",Connection_table)

#どんどん足していく
for q in range(Q):
    p,x=input().split(" ")
    p=int(p)-1
    x=int(x)

    add_int_to_subtree(p, x)

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

Submission status: Runtime Error

Solution code:
Connection_table=[]
Int_list=[]

def add_int_to_subtree(top_number,add_number):
    Int_list[top_number]+=add_number
    if len(Connection_table[top_number])!=0:
        for branch in Connection_table[top_number]:
            add_int_to_subtree(branch, add_number)
#最初のNとQの読み込み
N,Q=input().split(" ")
N=int(N)
Q=int(Q)

#まずデータを入れるリスト作り

for n in range(N):
    Int_list.append(0)
    Connection_table.append([])

#枝の付き方を保存
for n in range(N-1):
    a,b=input().split(" ")
    a=int(a)-1
    b=int(b)
    Connection_table[a].append(b)

#どんどん足していく
for q in range(Q):
    p,x=input().split(" ")
    p=int(p)-1
    x=int(x)
    add_int_to_subtree(p, x)

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