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:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def add(m,n,x,v,r):
    r[x] += v
    for i in m[x]:
        add(m,n,i,v,r)

n,q = LI()
m = [[] for i in range(n)]
r = [0]*n
for i in range(n-1):
    a,b = LI()
    m[a-1].append(b-1)
for i in range(q):
    p,x = LI()
    add(m,n,p-1,x,r)
print(*r)

Submission status: Runtime Error

Solution code:
cin = open(0).read().strip().split('\n')
n, q = map(int, cin[0].split(' '))
ab = [tuple(map(int, a.split(' '))) for a in cin[1:n]]
px = [tuple(map(int, a.split(' '))) for a in cin[n:]]

def dfs(g, idx, s):
    global score

    score[idx] += s
    for i in g[idx]:
        dfs(g, i, s)

g = [[] for _ in range(n)]
for abi in ab:
    g[abi[0]-1].append(abi[1]-1)

score = [0]*n
for pxi in px:
    dfs(g, pxi[0]-1, pxi[1])

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

Submission status: Runtime Error

Solution code:
import sys

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

def dfs(p, v, num):
    global g, plus_list, ans
    tmp = num + plus_list[v]
    for x in g[v]:
        if x == p:
            continue
        dfs(v, x, tmp)
    ans[v] = tmp
    
def main():
    global g, plus_list, ans
    N, Q = map(int, input().split())
    g = [[] for i in range(N)] 
    for i in range(N-1):
        a, b = map(int, readline().split())
        a -= 1
        b -= 1
        g[a].append(b)
        g[b].append(a)

    plus_list = [0] * N
    for i in range(Q):
        q, y = map(int, input().split())
        plus_list[q-1] += y

    ans = [0] * N
    
    dfs(-1, 0, 0)
    print(*ans)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
