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
sys.setrecursionlimit(10000)    
from collections import deque

seen=[]
first_order =[]
last_order = []
path=[]

def dfs(g, v, first_ptr, last_ptr,xi):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    first_order[v] = first_ptr
    path.append(v)
    seen[v]=True
#    c[v]+=xi
#    print(v,c,g[v])          
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr,xi)
    last_ptr=last_ptr+1
    first_ptr=last_ptr
    last_order[v] = last_ptr
    path.append(v)

    return first_ptr, last_ptr

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        pi,xi=map(int,input().split())
        p[i]=pi-1
        x[i]=xi
elif icase==1:
    n,q=3,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([2])
    g[1]=deque([2])
    g[2]=deque([0,1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=2
    x[1]=100
    p[2]=0
    x[2]=1    
#ans:1 111 101
elif icase==2:
    n,q=4,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1])
    g[1]=deque([0,2,3])
    g[2]=deque([1])
    g[3]=deque([1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=0
    x[1]=100
    p[2]=2
    x[2]=1    
#ans:100 110 111 110
elif icase==3:
    n,q=6,2
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1,2])
    g[1]=deque([0,3,4])
    g[2]=deque([0,5])
    g[3]=deque([1])
    g[4]=deque([1])
    g[5]=deque([2])
    p=[0]*q
    x=[0]*q
    p[0]=0
    x[0]=10    
    p[1]=0
    x[1]=10
#ans:100 110 111 110

xx=[0]*n
for i in range(q):
    xx[p[i]]=xx[p[i]]+x[i]
    
c=[0]*n

#for i in range(q):
#    seen=[False]*n
#    first_ptr=0
#    last_ptr=0
#    first_order =[-1]*n
#    last_order = [-1]*n
#    first_ptr, last_ptr=dfs(g,p[i],first_ptr, last_ptr,x[i])

seen=[False]*n
first_ptr=0
last_ptr=0
first_order =[-1]*n
last_order = [-1]*n
first_ptr, last_ptr=dfs(g,0,first_ptr, last_ptr,x[0])

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,q = map(int,input().split())
graph = [[] for _ in range(n)]
point = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
for _ in range(q):
    a,b = map(int,input().split())
    a = a -1
    point[a]+=b
def dfs(now, prev = -1):
    for next in graph[now]:
        if next == prev:
            continue
        point[next] += point[now]
        dfs(next, now)
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
graph = [[] for _ in range(n)]
point = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
for _ in range(q):
    a,b = map(int,input().split())
    a = a -1
    point[a]+=b
def dfs(now, prev = -1):
    for next in graph[now]:
        if next == prev:
            continue
        point[next] += point[now]
        dfs(next, now)
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:

import sys
sys.setrecursionlimit(10000)    
from collections import deque

seen=[]
first_order =[]
last_order = []

def dfs(g, v, first_ptr, last_ptr,xi):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True
    c[v]+=xi
#    print(v,c,g[v])          
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr,xi)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr


icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        pi,xi=map(int,input().split())
        p[i]=pi-1
        x[i]=xi

c=[0]*n

for i in range(q):
    seen=[False]*n
    first_ptr=0
    last_ptr=0
    first_order =[-1]*n
    last_order = [-1]*n
    first_ptr, last_ptr=dfs(g,p[i],first_ptr, last_ptr,x[i])

for i in range(n):
    print(c[i],end=" ")
print()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)    
from collections import deque

seen=[]
first_order =[]
last_order = []

def dfs(g, v, first_ptr, last_ptr,xi):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    
    first_order[v] = first_ptr
    seen[v]=True
    c[v]+=xi
#    print(v,c,g[v])          
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr,xi)
    last_ptr=last_ptr+1
    first_ptr=last_ptr

    last_order[v] = last_ptr
    return first_ptr, last_ptr


icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        pi,xi=map(int,input().split())
        p[i]=pi-1
        x[i]=xi

c=[0]*n

for i in range(q):
    seen=[False]*n
    first_ptr=0
    last_ptr=0
    first_order =[-1]*n
    last_order = [-1]*n
    first_ptr, last_ptr=dfs(g,p[i],first_ptr, last_ptr,x[i])

for i in range(n):
    print(c[i],end="")
print()

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(lambda x:int(x)-1, input().split())
    G[a].append(b)

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

from collections import deque
st = deque([[0, points[0]]])
result = [0] * N
while st:
    idx, count = st.pop()
    result[idx] = str(count)
    for ni in G[idx]:
        st.append([ni, count+points[ni]])

print(" ".join(result))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

