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())
A=[list(map(int,input().split())) for i in range(N-1)]
B=[list(map(int,input().split())) for i in range(Q)]

#行先
C=[[]*i for i in range(N)]
#合計値
D=[0]*N
#足跡
E=[False]*N

for i in range(N-1):
  C[A[i][0]-1].append(A[i][1])
  C[A[i][1]-1].append(A[i][0])


def f(a,b):
  D[a-1]+=b
  E[a-1]=True
  x=len(C[a-1])
  if x>0:
    for k in C[a-1]:
      if a<k:
        f(k,b)

for l in range(Q):
  f(B[l][0],B[l][1])
  
  
print(*D)

Submission status: Runtime Error

Solution code:
r = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    r[p - 1] += x
stack = [0]
used = [False] * n    
while stack:
    i = stack.pop()
    used[i] = True
    for to in l[i]:
        if not used[to]:
            stack.append(to)
            r[to] += r[i]
for t_r in r:
    print(t_r)

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
A=[list(map(int,input().split())) for i in range(N-1)]
B=[list(map(int,input().split())) for i in range(Q)]
#行先
C=[[]*i for i in range(N)]
#合計値
D=[0]*N

for i in range(N-1):
  C[A[i][0]-1].append(A[i][1])

def f(a,b):
  D[a-1]+=b
  x=len(C[a-1])
  if x>0:
    for k in range(x):
      f(C[a-1][k],b)

for l in range(Q):
  f(B[l][0],B[l][1]) 
  
print(*D)

Submission status: Runtime Error

Solution code:
input = sys.stdin.readline
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
graph = [[] for _ in range(N+1)]
for ab in AB:
    graph[ab[0]].append(ab[1])
    graph[ab[1]].append(ab[0])
val = [0] * (N+1)
for px in PX:
    val[px[0]] += px[1]
parent = [0] * (N+1)
Q  = [1]
while Q:
    q = Q.pop()
    for c in graph[q]:
        if c == parent[q]:
            continue
        parent[c] = q
        Q.append(c)
        val[c] += val[q]
print(*val[1:])

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
graph = [[] for _ in range(N+1)]
for ab in AB:
    graph[ab[0]].append(ab[1])
    graph[ab[1]].append(ab[0])
val = [0] * (N+1)
for px in PX:
    val[px[0]] += px[1]
parent = [0] * (N+1)
Q  = [1]
while Q:
    q = Q.popleft()
    for c in graph[q]:
        if c == parent[q]:
            continue
        parent[c] = q
        Q.append(c)
        val[c] += val[q]
print(*val[1:])

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
value = dict()
for px in PX:
    value.setdefault(px[0],0)
    value[px[0]] += px[1]
tree = dict()
for ab in AB:
    tree.setdefault(ab[0],[])
    tree[ab[0]].append(ab[1])
from collections import deque
Q = deque([1])
while Q:
    q = Q.pop()
    tree.setdefault(q,[])
    for c in tree[q]:
        value.setdefault(c,0)
        value.setdefault(q,0)
        value[c] += value[q]
        Q.append(c)
ans = []
for n in range(N):
    ans.append(value[n+1])
print(*ans)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
value = dict()
for px in PX:
    value.setdefault(px[0],0)
    value[px[0]] += px[1]
tree = dict()
for ab in AB:
    tree.setdefault(ab[0],[])
    tree[ab[0]].append(ab[1])
from collections import deque
Q = deque([1])
while Q:
    q = Q.pop()
    tree.setdefault(q,[])
    for c in tree[q]:
        value.setdefault(c,0)
        value[c] += value[q]
        Q.append(c)
ans = []
for n in range(N):
    ans.append(value[n+1])
print(*ans)

Submission status: Runtime Error

Solution code:
from collections import deque

n, q = map(int, input().split())
t = {}
tp = {1: 0}

for i in range(n-1):
    a, b = map(int, input().split())
    if a in t.keys():
        t[a].append(b)
    else:
        t.setdefault(a, [b])
    tp.setdefault(b, 0)

#print(t)

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


#print(tp)

####
stack=deque()
stack.append(1)
visitted={}