to = [[] for _ in range(n)]
score = [0 for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    to[a].append(b)
    to[b].append(a)
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    score[p] += x

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

dfs(0)
print(*score)

Submission status: Runtime Error

Solution code:
def dfs(p, v, num):
    global g, plus_list, ans
    tmp = num + plus_list[v]
    for x in g[v]:
        if v == p:
            continue
        dfs(v, x, tmp)
    ans[v] = tmp
    
def main():
    import sys
    sys.setrecursionlimit(500000)
    global g, plus_list, ans
    N, Q = map(int, input().split())
    g = [[] for i in range(N)] 
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        g[a].append(b)
        g[b].append(a)

    plus_list = [0] * N
    for i in range(Q):
        q, y = map(int, input().split())
        plus_list[q-1] += y

    ans = [0] * N
    
    dfs(-1, 0, 0)
    print(*ans)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

readline = sys.stdin.readline

def dfs(fr, now, num):
    global graph, addsum, ans
    tmp = num + addsum[now]
    for a in graph[now]:
        if a == fr:
            continue
        dfs(now, a, tmp)
    ans[now] = tmp
    

def main():
    global graph, addsum, ans
    N, Q = map(int, readline().split())
    graph = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, readline().split())
        a -= 1; b -= 1
        graph[a].append(b)
        graph[b].append(a)
    addsum = [0]*N
    for _ in range(Q):
        p, x = map(int, readline().split())
        p -= 1
        addsum[p] += x
    
    ans = [0]*N
    dfs(-1, 0, 0)

    print(*ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

ans = [0] * N
def dfs(v, p):
    ans[v] = plus_list[v] + ans[p]
    for i in range(len(g[v])):
        x = g[v][i]
        if x != v:
            dfs(x, v)

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

Submission status: Runtime Error

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

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

ans = [0] * N
def dfs(v, p):
    ans[v] = plus_list[v] + ans[p]
    for i in range(len(g[v])):
        if g[v][i] != v:
            dfs(x, v)

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

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
v = [[] for _ in range(n)]
for _ in range(n - 1):
    ai, bi = map(int, input().split())
    ai -= 1; bi -= 1
    v[ai].append(bi)
    v[bi].append(ai)
c = [0] * n
for _ in range(q):
    pi, xi = map(int, input().split())
    c[pi - 1] += xi

def dfs(i, cv=0, parent=-1):
    cv += c[i]
    c[i] = cv
    for j in v[i]:
        if j == parent:
            continue
        dfs(j, cv, i)

dfs(0)
print(*c)

Submission status: Runtime Error

Solution code:
n,q = list(map(int, input().split()))
li = [[] for i in range(n)]
ans=[0]*n


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

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


visited=[0]*n

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

Submission status: Runtime Error

Solution code:
n,q = list(map(int, input().split()))
li = []
ans=[0]*n
for i in range(n):
    li.append([])


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

def dfs(v, p=-1):
    if v==p:
        return
    visited[v]=1
    for u in li[v]:
        if visited[u]==0:
            ans[u]+=ans[v]
            dfs(u,v)


visited=[0]*n

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

Submission status: Runtime Error

Solution code:
n,q = list(map(int, input().split()))
li = []
ans=[0]*n
for i in range(n):
    li.append([])


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

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




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

Submission status: Runtime Error

Solution code:
n,q = list(map(int, input().split()))
li = []
ans=[0]*n
for i in range(n):
    li.append([])

def dfs(v, p=-1):
    for i in li[v]:
        if v==p:
            continue
        ans[i]+=ans[v]
        dfs(i,v)
for i in range(n-1):
    a, b = list(map(int, input().split()))
    li[a-1].append(b-1)

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

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

Submission status: Runtime Error

Solution code:
n,q = list(map(int, input().split()))
li = []
ans=[0]*n
for i in range(n):
    li.append([])

def dfs(v, p=-1):
    for i in li[v]:
        if v==p:
        continue
        ans[i]+=ans[v]
        dfs(i,v)
for i in range(n-1):
    a, b = list(map(int, input().split()))
    li[a-1].append(b-1)

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

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

Submission status: Runtime Error

Solution code:
n,q = list(map(int, input().split()))
li = []
ans=[0]*n
for i in range(n):
    li.append([])

def dfs(v, p=-1):
    if v==p:
        return
    for i in li[v]:
        ans[i]+=ans[v]
        dfs(i, v)
for i in range(n-1):
    a, b = list(map(int, input().split()))
    li[a-1].append(b-1)

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

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

Submission status: Runtime Error

Solution code:
n,q = list(map(int, input().split()))
li = []
ans=[0]*n
for i in range(n):
    li.append([])

def dfs(v, p=-1):
    if v==p:
        return
    for i in li[v]:
        ans[i]+=ans[v]
        dfs(i, v)
for i in range(n-1):
    a, b = list(map(int, input().split()))
    li[a-1].append(b-1)

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

dfs(0)
print(ans)

Submission status: Runtime Error

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

result=[0]*(n+1)
def dfs(node,edge,par):
    result[node] = result[par] + point[node]
    for next_node in edge[node]:
        if next_node == par:
            continue
        dfs(next_node,edge,node)

dfs(1,edge,0)
print(*result[1:])

Submission status: Runtime Error

Solution code:
from itertools import accumulate
N, Q = map(int, input().split())
graph = [[] for _ in range(N)]
visited = [0] * N
children = [0] * N
count = 0
order = [0] * N
def dfs_recursive(edge):
    global count
    visited[edge] = 1
    order[edge] = count
    count += 1
    child = edge
    for e in graph[edge]:
        if not visited[e]:
            child = dfs_recursive(e)
    children[edge] = child
    return child
for _ in range(N - 1):
    a, b = map(lambda n: int(n) - 1, input().split())
    graph[a].append(b)
    graph[b].append(a)
dfs_recursive(0)
imos = [0] * (N + 1)
for _ in range(Q):
    p, x = map(int, input().split())
    root = p - 1
    root_idx = order[root]
    child = children[root]
    child_idx = order[child]
    imos[root_idx] += x
    imos[child_idx + 1] -= x
ans = list(accumulate(imos))
ans.pop()
print(*ans)

Submission status: Runtime Error

Solution code:
from collections import OrderedDict
#初期データ、構造
N,Q = (int(x) for x in input().split())
connection ={i:set() for i in range(1,N+1)}
for i in range(N-1):
    a,b = (int(x) for x in input().split())
    connection[a].add(b)
    connection[b].add(a)
#point =OrderedDict({i:0 for i in range(1,N+1)})
point ={i:0 for i in range(1,N+1)}
for i in range(Q):
    p,x = (int(x) for x in input().split())
    point[p]=x

def DFS(now,prev = -1):
    for next in connection[now]:
        if next == prev:
            continue
        point[next] +=point[now] 
        DFS(next,now)

DFS(1)
print(*point.values())

Submission status: Runtime Error

Solution code:
from collections import OrderedDict
#初期データ、構造
N,Q = (int(x) for x in input().split())
connection ={i:set() for i in range(1,N+1)}
for i in range(N-1):
    a,b = (int(x) for x in input().split())
    connection[a].add(b)
    connection[b].add(a)
point =OrderedDict({i:0 for i in range(1,N+1)})
#point ={i:0 for i in range(1,N+1)}
for i in range(Q):
    p,x = (int(x) for x in input().split())
    point[p]=x

def DFS(now,prev = -1):
    for next in connection[now]:
        if next == prev:
            continue
        point[next] +=point[now] 
        DFS(next,now)

DFS(1)
print(*point.values())

Submission status: Runtime Error

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

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

for i in range(N):
    ans[i] = tree[i]

def dfs(n, p):
    for i in range(len(graph[n])):
        if graph[n][i] != p:
            ans[graph[n][i]] += ans[n]
            dfs(graph[n][i], n)

dfs(0, -1)

for a in ans:
    print(a)

Submission status: Runtime Error

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

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

for i in range(N):
    ans[i] = tree[i]

def dfs(n, p):
    for i in range(len(graph[n])):
        if graph[n][i] != p:
            ans[graph[n][i]] += ans[n]
            dfs(graph[n][i], n)

dfs(0, -1)

ans = ""

for a in ans:
    ans += str(a) + " "

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N,Q = map(int,input().split())
adjacent = [[] for i in range(N)]
counter = dict.fromkeys(list(range(N)),0)
for i in range(N-1):
    a,b = map(int,input().split())
    adjacent[a-1].append(b-1)
    adjacent[b-1].append(a-1)
for j in range(Q):
    p,q = map(int,input().split())
    counter[p-1] += q
start = 0
frontier = deque([start])
visited = [start]
def DFS(counter,adjacent,frontier,visited):
    if len(frontier) == 0:
        return
    else:
        x = frontier.pop()
        for y in adjacent[x]:
            if y not in visited:
                counter[y] += counter[x]
                visited.append(y)
                frontier.append(y)
                DFS(counter,adjacent,frontier,visited)    
DFS(counter,adjacent,frontier,visited)
value = [counter[i] for i in range(N)]
print(*value)

Submission status: Runtime Error

Solution code:
from collections import deque
N,Q = map(int,input().split())
adjacent = [[] for i in range(N)]
counter = dict.fromkeys(list(range(N)),0)
for i in range(N-1):
    a,b = map(int,input().split())
    adjacent[a-1].append(b-1)
    adjacent[b-1].append(a-1)
for j in range(Q):
    p,q = map(int,input().split())
    counter[p-1] += q
start = 0
frontier = deque([start])
visited = [start]
def DFS(counter,adjacent,frontier,visited):
    print(frontier)
    if len(frontier) == 0:
        return
    else:
        x = frontier.pop()
        for y in adjacent[x]:
            if y not in visited:
                counter[y] += counter[x]
                visited.append(y)
                frontier.append(y)
                DFS(counter,adjacent,frontier,visited)    
DFS(counter,adjacent,frontier,visited)
value = [counter[i] for i in range(N)]
print(*value)

Submission status: Runtime Error

Solution code:
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n,q=LI()
ab=[LI() for _ in range(n-1)]
px=[LI() for _ in range(q)
ans=[0]*(n+1)
graph={i:collections.deque() for i in range(1,n+1)}
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
for p,x in px:
    ans[p]+=x
seen=[0]*(n+1)
stack=[]
def dfs():
    seen[1]=1
    stack.append(1)
    while stack:
        s=stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            gno=graph[s].popleft()
            if seen[gno]:
                continue
            seen[gno]=1
            stack.append(gno)
            ans[gno]+=ans[s]
dfs()
print(*ans[1:])

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())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

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


def dfs(curr, pare):
    for chi in g[curr]:
        if chi == pare: continue
        pl[chi] += pl[curr]
        dfs(chi, curr)

def main():
    dfs(0,-1)
    print(pl)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
class Tree:
    value=0
    def __init__(self,position):
        self.position = position
        self.kid = set()
        self.value = 0

    def add_kid(self,it):
        self.kid.add(it)

    def add_val(self,val):
        self.value += val

    def calc(self,val=0):
        self.add_val(val)
        # print("called",self.position)
        for i in self.kid:
            if(i.position in visited)):
                continue
            visited.add(i.position)
            i.calc(self.value)

    def print_val(self):
        print(self.value,end=" ")