n,q = map(int,input().split())
p = []
x = []
arr = [[] for i in range(n)]
ans = [0 for i in range(n)]
omosa = [0 for i in range(n)]

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

for i in range(q):
    omosa[p[i]] +=x[i]

def dfs(start, oya, weight): #weightは自分より上のノードの重さの和
    ans[start] = weight + omosa[start]
    for child in arr[start]:
        if child != oya:
            dfs(child, start, ans[start])
 
dfs(0,-1,0)
print(*ans)

Submission status: Runtime Error

Solution code:
#/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)

def input():
    return stdin.readline()

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

adj = [[] for _ in range(N)]

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

def dfs(node, parent):
    for child in adj[node]:
        if child == parent: continue
        values[child] += values[node]
        dfs(child, node)

dfs(0, -1)

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

Submission status: Runtime Error

Solution code:
#/usr/bin/env python3
N, Q = map(int, input().split())
     
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a, b = (a - 1, b - 1)
    adj[a].append(b)
    adj[b].append(a)
 
values = [0 for _ in range(N)]

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

def dfs(node, parent):
    for child in adj[node]:
        if child == parent: continue
        values[child] += values[node]
        dfs(child, node)

dfs(0, -1)

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

Submission status: Runtime Error

Solution code:
#/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6). 

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

adj = [[] for _ in range(N)]

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

def dfs(node, parent):
    for child in adj[node]:
        if child == parent: continue
        values[child] += values[node]
        dfs(child, node)

dfs(0, -1)

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

Submission status: Runtime Error

Solution code:
#/usr/bin/env python3

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

adj = [[] for _ in range(N)]

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

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

def dfs(node, parent):
    for child in adj[node]:
        if child == parent: continue
        values[child] += values[node]
        dfs(child, node)

dfs(0, -1)

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

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, Q =map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
 
g = [[] for _ in range(N+1)]
for a, b in ab:
    g[b].append(a)
    g[a].append(b)
cnt = [0]*(N+1)
 
for _ in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x
 
temp = [1]
visited = [0]*(N+1)
visited[1] = 1
 
while temp:
    q = temp.pop()   
    for i in g[q]:
        if visited[i] == 0:
            temp.append(i)
            visited[i] = 1
            cnt[i] += cnt[q]
print(*cnt[1:])
N, Q =map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]

g = [[] for _ in range(N+1)]
for a, b in ab:
    g[b].append(a)
    g[a].append(b)
cnt = [0]*(N+1)

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

temp = [1]
visited = [0]*(N+1)
visited[1] = 1

while temp:
    q = temp.pop()   
    for i in g[q]:
        if visited[i] == 0:
            temp.append(i)
            visited[i] = 1
            cnt[i] += cnt[q]
print(*cnt[1:])
提出情報
提出日時

Submission status: Runtime Error

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

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

l=[0]*n
l[0]=1

def dfs(now,last=-1):
    for to in edge[now]:
        if to==last:continue
        ans[to]+=ans[now]
        dfs(to,now)
dfs(0)


print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter
N, Q = map(int, input().split())

tree = {}
counter = Counter()

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

    a_in_tree = a in tree
    if a_in_tree == False:
        tree[a] = [b]
    else:
        tmp = tree[a]
        tmp.append(b)
        tree[a] = tmp

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

lst = [0 for i in range(N)]

def calc(node, num):
    cnt = num + counter[node]
    
    lst[node-1] = str(cnt)

    if node in tree:
        for t in tree[node]:
            calc(t, cnt)
        
calc(1, 0)

print(" ".join(lst))

Submission status: Runtime Error

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

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