while stack:
    cur = stack.popleft()
    visitted.setdefault(cur, 1)
    #print(stack)
    #print(cur)
    if cur not in t.keys():
        continue
    for v in t[cur]:
        if v in visitted.keys():
            continue
        stack.append(v)
        tp[v]=tp[v]+tp[cur]

print(*tp.values(), sep=' ')

Submission status: Runtime Error

Solution code:

from collections import deque

n, q = map(int, input().split())
t = {}
tp = {1: 0}

for i in range(n-1):
    a, b = map(int, input().split())
    if a in t.keys():
        t[a].append(b)
    else:
        t.setdefault(a, [b])
    tp.setdefault(b, 0)

#print(t)

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


#print(tp)

####
stack=deque()
stack.append(1)
visitted=[]

while stack:
    cur = stack.popleft()
    visitted.append(cur)
    #print(stack)
    #print(cur)
    if cur not in t.keys():
        continue
    for v in t[cur]:
        if v in visitted:
            continue
        stack.append(v)
        tp[v]=tp[v]+tp[cur]

print(*tp.values(), sep=' ')

Submission status: Runtime Error

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


def dfs(v, par):
    for nv in tree[v]:
        if nv != par:
            adds[nv] += adds[v]
            dfs(nv, v)


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

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

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

visited = [False] * N


def dfs(parent):
    visited[parent] = True
    for child in tree[parent]:
        if not visited[child]:
            counter[child] += counter[parent]
            dfs(child)


dfs(0)
print(' '.join(map(str, counter)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
l = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    l[a - 1].append(b - 1)
    l[b - 1].append(a - 1)
r = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    r[p - 1] += x
def dfs(v, p = -1):
    for to in l[v]:
        if to == p:
            continue
        r[to] += r[v]
        dfs(to, v)
dfs(0)
for t_r in r:
    print(t_r)

Submission status: Runtime Error

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

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

ans=[0 for _ in range(n+1)]
def dfs(vertex,pt):
    pt_v=pt+point[vertex]
    ans[vertex]=pt_v
    child_v=child[vertex]
    for i in range(len(child_v)):
        dfs(child_v[i],pt_v)


dfs(1,0)

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
n,q=map(int,input().split())
G=[[]*n for i in range(n)]
P=[0]*n
V=[0]*n
R=[0]*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)
for i in range(q):
	c,d=map(int,input().split())
	P[c-1]+=d

def dsf(i,p):
    V[i]=1
    R[i]+=p+P[i]
    p=0
    if G[i]!=[]:
        for j in G[i]:
            if V[j]!=1:
                dsf(j,R[i])
dsf(0,0)
print(' '.join(map(str,R)))

Submission status: Runtime Error

Solution code:
import sys
sys.recursionlimit(100000)
n,q=map(int,input().split())
G=[[]*n for i in range(n)]
P=[0]*n
V=[0]*n
R=[0]*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)
for i in range(q):
	c,d=map(int,input().split())
	P[c-1]+=d

print(G)
def dsf(i,p):
    V[i]=1
    R[i]+=p+P[i]
    p=0
    if G[i]!=[]:
        for j in G[i]:
            if V[j]!=1:
                dsf(j,R[i])
dsf(0,0)
print(' '.join(map(str,R)))

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
G=[[]*n for i in range(n)]
P=[0]*n
V=[0]*n
R=[0]*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)
for i in range(q):
	c,d=map(int,input().split())
	P[c-1]+=d

def dsf(i,p):
    V[i]=1
    R[i]+=p+P[i]
    p=0
    if G[i]!=[]:
        for j in G[i]:
            if V[j]!=1:
                dsf(j,R[i])
dsf(0,0)
print(' '.join(map(str,R)))

Submission status: Runtime Error

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

value=[0]*(n+1)

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

# print(value)
# print(graph)

def dfs(v,parent,add):
    value[v] += add
    # print(value)
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])

dfs(1,0,0)

ans=' '.join(map(str,value[1:]))
print(ans)

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

value=[0]*(n+1)

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

# print(value)
# print(graph)

def dfs(v,parent,add):
    value[v] += add
    # print(value)
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])