N,Q = [int(x) for x in input().split()]
ki = [Tree(i) for i in range(N)]

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

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1].add_val(val)

visited = set()
visited.add(0)
ki[0].calc()

for i in ki:
    i.print_val()
print()

Submission status: Runtime Error

Solution code:
N,Q = [int(x) for x in input().split()]
ki = [0]*N
tr = [[] for _ in range(N)]

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

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1] += val

stack =[1]
vivisted = set()

while stack:
    i = stack.pop()
    for j in tr[i]:
        if j in visited:
            continue
        visited.add(j)
        ki[j] += ki[i]
        stack.append(j)

for i in ki:
    print(i,end=" ")
print()

Submission status: Runtime Error

Solution code:
class Tree:
    value=0
    def __init__(self):
        self.kid = []
        self.value = 0

    def add_kid(self,it):
        self.kid.append(it)

    def add_val(self,val):
        self.value += val

    def calc(self,val=0):
        self.add_val(val)
        # print("called",len(self.kid))
        for i in self.kid:
            i.calc(self.value)

    def print_val(self):
        print(self.value,end=" ")

N,Q = [int(x) for x in input().split()]
ki = [Tree() for _ in range(N)]

for i in range(N-1):
    parent,kid = [int(x) for x in input().split()]
    ki[parent-1].add_kid(ki[kid-1])