def dfs(i,j,k):
    nex=g[i]
    lis[i]=k
    for d in nex:
        if d==j: continue
        nk=k+lis[d]
        dfs(d,i,nk)

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N, Q = map(int, input().split())
G = defaultdict(list)
for i in range(N - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1  # 0-indexed
    G[a].append(b)
    G[b].append(a)

# print('G', G)

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


def dfs(v, p):
    if p != -1:
        V[v] += V[p]
    for idx in G[v]:
        if idx == p:
            continue
        dfs(idx, v)


dfs(0, -1)

# print('V', V)

for v in V:
    print(v)

Submission status: Runtime Error

Solution code:
def input_undirected_graph(V: int, E: int, origin: int = 1):
    graph = [list() for _ in range(V)]
    for i in range(E):
        u, v = [int(x) for x in input().split()]
        u -= origin
        v -= origin
        graph[u].append(v)
        graph[v].append(u)
    return graph


def main():
    N, Q = [int(x) for x in input().split()]
    graph = input_undirected_graph(N, N-1)

    vals = [0] * N
    for _ in range(Q):
        p, x = [int(x) for x in input().split()]
        vals[p-1] += x
    
    ans = [0] * N
    # stack = [(0, -1, 0)]  # node, par, val
    # while stack:
    #     node, par, val = stack.pop()
    #     ans[node] += vals[node] + val

    #     for child in graph[node]:
    #         if child == par:
    #             continue
    #         stack.append((child, node, ans[node]))

    def dfs(node, par, val):
        ans[node] += vals[node] + val
        for child in graph[node]:
            if child == par:
                continue
            dfs(child, node, ans[node])
    dfs(0,-1,0)

    for i in range(N):
        print(ans[i])
 
 
if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(20005)
    main()

Submission status: Runtime Error

Solution code:
def input_undirected_graph(V: int, E: int, origin: int = 1):
    graph = [list() for _ in range(V)]
    for i in range(E):
        u, v = [int(x) for x in input().split()]
        u -= origin
        v -= origin
        graph[u].append(v)
        graph[v].append(u)
    return graph


def main():
    N, Q = [int(x) for x in input().split()]
    graph = input_undirected_graph(N, N-1)

    vals = [0] * N
    for _ in range(Q):
        p, x = [int(x) for x in input().split()]
        vals[p-1] += x
    
    ans = [0] * N
    def dfs(node, par, val):
        ans[node] += vals[node] + val

        for child in graph[node]:
            if child == par:
                continue
            dfs(child, node, ans[node])

    dfs(0, -1, 0)
    for i in range(N):
        print(ans[i])
 
 
if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10000)
    main()

Submission status: Runtime Error

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

N , Q = map(int,readline().rstrip().split())
G = [[] for _ in range(N)]
for n in range(N-1):
    a , b = map(int,readline().rstrip().split())
    G[a-1].append(b-1)
query = [0] * N
for q in range(Q):
    p , x = map(int,readline().rstrip().split())
    query[p-1] += x
counter = [0] * N 
visit = [0] * N

def dfs(root , x ):
    if visit[root] == 0:
        visit[root] = 1
        x += query[root]
        counter[root] += x
    else:
        return 0
    
    for g in G[root]:
        dfs( g, x )

    return 0

def solve():
    dfs(0 , 0)
    [print(c , end = " " ) for c in counter]
    print("")

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

N , Q = map(int,readline().rstrip().split())
G = [[] for _ in range(N)]
for n in range(N-1):
    a , b = map(int,readline().rstrip().split())
    G[a-1].append(b-1)
query = []
[query.append( list(map(int,readline().rstrip().split()))) for _ in range(Q) ]
counter = [0] * N 
# visit = []

def dfs(root , x , visit):
    if visit[root] == 0:
        visit[root] = 1
        counter[root] += x
    else:
        return 0
    
    for g in G[root]:
        dfs( g, x , visit)

    return 0

def solve():
    for q in query:
        visit = [0] * N
        dfs(q[0]-1,q[1] , visit)
    [print(c , end = " " ) for c in counter]
    print("")

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

N , Q = map(int,readline().rstrip().split())
G = [[] for _ in range(N)]
for n in range(N-1):
    a , b = map(int,readline().rstrip().split())
    G[a-1].append(b-1)
query = []
[query.append( list(map(int,readline().rstrip().split()))) for _ in range(Q) ]
counter = [0] * N 
# visit = []

def dfs(root , x , visit):
    if visit[root] == 0:
        visit[root] = 1
        counter[root] += x
    else:
        return 0
    
    for g in G[root]:
        dfs( g, x , visit)
    
    return 0

def solve():
    for q in query:
        visit = [0] * N
        dfs(q[0]-1,q[1] , visit)
    [print(c , end="" ) for c in counter]
    print("")

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

N , Q = map(int,readline().rstrip().split())
G = [[] for _ in range(N)]
for n in range(N-1):
    a , b = map(int,readline().rstrip().split())
    G[a-1].append(b-1)
query = []
[query.append( list(map(int,readline().rstrip().split()))) for _ in range(Q) ]
counter = [0] * N 
# visit = []

def dfs(root , x , visit):
    if visit[root] == 0:
        visit[root] = 1
        counter[root] += x
    else:
        return 0
    
    for g in G[root]:
        dfs( g, x , visit)
    
    return 0

def solve():
    for q in query:
        visit = [0] * N
        dfs(q[0]-1,q[1] , visit)
    [print(c) for c in counter]

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100001)