dfs(1,0,0)

ans=' '.join(map(str,value[1:]))
print(ans)

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,readline().split())
ABPX = list(map(int,read().split()))
AB = iter(ABPX[:N+N-2])
PX = iter(ABPX[N+N-2:])

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

val = [0] * (N+1)
for p,x in zip(PX,PX):
    val[p] += x

stack = [1]

parent = [0] * (N+1)
while stack:
    x = stack.pop()
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
        val[y] += val[x]

print(' '.join(map(ste,val[1:])))

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,readline().split())
ABPX = list(map(int,read().split()))
AB = iter(ABPX[:N+N-2])
PX = iter(ABPX[N+N-2:])

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

val = [0] * (N+1)
for p,x in zip(PX,PX):
    val[p] += x

stack = [1]

parent = [0] * (N+1)
while stack:
    x = stack.pop()
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
        val[y] += val[x]

print(' '.join(map(std,val[i:])))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <queue>
#include <vector>

std::vector<std::vector<int>> make_tree(const int n) {
  std::vector<std::vector<int>> tree(n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    std::cin >> a >> b;
    tree[a - 1].push_back(b - 1);
    tree[b - 1].push_back(a - 1);
  }
  return tree;
}

std::vector<int> make_counter(const int n, const int q) {
  std::vector<int> counter(n, 0);
  for (int i = 0; i < q; ++i) {
    int p, x;
    std::cin >> p >> x;
    counter[p - 1] += x;
  }
  return counter;
}

int main() {
  int n, q;
  std::cin >> n >> q;

  const auto tree = make_tree(n);
  auto counter = make_counter(n, q);

  std::queue<int> roots;
  roots.push(0);

  // 探索済みかどうかを記録する配列
  std::vector<bool> is_searched(n, false);

  while (!roots.empty()) {
    const auto root = roots.front();
    roots.pop();

    is_searched[root] = true;
    const auto c = counter[root];
    const auto& nodes = tree[root];

    for (auto&& node : nodes) {
      if (!is_searched[node]) {
        counter[node] += c;
        roots.push(node);
      }
    }
  }

  for (auto&& c : counter) std::cout << c << ' ';
  std::cout << std::flush;
}

Submission status: Runtime Error

Solution code:
from collections import deque

n, q = map(int, input().split())
C = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  C[a-1].append(b-1)
  C[b-1].append(a-1)
dist = [-1]*n
dist[0] = 1
A = [0]*n
F = [0]*n
for _ in range(q):
  p, x = map(int, input().split())
  F[p-1] += x
dq = deque()
dq.append(0)
while dq:
  v = dq.popleft()
  for nv in C[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      dq.append(nv)
def dfs(v):
  A[v] += F[v]
  for nv in C[v]:
    if dist[nv] > dist[v]:
      A[nv] += A[v]
      dfs(nv)
dfs(0)
print(*A)

Submission status: Runtime Error

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

ki = [[]for _ in range(N)]
for a,b in AB:
    a-=1
    b-=1
    ki[a].append(b)
    ki[b].append(a)
point = [0]*(N)
for p,x in PX:
    p-=1
    point[p]+=x
def dfs(now, last ,add):
    point[now]+= add
    for x in ki[now]:
        if (x == last):continue
        dfs(x,now,point[now])
dfs(0,0,0)
print(*point)

Submission status: Runtime Error

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

ki = [[]for _ in range(N+1)]
for a,b in AB:
    a-=1
    b-=1
    ki[a].append(b)
    ki[b].append(a)
point = [0]*N
for p,x in PX:
    p-=1
    point[p]+=x
def dfs(now, last ,add):
    point[now]+= add
    for x in ki[now]:
        if (x == last):continue
        dfs(x,now,point[now])
dfs(0,0,0)
print(*point)

Submission status: Runtime Error

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

ki = [[]for _ in range(N+1)]
for a,b in AB:
    a-=1
    b-=1
    ki[a].append(b)
    ki[b].append(a)
point = [0]*(N+1)
for p,x in PX:
    p-=1
    point[p]+=x
def dfs(now, last ,add):
    point[now]+= add
    for x in ki[now]:
        if (x == last):continue
        dfs(x,now,point[now])
dfs(0,0,0)
print(*point)

Submission status: Runtime Error

Solution code:
//部分木に含まれる頂点を全て求めてしまえばよい
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<utility>

using namespace std;

int main(){
  int n,q;cin >> n >> q;
  vector< vector<int> > u(n);
  for(int i=0;i<n-1;i++){
    int a,b;cin >> a >> b;
    u[a-1].push_back(b-1);
    u[b-1].push_back(a-1);
  }

  vector<int> c(n);
  for(int i=0;i<q;i++){
    int p,x;cin >> p >> x;c[p-1]=x;
  }
  //cout << 0 << endl;
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<u[i].size();j++){
      cout << u[i][j];
      if(j==u[i].size()-1) cout << endl;
      else cout << " ";
    }
  }
  */

  queue< pair<int,int> > k;k.push(make_pair(-1,0));
  while(k.size()!=0){
  //for(int z=0;z<10;z++){
    int l1=k.size();
    for(int i=0;i<l1;i++){
      pair<int,int> a=k.front();
      int l2=u[a.second].size();
      for(int j=0;j<l2;j++){
        if(u[a.second][j]!=a.first){
          c[u[a.second][j]]+=c[a.second];
          k.push(make_pair(a.second,u[a.second][j]));
        }
      }
      k.pop();
    }

  }
  for(int i=0;i<n;i++){
    cout << c[i];
    if(i==n-1) cout << endl;
    else cout << " ";
  }
}