for i in range(Q):
    position,val = [int(x) for x in input().split()]
    ki[position-1].add_val(val)

ki[0].calc()

for i in ki:
    i.print_val()
print()

Submission status: Runtime Error

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

E = [[] for _ in range(N + 1)]
X = [0 for _ in range(N + 1)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    E[a].append(b)
    E[b].append(a)

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

visited = [False for _ in range(N + 1)]
visited[1] = True


def add(i, val):
    X[i] += val
    for to in E[i]:
        if visited[to]:
            continue
        visited[to] = True
        add(to, X[i])


add(1, 0)

print(*X[1:])

Submission status: Runtime Error

Solution code:
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,Q = LI()
ab = [LI() for _ in [None]*(N-1)]
px = [LI() for _ in [None]*(Q)]
ans = [0]*(N+1) #1_index
graph = {i:collections.deque() for i in range(1,n+1)} #1_index
for a,b in ab:
        graph[a].append(b)
        graph[b].append(a)
for p,x in px:
    ans[p] += x
seen = [0]*(N+1) #1_index
stack = []
def dfs():
    seen[1] = 1
    stack.append(1)
    while stack:
        s = stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            g_NO = graph[s].popleft()
            if seen[g_NO]:
                continue
            seen[g_NO] = 1
            stack.append(g_NO)
            ans[g_NO] += ans[s]
dfs()
print(*ans[1:])

Submission status: Runtime Error

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

count = [0]*N
bubun = [[i] for i in range(N)]

def dfs(v):
    Vs = query[v]
    if len(Vs)==0:
        return [v]
    lis = []
    for i in Vs:
        lis += dfs(i)
    bubun[v] += lis
    return bubun[v]

dfs(0)

for i in range(Q):
    p, x = map(int, input().split())
    B = set(bubun[p-1])
    for i in B:
        count[i] += x
    
print(*count)

Submission status: Runtime Error

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

count = [0]*N
bubun = [[i] for i in range(N)]

def dfs(v):
    Vs = query[v]
    if len(Vs)==0:
        return [v]
    lis = []
    for i in Vs:
        lis += dfs(i)
    bubun[v] += lis
    return bubun[v]

dfs(0)

for i in range(Q):
    p, x = map(int, input().split())
    B = bubun[p-1]
    for i in B:
        count[i] += x
    
print(*count)

Submission status: Runtime Error

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

count = [0]*N

def dfs(v):
    global x
    if checked[v]:
        return
    checked[v] = 1
    count[v] += x
    for i in query[v]:
        if not checked[i]:
            dfs(i)

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

print(*count)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
 
# 遅延評価で加えてあげるだけ
 
N,Q = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

graph = [[] for _ in range(N+1)]
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
 
value = [0] * (N+1)
for p,x in px:
    value[p] += x
    
q = [(1,0)]
while q:
    x,parent = q.pop()
    value[x] += value[parent]
    for y in graph[x]:
        if y == parent:
            continue
        q.append((y,x))
 
answer = ' '.join(map(str,value[1:]))
print(answer)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(N-1)]
px = [list(map(int,input().split())) for i in range(Q)]

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

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