def dfs(node,cnt):
  for nd in tree[node]:
    if visited[nd] > -1:
      continue
    counter[nd] += cnt
    visited[nd] =1
    dfs(nd,cnt+p[nd])
    


N,Q= map(int,input().split())
tree = {}
p = {}
counter = [0]*(N+1)
visited = [-1]*(N+1)
for i in range(1,N+1):
  tree[i] = []
  p[i] = 0
  
for i in range(N-1):
  a,b = map(int,input().split())
  tree[a].append(b)
  tree[b].append(a)
  
for i in range(Q):
  p_i,x = map(int,input().split())
  p[p_i] += x
  
visited[1] = 1
counter[1] += p[1]
dfs(1,p[1])

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

Submission status: Runtime Error

Solution code:
def dfs(i,x):
    ans[i]+=x
    if dist[i]==bottom:
        return
    for n in tree[i]:
        if dist[n]>dist[i]:
            dfs(n,x)

from collections import deque

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

tree=[[] for _ in range(N)]

for _ in range(N-1):
    A,B=map(int,input().split())
    tree[A-1].append(B-1)
    tree[B-1].append(A-1)

#BFSで各ノードの深さを調べる
dist = [-1]*N
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in tree[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)

#カウンターをDFSで制御
ans=[0 for _ in range(N)]
bottom=max(dist)
for j in range(Q):
    p,x=map(int,input().split())
    dfs(p-1,x)