Submission status: Runtime Error

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

def ki_add(p,x):
    ans[p] += x
    for i in ki[p]:
        ki_add(i,x)

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

Submission status: Runtime Error

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

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

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

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

counter = [0] * N

def process(root, value):
    counter[root] += value
    for next_root in edges[root]:
        process(next_root, value)

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

print(" ".join(counter))

Submission status: Runtime Error

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

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

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

counter = [0] * N

def process(root, value):
    counter[root] += value
    for next_root in edges[root]:
        process(next_root, value)

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

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

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readlin
    
    n,q=map(int,input().split())
    tree=[[] for _ in range(n)]
    for i in range(n-1):
        a,b=map(int,input().split())
        tree[a-1].append(b-1)
        tree[b-1].append(a-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        p[i],x[i] = map(int,input().split())
        
    px=[[] for i in range(n)]
    for pp,xx in zip(p,x):
        px[pp-1].append(xx)
    px=[sum(i) for i in px]
    c=[0]*n
    flag=[True]*n
     
    from collections import deque
    q = deque([(-1,0,0)])
    while q:
        parent,child,point = q.popleft()
        if parent!=-1:
            flag[parent]=False
        point+=px[child]
        c[child]=point
        for y in tree[child]:
            if y == parent:
                continue
            if flag[y]:
                q.append((child,y,point))
    print(*c)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
tree=[[] for _ in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
p=[0]*q
x=[0]*q
for i in range(q):
    p[i],x[i] = map(int,input().split())
    
px=[[] for i in range(n)]
for pp,xx in zip(p,x):
    px[pp-1].append(xx)
px=[sum(i) for i in px]
c=[0]*n
flag=[True]*n

from collections import deque
q = deque([(-1,0,0)])
while q:
    parent,child,point = q.popleft()
    if parent!=-1:
        flag[parent]=False
    point+=px[child]
    c[child]=point
    print(parent,child,point,tree[child],c)
    for y in tree[child]:
        if y == parent
            continue
        if flag[y]:
            q.append((child,y,point))
print(*c)

Submission status: Runtime Error

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

def dfs(x,node,edge,color):
    color[x]=1
    for i in edge[x]:
        if color[i]==0:
            node[i]+=node[x]
            dfs(i,node,edge,color)
    color[x]=2

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

node = [0]*N
edge = [[] for _ in range(N)]
color = [0]*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)

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

dfs(0,node,edge,color)

for n in node:
    print(n)

Submission status: Runtime Error

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

def dfs(x,node,edge):
    for i in edge[x]:
        node[i]+=node[x]
        dfs(i,node,edge)

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

node = [0]*N
edge = [[] for _ in range(N)]

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

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

dfs(0,node,edge)

for n in node:
    print(n)

Submission status: Runtime Error

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

def dfs(now, fro, point):
    now, fro, point = int(now), int(fro), int(point)
    point += c[now]
    ans[now] = point
    for i in range(len(g[now])):
        if g[now][i]==fro:
            continue
        dfs(g[now][i], now, point)


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

Submission status: Runtime Error

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

def dfs(now, fro, point):
    point += c[now]
    ans[now] = point
    for i in g[now]:
        if i==int(fro):
            continue
        dfs(i, now, point)

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

Submission status: Runtime Error

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

def dfs(now, fro, point):
    point += c[now]
    ans[now] = point
    for i in g[now]:
        if i==fro:
            continue
        dfs(i, now, point)

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

Submission status: Runtime Error

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


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


n,q=[int(_) for _ in input().split()]
A=[[] for i in range(n)]
node=[0]*n

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

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

calculate(0)
print(*node)

Submission status: Runtime Error

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

n,q=map(int,line().split())
A=[[] for i in range(n)]
node=[0]*n

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

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


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


calculate(0)
print(*node)

Submission status: Runtime Error

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

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

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

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


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


calculate(0)
print(*node)

Submission status: Runtime Error

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

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

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

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


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


calculate(0)
print(*node)

Submission status: Runtime Error

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

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

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


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


calculate(0)
print(*node)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for j in range(1, N)]