from collections import deque
todo = deque([(1,0])])
while len(todo) > 0:
    x,parent = todo.pop()
    value[x] += value[parent]
    for y in graph[x]:
        if y == parent:
            continue
        todo.append((y,x))

print(*value[1:])

Submission status: Runtime Error

Solution code:
def i_map(): return map(int, input().split())
sys.setrecursionlimit(10 ** 7)

def dfs(current, parent, edges, ans_list):
    for child in edges[current]:
        if child == parent:
            continue
        ans_list[child] += ans_list[current]
        dfs(child, current, edges, ans_list)

def main():
    n, q = i_map()

    edges = [[] for _ in range(n)]
    for i in range(n-1):
        a, b = i_map()
        edges[a-1].append(b-1)
        edges[b-1].append(a-1)

    ans_list = [0] * n
    for i in range(q):
        p, x = i_map()
        ans_list[p-1] += x

    dfs(0, -1, edges, ans_list)

    print(*ans_list)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import heapq
import sys
sys.setrecursionlimit(100000)

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

lv =[[] for _ in range(n)]
ans=[0] * (n)

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

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

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

dfs(0)

for i in ans:print(i)

Submission status: Runtime Error

Solution code:
import heapq

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

lv =[[] for _ in range(n)]
ans=[0] * (n)

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

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

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

dfs(0)

for i in ans:print(i)

Submission status: Runtime Error

Solution code:
#@hoshikawa1309  Qiita
#dfsを用いて累積和を計算する
import sys
sys.setrecursionlimit(10 ** 6)
def input():#これをしないとTLE
    return sys.stdin.readline()[:-1]#このスライスは1行ずつ入力？
N , Q = map(int,input().split())
graph = [[] for _ in range(N)]
point = [0] * N
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a - 1].append(b - 1)#0-index
    graph[b - 1].append(a - 1)#0-index
for _ in range(Q):
    a , b = map(int,input().split())
    a = a - 1#0-index
    point[a] += b#問題で与えられる、その頂点についての得点をプラス
# 初期状態だと前の値がないためデフォルト引数に-1を代入
def dfs(now , prev = -1):#いま見ている頂点と、どの頂点から来たかが引数
    #for文で、いま見ている頂点に繋がっている、次に頂点について以下ABC
    for next in graph[now]:
        # 次のノードが前に参照した値の時はcontinue何もしない
        if next == prev:       continue　　#A.逆走防止
        # 現在の値を次のポイントに加算することで累積和をとる
        point[next] += point[now]#B.実際の処理は、この累積和だけ
        # 次のノードと現在のノードを引数にdfsを継続する
        dfs(next , now)#C.深く探索するイメージ
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
import queue


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

dic = {}
for k in range(N-1):
  dic[a[k]] = []
for k in range(N-1):
  dic[a[k]].append(b[k])
