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(10 ** 6)
"""
n, q = map(int, input().split())
cnt = [0] * n
tree = [[] for i 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)

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

def dfs(cur, par):
    for chi in tree[cur]:
        if chi == par:
            continue
        cnt[chi] += cnt[cur]
        dfs(chi, cur)
dfs(0, -1)
print(*cnt)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
cnt = [0] * n
tree = [[] for i 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)

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

def dfs(cur, par):
    for chi in tree[cur]:
        if chi == par:
            continue
        cnt[chi] += cnt[cur]
        dfs(chi, cur)
dfs(0, -1)
print(*cnt)

Submission status: Runtime Error

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

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

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

ans = [0] * N

def dfs(v, pv, w):
    w += weight[v]
    ans[v] = w
    for nv in edge[v]:
        if nv == pv: continue
        dfs(nv, v, w)

dfs(0, -1, 0)
print(*ans, sep=" ")

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i,s,n) for(int i=s;i<n;i++)
#define pb push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
using P=pair<int,int>;
using G=vector<vector<int>>;
using ll=long long;
ll N;
vector<ll> H, S;
vector<bool> seen;
vector<ll>sum;
vector<int> pl;

bool isYes(ll mid)
{
	vector<ll> count(N, 0);
	for (ll i = 0; i < N; ++i)
	{
		if (mid - H[i] < 0) return false;
		++count[min(N-1, (mid - H[i]) / S[i])];
	}
	for (ll i = 0; i < N-1; ++i)
		count[i+1] += count[i];
	for (ll i = 0; i < N; ++i)
		if (count[i] > i+1)
			return false;
	return true;
}
//普通の深さ優先探索seenを返す感じ
void dfs2(const G &G,int v){
    seen[v] = true; //v探索済み
	sum[v]+=pl[v];

    for(auto next_v :G[v]){
        if(seen[next_v])continue;//もう探索していた
		sum[next_v]+=sum[v];
        dfs2(G,next_v);
    }
}

void dfs(const G &G, int v, int p) {
	sum[v]+=pl[v];

    for (auto nv : G[v]) {
        if (nv == p) continue; // nv が親 p だったらダメ
		sum[nv]+=sum[v];
        dfs(G, nv, v); // v は新たに nv の親になります
    }
}

int main(int argc, char **argv){
	ll n,q; cin>>n>>q;
	G graph(n);
	sum.assign(n,0);
	pl.assign(n,0);

	rep(i,0,n-1){
		int a,b; cin>>a>>b;
		graph[a-1].push_back(b-1);
	}
	rep(i,0,q){
		int p,x; cin>>p>>x;
		pl[p-1]+=x;
	}

	seen.assign(n,false);
	rep(i,0,n){
		dfs2(graph,i);
	}

	rep(i,0,n){
		cout<<sum[i]<<" ";
	}

}

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline

N,Q=map(int,input().split())
a=[0]*(N-1)
b=[0]*(N-1)
p=[0]*Q
x=[0]*Q
c=[[] for _ in range(N)]  #接続リスト
cnt=[0]*N  #カウンター値

for i in range(N-1):
  a[i],b[i]=map(int,input().split())
  c[a[i]-1].append(b[i]-1)
  c[b[i]-1].append(a[i]-1)
for i in range(Q):
  p[i],x[i]=map(int,input().split())
  cnt[p[i]-1] += x[i]

def dfs(x,parent):
  if parent!=-1:
    cnt[x] += cnt[parent]
  for child in c[x]:
    if child!=parent:
      dfs(child,x)

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

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
a=[0]*(N-1)
b=[0]*(N-1)
p=[0]*Q
x=[0]*Q
c=[[] for _ in range(N)]  #接続リスト
cnt=[0]*N  #カウンター値

for i in range(N-1):
  a[i],b[i]=map(int,input().split())
  c[a[i]-1].append(b[i]-1)
  c[b[i]-1].append(a[i]-1)
for i in range(Q):
  p[i],x[i]=map(int,input().split())
  cnt[p[i]-1] += x[i]

def dfs(x,parent):
  if parent!=-1:
    cnt[x] += cnt[parent]
  for child in c[x]:
    if child!=parent:
      dfs(child,x)

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

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
a=[0]*(N-1)
b=[0]*(N-1)
p=[0]*Q
x=[0]*Q
c=[[] for _ in range(N)]  #接続リスト
cnt=[ 0 for _ in range(N)]  #カウンター値

for i in range(N-1):
  a[i],b[i]=map(int,input().split())
  c[a[i]-1].append(b[i]-1)
  c[b[i]-1].append(a[i]-1)
for i in range(Q):
  p[i],x[i]=map(int,input().split())
  cnt[p[i]-1] += x[i]

def dfs(x,parent):
  if parent!=-1:
    cnt[x] += cnt[parent]
  for child in c[x]:
    if child!=parent:
      dfs(child,x)

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

Submission status: Runtime Error

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

N, Q = list(map(int, input().split()))
graph = [[] for i in range(N)]
# 最初にroot pointを定めて:
# graph構造に大して点を与える...
for i in range(N-1):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

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

visited = [False] * N
tot_points = [0] * N
tot_points[0] = points[0]

nodes = graph[0]
visited[0] = True


def dfs(old_node, node):
    if visited[node]:
        return
    tot_points[node] = tot_points[old_node] + points[node]
    visited[node] = True
    nodes = graph[node]
    for _node in nodes:
        dfs(node, _node)
for _node in nodes:
    dfs(0, _node)

for x in tot_points.tolist():
    print(x, end=" ")

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
import queue
sys.setrecursionlimit(1000000000)

N, Q = list(map(int, input().split()))
graph = [[] for i in range(N)]
# 最初にroot pointを定めて:
# graph構造に大して点を与える...
for i in range(N-1):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

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

visited = np.zeros(N, dtype=bool)
tot_points = np.zeros(N, dtype=int)
tot_points[0] = points[0]
top_nodes = np.full(N, -1, dtype=int)

nodes = graph[0]
top_nodes[nodes] = 0
old_node = 0
visited[0] = True


def dfs(old_node, node):
    if visited[node]:
        return
    tot_points[node] = tot_points[old_node] + points[node]
    visited[node] = True
    nodes = queue.deque(graph[node])
    for _node in nodes:
        dfs(node, _node)
while _node in nodes:
    dfs(0, _node)
    

for x in tot_points.tolist():
    print(x, end=" ")

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
sys.setrecursionlimit(1000000000)

N, Q = list(map(int, input().split()))
graph = [[] for i in range(N)]
# 最初にroot pointを定めて:
# graph構造に大して点を与える...
for i in range(N-1):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

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

visited = np.zeros(N, dtype=bool)
tot_points = np.zeros(N, dtype=int)
tot_points[0] = points[0]
top_nodes = np.full(N, -1, dtype=int)

nodes = graph[0]
top_nodes[nodes] = 0
old_node = 0
visited[0] = True


def dfs(old_node, node):
    if visited[node]:
        return
    tot_points[node] = tot_points[old_node] + points[node]
    visited[node] = True
    nodes = graph[node]
    while len(nodes):
        dfs(node, nodes.pop())
while len(nodes):
    dfs(0, nodes.pop())
    

for x in tot_points.tolist():
    print(x, end=" ")

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

N, Q = list(map(int, input().split()))
graph = [[] for i in range(N)]
# 最初にroot pointを定めて:
# graph構造に大して点を与える...
for i in range(N-1):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)

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

visited = np.zeros(N, dtype=bool)
tot_points = np.zeros(N, dtype=int)
tot_points[0] = points[0]
top_nodes = np.full(N, -1, dtype=int)

nodes = graph[0]
top_nodes[nodes] = 0
old_node = 0
visited[0] = True


def dfs(old_node, node):
    if visited[node]:
        return
    tot_points[node] = tot_points[old_node] + points[node]
    visited[node] = True
    nodes = graph[node]
    while len(nodes):
        dfs(node, nodes.pop())
while len(nodes):
    dfs(0, nodes.pop())
    

for x in tot_points.tolist():
    print(x, end=" ")

Submission status: Runtime Error

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

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

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

def dfs(now, prev=-1):
    for next in g[now]:
        if next != prev:
            c[next] += c[now]
            dfs(next, now)

dfs(0)
print(*c)

Submission status: Runtime Error

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

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

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

def dfs(now, prev=-1):
    for next in g[now]:
        if next != prev:
            c[next] += c[now]
            dfs(next, now)

dfs(0)
print(*c)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
graph=[[] for _ in range(n+1)]
ab=[list(map(int,input().split())) for _ in range(n-1)]
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
px=[list(map(int,input().split())) for _ in range(q)]
ans=[0]*(n+1)
for p,x in px:
    ans[p]+=x
seen=[0]*(n+1)
def dfs(i):
    seen[i]=1
    for li in graph[i]:
        if seen[li]==0:
            ans[li]+=ans[i]
            dfs(li)
dfs(1)
print(' '.join(map(str,ans[1:])))

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
graph=[[] for _ in range(n+1)]
ab=[list(map(int,input().split())) for _ in range(n-1)]
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
px=[list(map(int,input().split())) for _ in range(q)]
ans=[0]*(n+1)
for p,x in px:
    ans[p]+=x
seen=[0]*(n+1)
def dfs(i):
    seen[i]=1
    l=graph[i]
    for li in l:
        if seen[li]==0:
            ans[li]+=ans[i]
            todo.append(li)
    if len(todo)>0:
        dfs(todo.pop())
todo=[1]
dfs(todo.pop())
print(' '.join(map(str,ans[1:])))

Submission status: Runtime Error

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

input=sys.stdin.readline

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)

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

result=[0]*n

def counter(chouten,count):
    global result,point,tree
    count+=point[chouten]
    result[chouten]=count

    #print(chouten,result)

    for item in tree[chouten]:
        if result[item]==0:
            counter(item,count)

counter(0,0)

result=map(str,result)
print(" ".join(result))

Submission status: Runtime Error

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

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

result=[0]*n

def counter(chouten,count):
    global result,point,tree
    count+=point[chouten]
    result[chouten]=count

    #print(chouten,result)

    for item in tree[chouten]:
        if result[item]==0:
            counter(item,count)

counter(0,0)

result=map(str,result)
print(" ".join(result))

Submission status: Runtime Error

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

def dfs(u,par):
  ans_list[u]+=ans_list[par]  
  for v in tree_list[u]:
    if v!=par:
      dfs(v,u)      
  return

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

tree_list=[[] for _ in range(N+1)]
for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  tree_list[b].append(a)
#print(tree_list)

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

dfs(1,0,0)

print(*ans_list[1:])

Submission status: Runtime Error

Solution code:
    import sys
    sys.setrecursionlimit(10**6)
    n, q = map(int, input().split())
    node = [0] * n
    par = [-1] * n
    for _ in range(n-1):
        a, b = map(int, input().split())
        par[b-1] = a-1
    for _ in range(q):
        p, x = map(int, input().split())
        node[p-1]+=x
    def dfs(i, count=0):
        count+=node[i]
        if par[i] == -1:
            return count
        else:
            return dfs(par[i], count)
    print(' '.join([str(dfs(i)) for i in range(n)]))

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

node = [0] * n
par = [-1] * n
for a, b in ab:
    par[b-1] = a-1
for p, x in px:
    node[p-1]+=x
def dfs(i, count=0):
    count+=node[i]
    if par[i] == -1:
        return count
    else:
        return dfs(par[i], count)
print(' '.join([str(dfs(i)) for i in range(n)]))

Submission status: Runtime Error

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

G=[[0]*N]
ans =[0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    G[a-1] = b-1
    G[b-1] = a-1

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

print(*ans, sep=" ")

Submission status: Runtime Error

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

N, Q = map(int, input().split())
to = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
ans = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import sys
sys.setlimitrecursion(100000000)
def main():
    #入力
    N, Q = (int(_) for _ in input().split())
    edge = [[int(_) for _ in input().split()] for __ in range(N-1)]
    query = [[int(_) for _ in input().split()] for __ in range(Q)]

    tree = defaultdict(list)
    for a, b in edge:
        tree[a].append(b)
        tree[b].append(a)

    cc = [0] * (N+1)
    seen = [False] * (N+1)

    for p, x in query:
        cc[p] += x

    def dfs(x):
        for p in tree[x]:
            if seen[p] == False:
                seen[p] = True
                cc[p] += cc[x]
                dfs(p)
        return

    seen[1] = True
    dfs(1)

    print(*cc[1:])
    return

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict
def main():
    #入力
    N, Q = (int(_) for _ in input().split())
    edge = [[int(_) for _ in input().split()] for __ in range(N-1)]
    query = [[int(_) for _ in input().split()] for __ in range(Q)]

    tree = defaultdict(list)
    for a, b in edge:
        tree[a].append(b)
        tree[b].append(a)

    cc = [0] * (N+1)
    seen = [False] * (N+1)

    for p, x in query:
        cc[p] += x

    def dfs(x):
        for p in tree[x]:
            if seen[p] == False:
                seen[p] = True
                cc[p] += cc[x]
                dfs(p)
        return

    seen[1] = True
    dfs(1)

    print(*cc[1:])
    return

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

n, q = map(int, input().split())
edges = [[] for _ in range(n)]

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

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


def dfs(v, parent):
    for v2 in edges[v]:
        if v2 == parent:
            continue
        count[v2] += count[v]
        dfs(v2, v)


dfs(0, None)
print(*count)

Submission status: Runtime Error

Solution code:
#ABC138D
#node:節 edge:枝
def main():
    import sys
    N, Q = map(int, sys.stdin.readline().split())
    edge = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N-1)]
    #1:ノードのリストを作る
    nodeL = [[] for _ in range(N)]
    for a, b in edge:
        nodeL[a-1].append(b-1)
        nodeL[b-1].append(a-1)
    #print(nodeL)
    px = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    #print(px)
    score = [0 for i in range(N)]
    for p, x in px:
        score[p-1] +=x
    #print(score)
    result = score.copy()
    
    def dfs(p, g=-1):
        for c in nodeL[p]:
            if c !=g:
                score[c] +=score[p]
                dfs(c, p)
    
    
    dfs(0)
    print(*score)
    


if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
#ABC138D
#node:節 edge:枝
def main():
    import sys
    N, Q = map(int, sys.stdin.readline().split())
    edge = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N-1)]
    #1:ノードのリストを作る
    nodeL = [[] for _ in range(N)]
    for a, b in edge:
        nodeL[a-1].append(b-1)
        nodeL[b-1].append(a-1)
    #print(nodeL)
    px = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    #print(px)
    score = [0 for i in range(N)]
    for p, x in px:
        score[p-1] +=x
    #print(score)
    result = score.copy()
    
    def dfs(p, g=-1):
        for c in nodeL[p]:
            if c !=g:
                score[c] +=score[p]
                dfs(c, p)
    
    
    dfs(0)
    print(score)
    


if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
def dfs(no, fr):
    for t in to[no]:
        if t == fr:
            continue
        ans[t] += ans[no]
        dfs(t, no)


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

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
cnt = [0 for i in range(N)]
val = [0 for i in range(N)]
t = [[] for i in range(N)]
seen = [False for i in range(N)]

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

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

points = 0
#dfsでいけなくなったらそのノードの点を引く
def dfs(u):
    global points
    points += val[u]
    cnt[u] = points
    for v in t[u]:
        if seen[v]:
            continue
        seen[v] = 1
        dfs(v)
    points -= val[u]

seen[0] = 1
dfs(0)
for i in range(N):
    print(cnt[i], end=" ")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)

class Node:
    def __init__(self, num):
        self.num = num
        self.ct = 0
        self.temp_ct = 0
        self.edge = []
    
    def set_temp_ct(self, ct_):
        self.temp_ct += ct_
    
    def set_ct(self, ct):
        self.ct = self.temp_ct + ct
        return self.ct

    def set_edge(self, e):
        self.edge.append(e)

    def pop_edge(self, e):
        if e in self.edge:
            self.edge.remove(e)



def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    nodes = [Node(num+1) for num in range(N)]
    for qa, qb in zip(a,b):
        nodes[qa-1].set_edge(qb)
        nodes[qb-1].set_edge(qa)

    for qp, qx in zip(p, x):
        nodes[qp-1].set_temp_ct(qx)

    def d_node(num, pre_ct):
        pre_ct = nodes[num-1].set_ct(pre_ct)
        for ls in nodes[num-1].edge:
            nodes[ls-1].pop_edge(num)
            d_node(ls, pre_ct)
    
    d_node(1, 0)

    a = [nodes[x].ct for x in range(N)]
    print(*a)


    return


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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
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)
  
add=[0]*n

for i in range(q):
  p,x=map(int,input().split())
  add[p-1]+=x
  
ans=[0]*n
def dfs(v,p,value):
  value+=add[v]
  ans[v]=value
  for j in g[v]: 
    if j==p: continue
    dfs(j,v,value)
      
      
dfs(0,-1,0)

print(*ans)

Submission status: Runtime Error

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

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

visited = [0]*n


def dfs(parent):
    visited[parent] = 1
    for child in edges[parent]:
        if visited[child] == 1:
            continue
        else:
            counter[child] += counter[parent]
            dfs(child)


dfs(0)
print(*counter)

Submission status: Runtime Error

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

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

visited = [0]*n


def dfs(parent):
    visited[parent] = 1
    for child in edges[parent]:
        if visited[child] == 1:
            continue
        else:
            counter[child] += counter[parent]
            dfs(child)


dfs(0)
print(*counter)

Submission status: Runtime Error

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

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

visited = [0]*n


def dfs(parent):
    visited[parent] = 1
    for child in edges[parent]:
        if visited[child] == 1:
            continue
        else:
            counter[child] += counter[parent]
            dfs(child)

        
dfs(0)
print(counter)

Submission status: Runtime Error

Solution code:
mport sys
sys.setrecursionlimit(10**9)

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

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

ans=[0]*N

def px(p,x):
  for i in link[p]:
    ans[i]+=x
    px(i,x)

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

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

link=[[] for _ in range(N)]
for i in range(N-1):
  a=AB[i][0]-1
  b=AB[i][1]-1
  link[a].append(b)

ans=[0]*N

def px(p,x):
  for i in link[p]:
    ans[i]+=x
    px(i,x)

for i in range(Q):
  p=PX[i][0]-1
  x=PX[i][1]
  ans[p]+=x
  px(p,x)

print(*ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque

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

parent_child = defaultdict(list)

AB_lst = [list(map(int, input().split())) for _ in range(N - 1)]
AB_lst.sort(key=lambda x: (x[0], x[1]))

for a, b in AB_lst:
    parent_child[a].append(b)

query_weight = defaultdict(int)
for _ in range(Q):
    q, x = map(int, input().split())
    query_weight[q] += x


dq = deque([])
dq.append((1, query_weight[1]))

ans = [0] * N
while dq:
    node, w = dq.popleft()
    children = parent_child[node]

    ans[node - 1] = str(w)

    for c in children:
        dq.append((c, w + query_weight[c]))


print(' '.join(ans))

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque

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

parent_child = defaultdict(set)

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

query_weight = defaultdict(int)
for _ in range(Q):
    q, x = map(int, input().split())
    query_weight[q] += x


dq = deque([])
dq.append((1, query_weight[1]))

ans = [0] * N
while dq:
    node, w = dq.popleft()
    children = parent_child[node]

    ans[node - 1] = str(w)

    for c in children:
        dq.append((c, w + query_weight[c]))


print(' '.join(ans))

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
is_have_parent = [False]*(N+1)
unknown_pair = []
tree = {}
count = {}
depth = {}

is_have_parent[1] = True
depth[1] = 1

for i in range(1,N+1):
  tree[i] =[]
  count[i] = 0
  
  
for i in range(N-1):
  a,b = map(int,input().split())
  if not (is_have_parent[a] == True or is_have_parent[b] == True):
    unknown_pair.append((a,b))
    continue
  if is_have_parent[a] == False:
    tmp = a
    a = b
    b = tmp
  depth[b] = depth[a]+1 
  is_have_parent[b] = True  
  tree[a].append(b)

for pair in unknown_pair:
  a,b = pair[0],pair[1]
  if is_have_parent[a] == True:
    depth[b] = depth[a]+1 
    is_have_parent[b] = True  
    tree[a].append(b)
  else:
    depth[a] = depth[b]+1 
    is_have_parent[a] = True  
    tree[b].append(a)

for i in range(Q):
  p,x = map(int,input().split())
  count[p] += x
depth = sorted(depth.items(), key=lambda x: x[1])
depth = list(zip(*depth))[0]
for i in depth:
  for j in range(len(tree[i])): 
    count[tree[i][j]] += count[i]
    
print(" ".join(map(str,count.values())))

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
is_have_parent = [False]*(N+1)
unknown_pair = []
tree = {}
count = {}
depth = {}

is_have_parent[1] = True
depth[1] = 1

for i in range(1,N+1):
  tree[i] =[]
  count[i] = 0
  
  
for i in range(N-1):
  a,b = map(int,input().split())
  if not (is_have_parent[a] == True or is_have_parent[b] == True):
    unknown_pair.append((a,b))
    continue
  print(a,b)
  if is_have_parent[a] == False:
    tmp = a
    a = b
    b = tmp
  depth[b] = depth[a]+1 
  is_have_parent[b] = True  
  tree[a].append(b)

for pair in unknown_pair:
  a,b = pair[0],pair[1]
  if is_have_parent[a] == True:
    depth[b] = depth[a]+1 
    is_have_parent[b] = True  
    tree[a].append(b)
  else:
    depth[a] = depth[b]+1 
    is_have_parent[a] = True  
    tree[b].append(a)

for i in range(Q):
  p,x = map(int,input().split())
  count[p] += x
print(tree)
depth = sorted(depth.items(), key=lambda x: x[1])
depth = list(zip(*depth))[0]
for i in depth:
  for j in range(len(tree[i])): 
    count[tree[i][j]] += count[i]
    
print(" ".join(map(str,count.values())))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)

n, q = map(int, input().split())
node_dict = {}
for i in range(n-1):
    ai, bi = map(int, input().split())
    if not ai in node_dict.keys():
        node_dict[ai] = [bi]
    else:
        node_dict[ai].append(bi)

res_dict = {}
for j in range(q):
    pj, xj = map(int, input().split())
    if not pj in res_dict.keys():
        res_dict[pj] = xj
    else:
        res_dict[pj] += xj

def add_root(root):
    if not root in node_dict.keys():
        return 0
    else:
        for node in node_dict[root]:
            if not node in res_dict.keys():
                res_dict[node] = res_dict[root]
            else:
                res_dict[node] += res_dict[root]
            add_root(node)

add_root(1)

res_list = []
for k in range(1, n+1):
    if not k in res_dict.keys():
        res_list.append(0)
    else:
        res_list.append(res_dict[k])
print(' '.join([str(x) for x in res_list]))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
node_dict = {}
for i in range(n-1):
    ai, bi = map(int, input().split())
    if not ai in node_dict.keys():
        node_dict[ai] = [bi]
    else:
        node_dict[ai].append(bi)

def add_x(p, x):
    if not p in res_dict.keys():
        res_dict[p] = x
    else:
        res_dict[p] += x

    if not p in node_dict.keys():
        return 0
    else:
        for node in node_dict[p]:
            add_x(node, x)

res_dict = {}
for j in range(q):
    pj, xj = map(int, input().split())
    add_x(pj, xj)

res_list = []
for k in range(1, n+1):
    if k in res_dict.keys():
        res_list.append(res_dict[k])
    else:
        res_list.append(0)
print(' '.join([str(x) for x in res_list]))

Submission status: Runtime Error

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

def dfs(v,x):
    c[v] += x
    for i in g[v]:
        dfs(i,x)

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

print(*c)