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=map(int,input().split())
egde=[[] for _ in range(N)]
cnt=[0]*N
for _ in range(N-1):
  a,b=map(int,input().split())
  edge[a-1]+=(b-1),
  edge[b-1]+=(a-1),
for _ in range(Q):
  p,x=map(int,input().split())
  cnt[p-1]+=x
done=[False]*N
done[0]=True
stack=[0]
while stack:
  par=stack.pop()
  for child in edge[par]:
    if not done[child]:
      done[child]=True
      cnt[child]+=cnt[par]
      stack.append(child)
print(*cnt)

Submission status: Runtime Error

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

def score(p, c):
    ans[p] += c
    for child in node[p]:
        score(child, ans[p])

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

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

score(0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    edges = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        edges[a].append(b)
        edges[b].append(a)

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

    visited = [False]*N
    visited[0] = True

    def dfs(v: int):
        nonlocal visited
        nonlocal cnt
        if all(visited):
            return
        for n in edges[v]:
            if visited[n]:
                continue
            visited[n] = True
            cnt[n] += cnt[v]
            dfs(n)
        return

    dfs(0)
    print(*cnt)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
from collections import defaultdict
dic = defaultdict(list)
for _ in range(n-1):
    a,b = map(int,input().split())
    dic[a-1].append(b-1)
    dic[b-1].append(a-1)

from collections import defaultdict
dicp = defaultdict(int)

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


ansl = [0 for i in range(n)]
used = [0 for i in range(n)]
def dfs(pos, point):
    point += dicp[pos]
    used[pos] = 1
    ansl[pos] = point
    for p in dic[pos]:
        if used[p] == 0:
            dfs(p, point)

dfs(0, 0)

for a in ansl:
    print(a)

Submission status: Runtime Error

Solution code:
def dfs(G,s,p):
    for nv in G[s]:
        if(nv==p):
            continue
        Count[nv]+=Count[s]
        dfs(G,nv,s)
    
N,Q=map(int,input().split())
Count=[0]*N
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
    G[b].append(a)
for i in range(Q):
    p,x=map(int,input().split())
    Count[p-1]+=x
dfs(G,0,-1)
print(*Count)

Submission status: Runtime Error

Solution code:
def dfs(G,s,p):
    for nv in G[s]:
        if(nv==p):
            continue
        Count[nv]+=Count[s]
        dfs(G,nv,s)
    
N,Q=map(int,input().split())
Count=[0]*N
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
for i in range(Q):
    p,x=map(int,input().split())
    Count[p-1]+=x
dfs(G,0,-1)
print(*Count)

Submission status: Runtime Error

Solution code:
def dfs(G,s):
    for nv in G[s]:
        Count[nv]+=Count[s]
        dfs(G,nv)
    
N,Q=map(int,input().split())
Count=[0]*N
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
for i in range(Q):
    p,x=map(int,input().split())
    Count[p-1]+=x
dfs(G,0)
print(*Count)

Submission status: Runtime Error

Solution code:
def dfs(G,s,x):
    Counter[s]+=x
    for nv in G[s]:
        dfs(G,nv,x)
    
N,Q=map(int,input().split())
Counter=[0]*N
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
for i in range(Q):
    p,x=map(int,input().split())
    dfs(G,p-1,x)
print(*Counter)

Submission status: Runtime Error

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

def update(p, x):
    counter[p] += x
    for pp in tree[p]:
        update(pp, x)
    
tree = {i: [] for i in range(N)}
counter = [0] * N
for _ in range(N-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
for _ in range(Q):
    p, x = map(int, input().split())
    update(p-1, x)
print(" ".join(map(str, counter)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
def input():
    return sys.stdin.readline()[:-1]
 
n , q = map(int,input().split())
point = [0] * n
edge = [[] for i in range(n)]
 
for i in range(n-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edge[a].append(b)
    edge[b].append(a)
 
for _ in range(q):
    a , b = map(int,input().split())
    point[a-1] += b
 
def dfs(now, last=-1):
    for nxt in edge[now]:
        if nxt==last: continue
        point[nxt] += point[now]
        dfs(nxt, now)
dfs(0)
print(*point)
import sys
sys.setrecursionlimit(10 ** 6)
def input():
    return sys.stdin.readline()[:-1]

n , q = map(int,input().split())
point = [0] * n
edge = [[] for i in range(n)]

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

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

def dfs(now, last=-1):
    for nxt in edge[now]:
        if nxt==last: continue
        point[nxt] += point[now]
        dfs(nxt, now)
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
V = [[] for _ in range(N)]
X = [0 for _ in range(N)]

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

for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    X[p] += x
# p親、n現在、s合計
def dfs(p, n, s):
    X[n] += s
    for i in V[n]:
        if i == p:
            continue
        else:
            dfs(n, i, X[n])
dfs(-1,0,0)
for i in range(N):
    print(X[i], end=" ")

Submission status: Runtime Error

Solution code:
import sys

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

adj_list = [[] for _ in range(n + 1)]

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

val = [0] * (n + 1)
visited = [False] * (n + 1)

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


def dfs(val, adj_list, visited, cur, prev):
    if prev != -1:
        val[cur] += val[prev]

    visited[cur] = True

    for next in adj_list[cur]:
        if visited[next] is False:
            dfs(val, adj_list, visited, next, cur)


dfs(val, adj_list, visited, 1, -1)

for i in range(n):
    print(val[i + 1], end="")
    if i != n - 1:
        print(" ", end="")
    else:
        print()

Submission status: Runtime Error

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

adj_list = [[] for _ in range(n + 1)]

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

val = [0] * (n + 1)
visited = [False] * (n + 1)

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


def dfs(val, adj_list, visited, cur, prev):
    if prev != -1:
        val[cur] += val[prev]

    visited[cur] = True

    for next in adj_list[cur]:
        if visited[next] is False:
            dfs(val, adj_list, visited, next, cur)


dfs(val, adj_list, visited, 1, -1)

for i in range(n):
    print(val[i + 1], end="")
    if i != n - 1:
        print(" ", end="")
    else:
        print()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#26　AtCoder Beginner Contest 138 D - Ki
n, q = map(int, input().split())
edge = [[] for _ in range(n)]
counter = [[] for _ in range(n)]
ans_list = [0]*n

def dfs(start, count):
    ans_list[start] += count
    for c in edge[start]:
        dfs(c, count)

for _ in range(n-1):
    u, v = map(int, input().split())
    edge[u-1].append(v-1)
for _ in range(q):
    u, v = map(int, input().split())
    dfs(u-1, v)
print(*ans_list)

Submission status: Runtime Error

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]

n,q = LI()

v = [[] for i in range(n)]
for i in range(n-1):
  a,b = LI()
  a -= 1 
  b -= 1 
  v[a].append(b)
  v[b].append(a)

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

def dfs(x,d,s): 
  for y in v[x]:
    if d[y]: 
      s += f[y]
      ans[y] = s 
      d[y] = 0 
      dfs(y,d,s) 
      s -= f[y] 

ans = [0]*n
d = [1]*n
d[0] = 0
ans[0] = f[0]
s = f[0]
dfs(0,d,s)
print(*ans)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
A_B = []
for _ in range(N):
    A_B.append(list(map(int, input().split())))
P_X = []
for _ in range(Q):
    P_X.append(list(map(int, input().split())))

counters = [0 for _ in range(N)]
def op(p,x):
    for a,b in A_B:
        if a == p:
            counters[a-1] += x
    return counters

for p, x in P_X:
    op(p,x)
    
counters = list(map(str, counters))
print(" ".join(counters))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
A_B = []
for _ in range(N):
    A_B.append(map(int, input().split()))
P_X = []
for _ in range(Q):
    P_X.append(map(int, input().split()))

counters = [0 for _ in range(N)]
def op(p,x):
    for a,b in A_B:
        if a == p:
            counters[a] += x
    return counters

for p, x in P_X:
    op(p,x)
    
counters = list(map(str, counters))
print(" ".join(counters))

Submission status: Runtime Error

Solution code:
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)
    graph[b-1].append(a-1)
for _ in range(Q):
    a,b=map(int,input().split())
    a=a-1
    point[a]=point[a]+b

def dfs(now,prev=-1):
    for i in graph[now]:
        if i==prev:
            continue
        point[i]=point[i]+point[now]
        dfs(i,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 _ in range(N-1):
    a,b=map(int,input().split())
    graph[a-1].append(b-1)
for _ in range(Q):
    a,b=map(int,input().split())
    a=a-1
    point[a]=point[a]+b

def dfs(now,prev=-1):
    for i in graph[now]:
        if i==prev:
            continue
        point[i]=point[i]+point[now]
        dfs(i,now)
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
import sys
from collections import OrderedDict

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


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = set()
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].add(v2)
        self.adjacency_dict[v2].add(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    def dfs_stack(self, start):
        stack = [start]
        visited = set()

        parent_vertex = 0
        while stack != []:
            current_vertex = stack.pop()
            if parent_vertex != 0:
                self.counter_dict[current_vertex] += self.counter_dict[parent_vertex]
            visited.add(current_vertex)
            for v in self.adjacency_dict[current_vertex]:
                if v not in visited and v not in stack and v != parent_vertex:
                    stack.append(v)
            parent_vertex = current_vertex

    def dfs_rec(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    # tree.dfs_stack(1)
    tree.dfs_rec(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

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
     
    def dfs(v,parent,add):
        value[v] += add
        for x in graph[v]:
            if x == parent:
                continue
            dfs(x,v,value[v])
     
    dfs(1,0,0)
     
    answer = ' '.join(map(str,value[1:]))
    print(answer)

Submission status: Runtime Error

Solution code:
def dfs(current, prev):
    for next in g[current]:
        if next != prev:
            if visited[next]:
                ki = False
            else:
                visited[next] = True
                dfs(next, now)


n, m = map(int, input().split())
g = [[] * n for i in range(n)]
ans = 0
for i in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
visited = [False for i in range(n)]
for i in range(n):
    if not visited[i]:
        ki = True
        dfs(i, -1)
        if ki:
            ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from functools32 import lru_cache
from collections import OrderedDict

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


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    @lru_cache
    def dfs(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    @lru_cache
    def dfs(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

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

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

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

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

def dfs(graph,counter,edge):
    for e in graph[edge]:
        if parent[edge] == e:
            continue
        else:
            parent[e] = edge
            counter[e] += counter[edge]
            dfs(graph,counter,e)

dfs(graph,counter,1)

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

Submission status: Runtime Error

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

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

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

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

counter = [0 for _ in range(N+1)]
#counter[i]:i番目の頂点のカウンター
check = [False for _ in range(N+1)]
check[1] = True
for p,x in PX:
    counter[p] += x

def bfs(graph,counter,edge):
    for e in graph[edge]:
        if check[e]:
            continue
        else:
            counter[e] += counter[edge]
            check[e] = True
            bfs(graph,counter,e)

bfs(graph,counter,1)

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

Submission status: Runtime Error

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

cnt=[0]*(N+1)
for el in px:
    cnt[el[0]]+=el[1]
#print(cnt)

todo=[]
seen=[False]*(N+1)

lst=[[] for _ in range(N+1)]
for el in ab:
    lst[el[0]].append(el[1])
#print(lst)

def bfs(n):
    seen[n]=True
    for child in lst[n]:
        if seen[child]==False:
            cnt[child]+=cnt[n]
            todo.append(child)
    if todo:
        bfs(todo.pop(0))
    else:
        return None

bfs(1)
#print(cnt)
print(' '.join(list(map(str, cnt[1:]))))

Submission status: Runtime Error

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

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

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

def f(pi, pp=-1):
    for j in nd[pi]:
        if j == pp:
            continue
        ans[j] += ans[pi]
        f(j, pi)

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

f(0, 0)
for a in ans:
    print(a)

Submission status: Runtime Error

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

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

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

def f(pi, pp=-1):
    for j in nd[pi]:
        if j == pp:
            continue
        ans[j] += ans[pi]
        f(j, pi)

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

f(1, 1)
for a in ans[1:]:
    print(a)

Submission status: Runtime Error

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

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

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

c = [True for ii in range(n + 1)]

def f(pi, p=-1):
    for j in nd[pi]:
        # print(j, pi)
        if j == p:
            continue
        ans[j] += ans[pi]
        f(j, pi)

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

f(1, 1)
print(*ans[1:])

Submission status: Runtime Error

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

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

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

c = [True for ii in range(n + 1)]

def f(pi, p=-1):
    for j in nd[pi]:
        if j != p:
            ans[j] += ans[pi]
            f(j, pi)

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


f(1, 1)

print(*ans[1:])

Submission status: Runtime Error

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

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

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

c = [True for ii in range(n + 1)]

def f(pi, p=-1):
    for j in nd[pi]:
        if j != p:
            ans[j] += ans[pi]
            f(j, pi)

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


f(1, 1)

print(*ans[1:

Submission status: Runtime Error

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

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

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

c = [True for ii in range(n + 1)]

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

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


f(1, ans[1])

print(*ans[1:])

Submission status: Runtime Error

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

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

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

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

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

c = [True for ii in range(n + 1)]
f(1, ans[1])

print(*ans[1:])

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
y = [[] for _ in range(n)]
c = [0 for _ in range(n)]
for _ in range(n-1):
  ai,bi = map(int,input().split())
  y[ai-1].append(bi-1)
  y[bi-1].append(ai-1)
for _ in range(q):
  pi,xi = map(int,input().split())
  c[pi-1] += xi
def f(j,b):
  for yj in y[j]:
    if yj != b:
      c[yj] += c[j]
      f(yj,j)
f(0,-1)
for i in range(n):
  print(c[i],end=" ")

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from operator import setitem

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def main():
    n, q = map(int, input().split())
    edges = [[] for _ in range(n+1)]

    for a, b in readlines(n-1):
        edges[a].append(b)
        edges[b].append(a)

    counter = [0] * (n+1)
    for q, x in readlines(q):
        counter[q] += x

    def dfs(u, prev):
        for v in edges[u]:
            if v == prev:
                continue
            counter[v] += counter[u]
            dfs(v, u)

    dfs(1, 0)

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

main()

Submission status: Runtime Error

Solution code:
import sys
n,q = map(int,sys.stdin.readline().rstrip().split())
t = {}
for i in range(n-1):
    a,b = map(int,sys.stdin.readline().rstrip().split())
    if a in t:
        t[a].append(b)
    else:
        t[a] = [b]
ans = [0 for i in range(n)]

def search(j):
    if j in t:
        l = [j]
        for k in t[j]:
            l = l + search(k)
    else:
        l = [j]
    return l

for i in range(q):
    p,x = map(int,sys.stdin.readline().rstrip().split())
    s = search(p)
    for i in s:
        ans[i-1] += x
print(" ".join(map(str,ans)))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from operator import setitem

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def main():
    n, q = map(int, input().split())
    edges = {i: 0 for i in range(n+1)}

    bigroot = False
    for a, b in readlines(n-1):
        if not bigroot and edges[b] > 0:
            edges = {v: k for k, v in edges.items()}
            bigroot = True

        if bigroot:
            edges[a] = b
        else:
            edges[b] = a

    counter = [0] * (n+1)

    for q, x in readlines(q):
        counter[q] += x

    iterator = range(n, 0, -1) if bigroot else range(1, n+1)
    for i in iterator:
        parent = edges[i]
        counter[i] += counter[parent]

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

main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from operator import setitem

def readlines(n):
    for _ in range(n):
        a, b = map(int, input().split())
        yield a, b

def main():
    n, q = map(int, input().split())
    edges = {i: 0 for i in range(n+1)}

    bigroot = False
    for a, b in readlines(n-1):
        if not bigroot and edges[b] > 0:
            edges = {v: k for k, v in edges.items()}
            bigroot = True

        if bigroot:
            edges[a] = b
        else:
            edges[b] = a

    counter = [0] * (n+1)

    for q, x in readlines(q):
        counter[q] += x

    iterator = range(n+1, 0, -1) if bigroot else range(1, n+1)
    for i in iterator:
        parent = edges[i]
        counter[i] += counter[parent]

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

main()

Submission status: Runtime Error

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

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

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

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

if __name__ == "__main__":
  dfs(0)
  print(*result)

Submission status: Runtime Error

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

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

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

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

if __name__ == "__main__":
  dfs(0)
  print(*result)

Submission status: Runtime Error

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

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

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

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

if __name__ == "__main__:
  dfs(0)
  print(*result)

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

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

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

dfs(0)
print(*result)

Submission status: Runtime Error

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

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

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

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