ans = []
while len(dic) > 0:
  q = queue.Queue()
  qlist = []
  q.put(next(iter(dic)))
  while not q.empty():
    print(dic,q.queue)
    d = q.get()
    qlist.append(d)
    if d in dic:
      for l in dic[d]:
        q.put(l)
      dic.pop(d)
  ans.append(qlist)
print(ans)

ret = [0] * N
for k in range(len(ans)):
  for j in range(len(p)):
    if j in ans[k]:
      for l in ans[k]:
        ret[l] += x[j]
print(ret)

Submission status: Runtime Error

Solution code:
def count(x):
    if len(child_list[x]) > 0:
        for index in child_list[x]:
            total_list[index] += total_list[x]
            count(index)

n, q = map(int, input().split())
child_list = [[] for _ in range(n)]
position_list = [10 * 6] * n
position_list[0] = 1
input_list = []
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if a < b:
        a, b = b, a
    input_list.append([b, a])
input_list.sort()
for a, b in input_list:
    if position_list[a] < position_list[b]:
        a, b = b, a
    position_list[a] = position_list[b] + 1
    child_list[b].append(a)
total_list = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    total_list[p - 1] += x

count(0)
print(*total_list)

Submission status: Runtime Error

Solution code:
def m():
    n, q = map(int, input().split())
    tree = [[] for _ in range(n + 1)]
    for i in range(1, n):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    counter = [0 for _ in range(n + 1)]
    for _ in range(q):
        p, x = map(int, input().split())
        counter[p] += x

    ans = [0] * (n + 1)
    visited = []

    def dfs(root, point):
        point += counter[root]
        ans[root] = point
        visited.append(root)
        for j in tree[root]:
            if j in visited:
                continue
            dfs(j, point)
        return

    dfs(1, 0)
    for i in range(1, n + 1):
        print(ans[i], '', end='')
    return ans


m()

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
ab=sorted([list(map(int,input().split())) for _ in range(n-1)],key=lambda x:x[0])
d={}
for i in range(q):
  q,x=map(int,input().split())
  if q in d:
    d[q]+=x
  else:
    d[q]=x
ans=[0]*n
d2={}
for i in ab:
  if i[0] in d:
    if ans[i[0]-1]==0:
      ans[i[0]-1]+=d[i[0]]
  if i[1] in d:
    if ans[i[1]-1]==0:
      ans[i[1]-1]+=d[i[1]]
  ans[i[1]-1]+=ans[i[0]-1]
  if i[1] in d2:
    ans[i[1]-1]-=d[i[1]-1]
    ans[i[0]-1]+=ans[i[1]-1]
  else:
    d2[i[1]]=0
print(' '.join(list(map(str,ans))))

Submission status: Runtime Error

Solution code:
from subprocess import *
call(('pypy3','-c'."""
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    n,q=map(int,input().split())
    P=[[] for _ in range(n)]
    for i in range(n-1):
        a,b=map(int,input().split())
        P[a-1].append(b-1)
        P[b-1].append(a-1)
    X=[0]*n
    for i in range(q):
        p,x=map(int,input().split())
        X[p-1]+=x
    deq=deque([0])
    visited=[False]*n 
    while deq:
        node=deq.popleft()
        if not(visited[node]):
            visited[node]=True
            for p in P[node]:
                if not(visited[p]):
                    X[p]+=X[node]
                    deq.append(p)
    print(*X)

if __name__=='__main__':
    main()
"""))

Submission status: Runtime Error

Solution code:
#138-d
from collections import deque

def bfs(edges, u, p, cost):
    #cost:今までのコスト
    #root0から順に、costsを更新してゆく
    cost += costs[u]
    costs[u] = cost
    nodes = edges[u]
    for v in nodes:
        if v!=p:
            bfs(edges, v, u, cost)                


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

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

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

for _ in range(q):
    u,value = [int(i) for i in input().split()]
    costs[u-1] += value 
    
bfs(edges, 0, -1, 0)
print(*costs)

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] += [b]
  graph[b] += [a]


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

def dfs(u, pre, c):
  for v in graph[u]:
    if v != pre:
      cnt[v] += cnt[u]
      dfs(v, u, cnt[v])

dfs(0, -1, cnt[0])
print(*cnt)