query_list = [input().split() for k in range(Q)]
query_list_int = [[int(k) for k in i] for i in query_list]

val_list = [0 for _ in range(N)]

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

for a, b in tree_list:
    a, b = int(a)-1, int(b) -1
    linked_node_list[a].append(b)
    linked_node_list[b].append(a)


for index, val in query_list_int:
    val_list[index-1] += val


def dfs(node_index, pairent_index):
    for child in linked_node_list[node_index]:
        
        if child != pairent_index:
            val_list[child] += val_list[node_index]

        dfs(child, node_index)

    return val_list


res = dfs(0, -1)

print(*res)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for j in range(1, N)]

query_list = [input().split() for k in range(Q)]
query_list_int = [[int(k) for k in i] for i in query_list]

val_list = [0 for _ in range(N)]

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

for a, b in tree_list:
    a, b = int(a)-1, int(b) -1
    linked_node_list[a].append(b)
    linked_node_list[b].append(a)


for index, val in query_list_int:
    val_list[index-1] += val


def dfs(node_index, pairent_index):
    for child in linked_node_list[node_index]:
        
        if child != pairent_index:
            val_list[child] += val_list[node_index]
            dfs(child, node_index)

    return val_list


res = dfs(0, -1)

print(*res)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for _ in range(1, N)]

query_list = [input().split() for _ in range(Q)]
query_list_int = [[int(k) for k in i] for i in query_list]

val_list = [0 for _ in range(N)]

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

for a, b in tree_list:
    linked_node_list[int(a) - 1].append(int(b) - 1)
    linked_node_list[int(b) - 1].append(int(a) - 1)


for index, val in query_list_int:
    val_list[index-1] += val


def dfs(node_index, pairent_index):
    for child in linked_node_list[node_index]:
        
        if child != pairent_index:
            val_list[child] += val_list[node_index]
            dfs(child, node_index)

    return val_list


res = dfs(0, -1)

print(*res)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for _ in range(1,N)]

query_list = [input().split() for _ in range(Q)]
query_list_int = [[int(k) for k in i]  for i in query_list]

val_list = [0 for _ in range(N)]
linkde_node_list = [[] for _ in range(N)]

for a, b in tree_list:
    linkde_node_list[int(a) -1].append(int(b) - 1)
    linkde_node_list[int(b) -1].append(int(a) - 1)


for index, val in query_list_int:
    val_list[index-1] += val

def dfs(node_index, pairent_index):

    for child in linkde_node_list[node_index]:

        if child == pairent_index:
            continue

        val_list[child] += val_list[node_index]
        dfs(child, node_index)

    return val_list

res = dfs(0, -1) 

print(*res)