print(*ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 17 19:21:21 2020

@author: Kanaru Sato
"""

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

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

visited = [0 for i in range(n+1)]
point = [0 for i in range(n+1)]

def dfs(n,p):
    visited[n] = 1
    point[n] += p
    if connected[n]:
        for v in connected[n]:
            if visited[v] == 0:
                vpoint = p+operations[v]
                dfs(v,vpoint)

dfs(0,0)

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
N,Q=map(int, sys.stdin.readline().split())

al=[ [] for _ in range(N+1) ]	#隣接リスト
for _ in range(N-1):
	u,v=map(int, sys.stdin.readline().split())
	al[u].append(v)
	al[v].append(u)

increment=[ 0 for _ in range(N+1) ]
for _ in range(Q):
    p,x=map(int, sys.stdin.readline().split())
    increment[p]+=x

socre=[ 0 for _ in range(N+1) ] #各頂点のカウンターの値を記録
visit=[ 0 for _ in range(N+1) ] #0なら未訪問、1なら訪問済み
point=0
def dfs(par,cur):
    visit[cur]=1
    global point
    point+=increment[cur]
    socre[cur]+=point       #各頂点のカウンターの値を記録
    old=point   #拡張点が子孫に遷移する前のカウンターの値を記録
    for to in al[cur]:
        if visit[to]==0:
            dfs(cur,to)
            point=old   #子孫に遷移し、増えたカウンターの値を自分の時の点数に戻す
dfs(-1,1)

for i in range(1,N+1):
    print socre[i],

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    N,Q=map(int,input().split())
    M = N-1  # 頂点数，辺数
    ans=[0]*N
    # グラフ入力受取 (ここでは無向グラフを想定)
    G = [[] for _ in range(N)]
    for i in range(M):
        a, b = map(int, input().split())
        a-=1
        b-=1
        G[a].append(b)
        G[b].append(a)
    
    for i in range(Q):
        p,x=map(int,input().split())
        p-=1
        ans[p]+=x

        
    
    seen = [False]*N
    
    
    def dfs(G, v=-1):    
        seen[v] = True
    
        for next_v in G[v]:
            if seen[next_v]:
                continue
            ans[next_v]+=ans[v]
            dfs(G, next_v)
    
    
    # 頂点 0 をスタートとした探索
    dfs(G, 0)
    print(*ans)
    
    
    
resolve()

Submission status: Runtime Error

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


class Tree(object):
    def __init__(self, num):
        self.num = num
        self.childs = []
        self.count = 0


def dfs(p, x):
    """
    add x to all elements which have p as parent
    :param p: root of subtree, Tree class
    :param x: numbers to add, int
    """
    # add x to p.count
    p.count += x

    # recurrsive call for direct children of p
    for child in p.childs:
        dfs(child, x)


trees = [Tree(num) for num in range(N)]

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


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


res = ""
for n, tree in enumerate(trees):
    if n != len(trees):
        res += str(tree.count) + " "
    else:
        res += str(tree.count)

print(res)

Submission status: Runtime Error

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


class Tree(object):
    def __init__(self, num):
        self.num = num
        self.childs = []
        self.count = 0


def dfs(p, x):
    """
    add x to all elements which have p as parent
    :param p: root of subtree, Tree class
    :param x: numbers to add, int
    """
    # add x to p.count
    p.count += x

    # recurrsive call for direct children of p
    for child in p.childs:
        dfs(child, x)


trees = [Tree(num) for num in range(N)]

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


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


for tree in trees:
    print(tree.count, end="")

Submission status: Runtime Error

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


class Tree(object):
    def __init__(self, num):
        self.num = num
        self.childs = []
        self.count = 0


def dfs(p, x):
    """
    add x to all elements which have p as parent
    :param p: root of subtree, Tree class
    :param x: numbers to add, int
    """
    # add x to p.count
    p.count += x

    # recurrsive call for direct children of p
    for child in p.childs:
        dfs(child, x)


trees = [Tree(num) for num in range(N)]
print(trees)

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


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


for tree in trees:
    print(tree.count)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)

n, q = map(int, input().split())
root = [[] for _ in range(n)]
ans = [0 for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    root[a - 1].append(b - 1)
    root[b - 1].append(a - 1)
for _ in range(q):
    p, x = map(int, input().split())
    ans[p - 1] += x
    
def dfs(pos, before):
    for next_pos in root[pos]:
        if next_pos == before:
            continue
        ans[next_pos] += ans[pos]
        dfs(next_pos, pos)

dfs(0, -1)

for i in range(n):
    print(ans[i], end=" ")import sys

sys.setrecursionlimit(10 ** 9)

n, q = map(int, input().split())
root = [[] for _ in range(n)]
ans = [0 for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    root[a - 1].append(b - 1)
    root[b - 1].append(a - 1)
for _ in range(q):
    p, x = map(int, input().split())
    ans[p - 1] += x
    
def dfs(pos, before):
    for next_pos in root[pos]:
        if next_pos == before:
            continue
        ans[next_pos] += ans[pos]
        dfs(next_pos, pos)

dfs(0, -1)

for i in range(n):
    print(ans[i], end=" ")

Submission status: Runtime Error

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

from collections import defaultdict as dd
n, q = map(int, input().split())
Tree = dd(lambda: [])
for i in range(n-1):
    a, b = map(int, input().split())
    Tree[a].append(b)

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

Ans = {}
def write(node, point):
    point += Point[node]
    Ans[node] = point
    for next_node in Tree[node]:
        write(next_node, point)
write(1, 0)

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
n, q = map(int, input().split())
Tree = dd(lambda: [])
for i in range(n-1):
    a, b = map(int, input().split())
    Tree[a].append(b)

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

Ans = dd(lambda: 0)
def write(node, point):
    point += Point[node]
    Ans[node] = point
    for next_node in Tree[node]:
        write(next_node, point)
write(1, 0)

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
n, q = map(int, input().split())
Tree = dd(lambda: [])
for i in range(n-1):
    a, b = map(int, input().split())
    Tree[a].append(b)

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

Ans = {}
def write(node, point):
    point += Point[node]
    Ans[node] = point
    for next_node in Tree[node]:
        write(next_node, point)
write(1, 0)

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

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:n+q]]

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

    seen[idx] = True
    for i in g[idx]:
        if seen[i]: continue
        score[i] += score[idx]
        dfs(g, i, seen)

g = [[] for _ in range(n)]
for abi in ab:
    # 無向グラフはお互いが参照し合う
    g[abi[0]-1].append(abi[1]-1)
    g[abi[1]-1].append(abi[0]-1)

score = [0]*n
for pxi in px:
    score[pxi[0]-1] += pxi[1]

seen = [False]*n
dfs(g, 0, seen)

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

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:n+q]]

import sys
sys.setrecursionlimit(10**10)

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

    seen[idx] = True
    for i in g[idx]:
        if seen[i]: continue
        score[i] += score[idx]
        dfs(g, seen, i)

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

score = [0]*n
for pxi in px:
    score[pxi[0]-1] += pxi[1]

seen = [False]*n
dfs(g, seen, 0)

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

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:n+q]]

import sys
sys.setrecursionlimit(2*10**5)

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

    seen[idx] = True
    for i in g[idx]:
        if seen[i]: continue
        score[i] += score[idx]
        dfs(g, seen, i)

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

score = [0]*n
for pxi in px:
    score[pxi[0]-1] += pxi[1]

seen = [False]*n
dfs(g, seen, 0)

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

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:n+q]]

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)

for idx in range(n):
    g[idx] = sorted(g[idx])

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

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