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: Wrong Answer

Solution code:
import math
import sys
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)


n,q = map(int,input().split())
edges = []
for i in range(n-1):
	edges.append(list(map(int,input().split())))
for i in range(len(edges)):
	edges[i][0] -= 1
	edges[i][1] -= 1

operations = []
for i in range(q):
	operations.append(list(map(int,input().split())))
for i,e in enumerate(operations):
	operations[i][0] -= 1


leafs = [[] for i in range(n)]

for edge in edges:
	leafs[edge[0]].append(edge[1])


values = [0 for i in range(n)]

def dfs(pos):
	reward_edges.append(pos)
	if len(leafs[pos]) == 0:
		pass
	else:
		for e in leafs[pos]:
			dfs(e)


for operation in operations:
	reward_edges = []
	dfs(operation[0])
	for reward_edge in set(reward_edges):
		values[reward_edge] += operation[1]
ans = ''
for e in values:
	ans += str(e) + ' '
print(ans[:-1])

Submission status: Wrong Answer

Solution code:
import math
import sys
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)


n,q = map(int,input().split())
edges = []
for i in range(n-1):
	edges.append(list(map(int,input().split())))
for i in range(len(edges)):
	edges[i][0] -= 1
	edges[i][1] -= 1

operations = []
for i in range(q):
	operations.append(list(map(int,input().split())))
for i,e in enumerate(operations):
	operations[i][0] -= 1


leafs = [[] for i in range(n)]

for edge in edges:
	leafs[edge[0]].append(edge[1])


values = [0 for i in range(n)]

def dfs(pos):
	reward_edges.append(pos)
	if len(leafs[pos]) == 0:
		pass
	else:
		for e in leafs[pos]:
			dfs(e)


for operation in operations:
	reward_edges = []
	dfs(operation[0])
	for reward_edge in reward_edges:
		values[reward_edge] += operation[1]
ans = ''
for e in values:
	ans += str(e) + ' '
print(ans[:-1])

Submission status: Wrong Answer

Solution code:
# ki
from collections import deque

# input

n, q = map(int, input().split())
ln = [list(map(int, input().split())) for i in range(n-1)]
lq = [list(map(int, input().split())) for i in range(q)]

edge = [[] for i in range(n+1)]

for next in ln:
    parent = next[0]
    child = next[1]
    edge[parent].append(child)


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

for q in lq:
    score[q[0]] += q[1]

que = deque()
que.append(1)

while que:
    node = que.popleft()
    if not visited[node]:
        visited[node] = True
        for next in edge[node]:
            if not visited[next]:
                score[next] += score[node]
                que.append(next)

print(" ".join([str(n) for n in score][1:]))

Submission status: Wrong Answer

Solution code:
# ki
from collections import deque

# input

n, q = map(int, input().split())
ln = [list(map(int, input().split())) for i in range(n-1)]
lq = [list(map(int, input().split())) for i in range(q)]

edge = [[] for i in range(n+1)]

for next in ln:
    parent = next[0]
    child = next[1]
    edge[parent].append(child)
    edge[child].append(parent)
    print(next)
    print(edge)

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

for q in lq:
    score[q[0]] += q[1]

que = deque()
que.append(1)

while que:
    node = que.popleft()
    if not visited[node]:
        visited[node] = True
        for next in edge[node]:
            if not visited[next]:
                score[next] += score[node]
                que.append(next)

print(" ".join([str(n) for n in score][1:]))

Submission status: Wrong Answer

Solution code:
while True:
    try:
        N, Q = map(int, input().split())
        verts = [None] * N
        parents = [None] * N
        qs = [0] * N
        results = [0] * N
        for i in range(N - 1):
            a, b = map(int, input().split())
            a -= 1
            b -= 1
            if verts[a] is None:
                verts[a] = list()
            verts[a].append(b)
            parents[b] = a
        for i in range(Q):
            q, x = map(int, input().split())
            q -= 1
            qs[q] += x
        rests = [0]
        while rests:
            x = rests.pop()
            if parents[x] is not None:
                results[x] += results[parents[x]]
            results[x] += qs[x]
            if verts[x] is not None:
                rests.extend(verts[x])

        print(" ".join(map(str, results)))
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
# ki
from collections import deque

# input

n, q = map(int, input().split())
ln = [list(map(int, input().split())) for i in range(n-1)]
lq = [list(map(int, input().split())) for i in range(q)]

edge = [[] for i in range(n+1)]

for next in ln:
    parent = next[0]
    child = next[1]
    edge[parent].append(child)

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

for q in lq:
    score[q[0]] += q[1]

que = deque()
que.append(1)

while que:
    node = que.popleft()
    if not visited[node]:
        visited[node] = True
        for next in edge[node]:
            if not visited[next]:
                score[next] += score[node]
                que.append(next)

print(" ".join([str(n) for n in score][1:]))

Submission status: Wrong Answer

Solution code:
# ki
from collections import deque

# input

n, q = map(int, input().split())
ln = sorted([list(map(int, input().split())) for i in range(n-1)], key=lambda x: x[0])
lq = [list(map(int, input().split())) for i in range(q)]

score = [0]*(n+1)

for q in lq:
    score[q[0]] += q[1]

que = deque()
que.append(1)

while len(que) > 0:
    cn = que.popleft()

    for n in ln:
        if n[0] == cn:
            score[n[1]] += score[cn]
            que.append(n[1])

print(" ".join([str(n) for n in score][1:]))

Submission status: Wrong Answer

Solution code:
# ki

# input

n, q = map(int, input().split())
ln = sorted([list(map(int, input().split())) for i in range(n-1)], key=lambda x: x[0])
lq = [list(map(int, input().split())) for i in range(q)]

score = [0]*(n+1)

for q in lq:
    score[q[0]] += q[1]

que = []
que.append(1)

while len(que) > 0:
    cn = que[0]
    que = que[1:]

    for n in ln:
        if n[0] == cn:
            score[n[1]] += score[cn]
            que.append(n[1])

print(" ".join([str(n) for n in score][1:]))

Submission status: Wrong Answer

Solution code:
while True:
    try:
        N, Q = map(int, input().split())
        verts = [None] * N
        parents = [None] * N
        qs = [0] * N
        results = [0] * N
        for i in range(N - 1):
            a, b = map(int, input().split())
            a -= 1
            b -= 1
            if verts[a] is None:
                verts[a] = list()
            verts[a].append(b)
            parents[b] = a
        for i in range(Q):
            q, x = map(int, input().split())
            q -= 1
            qs[q] += x
        rests = [0]
        while rests:
            x = rests.pop(0)
            if parents[x] is not None:
                results[x] += results[parents[x]]
            results[x] += qs[x]
            if verts[x] is not None:
                rests.extend(verts[x])

        print(" ".join(map(str, results)))
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
# ki

# input

n, q = map(int, input().split())
ln = sorted([list(map(int, input().split())) for i in range(n-1)], key=lambda x: x[0])
lq = [list(map(int, input().split())) for i in range(q)]

score = [0]*(n+1)

for q in lq:
    score[q[0]] += q[1]

for n in ln:
    root = n[0]
    leaf = n[1]

    score[leaf] += score[root]

print(" ".join([str(n) for n in score][1:]))

Submission status: Wrong Answer

Solution code:
# ki

# input

n, q = map(int, input().split())
ln = [list(map(int, input().split())) for i in range(n-1)]
lq = sorted([list(map(int, input().split())) for i in range(q)], key=lambda x: x[0])

score = [0]*(n+1)

for q in lq:
    score[q[0]] += q[1]

for n in ln:
    root = n[0]
    leaf = n[1]

    score[leaf] += score[root]

print(" ".join([str(n) for n in score][1:]))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
AB = [[0,0]] * (N-1)
node_list = []
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
    node_list.append(AB[n][0])
    node_list.append(AB[n][1])
PX = [[0,0]] * Q
for q in range(Q):
    PX[q] = list(map(int,input().split()))
node_list = sorted(list(set(node_list)))
tree = {}
for node in node_list:
    tree[node]={"score":0,"children":[]}
for n in range(N-1):
    tree[AB[n][0]]["children"].append(AB[n][1])
for q in range(Q):
    tree[PX[q][0]]["score"] += PX[q][1]
def calc_score(t,n):
    for child in t[n]["children"]:
        t[child]["score"] += t[n]["score"]
        calc_score(t,child)
    return t
ans = calc_score(tree,node_list[0])
ans2 = []
for node in node_list:
    ans2.append(ans[node]["score"])
print(*ans2)

Submission status: Wrong Answer

Solution code:
while True:
    try:
        N, Q = map(int, input().split())
        verts = [None] * N
        results = [0] * N
        for i in range(N - 1):
            a, b = map(int, input().split())
            if a == 1:
                continue
            a -= 1
            if verts[a] is None:
                verts[a] = list()
            verts[a].append(b)
        for i in range(Q):
            q, x = map(int, input().split())
            q-= 1
            results[q] += x
            if q == 0:
                for i in range(1, N):
                    results[i] += x
            elif verts[q] is not None:
                rests = verts[q][:]
                while rests:
                    i = rests.pop(0)
                    results[i-1] += x
                    if verts[i-1] is not None:
                        rests.extend(verts[i-1])
        print(" ".join(map(str, results)))
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
from collections import deque


def main():

    setrecursionlimit(2000001)

    n, q = [int(x) for x in stdin.readline().split()]

    tree = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        a, b = [int(x) for x in stdin.readline().split()]
        tree[a].append(b)
        tree[b].append(a)

    count = [0] * (n + 1)
    for _ in range(q):
        parent, x = [int(x) for x in stdin.readline().split()]
        count[parent] += x

    queue = deque([])
    visited = [False] * (n + 1)

    queue.append((0, 1))

    while len(queue) > 0:
        parent, current = queue.popleft()
        print(current)
        visited[current] = True
        count[current] += count[parent]
        for child in tree[current]:
            if not visited[child]:
                queue.append((current, child))

    print(*count[1:])


main()

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
parent=[0]*N
ans=[0]*(N+1)
for i in range(N-1):
    a,b=map(int,input().split())
    parent[b-1]=a
for i in range(Q):
    p,x=map(int,input().split())
    ans[p-1]+=x
for i in range(N):
    ans[i]+=ans[parent[i]-1]
    print(ans[i],end=" ")

Submission status: Wrong Answer

Solution code:

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

from collections import defaultdict

N, Q = map(int, input().split())
counts = [0] * N

# これだと N がでかすぎる時に TLE になるので、入ってきた値だけの配列を作る
#graph = [ [0 for _ in range(N)] for _ in range(N) ]
def get_graph(N):
    tmp = [False] * N
    graph = [ [] for _ in range(1) for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())

        # 1 - 3 - 2 みたいなケース
        if tmp[b-1]:
            graph[b-1].append(a-1)
            tmp[b-1] = True
        else:
            graph[a-1].append(b-1)
            tmp[b-1] = True

    return graph


graph = get_graph(N)
#print(graph)

# 指示はまとめる
dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x


def inc(p, x, graph):
    # 0 index
    counts[p] += x

    for c in graph[p]:
        inc(c, x, graph)


for P, X in dd.items():
    P -= 1
    inc(P, X, graph)

print(*counts)

Submission status: Wrong Answer

Solution code:

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

from collections import defaultdict

N, Q = map(int, input().split())
counts = [0] * N

# これだと N がでかすぎる時に TLE になるので、入ってきた値だけの配列を作る
#graph = [ [0 for _ in range(N)] for _ in range(N) ]
def get_graph(N):
    tmp = [False] * N
    graph = [ [] for _ in range(1) for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())

        # 1 - 3 - 2 みたいなケース
        if tmp[b-1]:
            graph[b-1].append(a-1)
            tmp[a-1] = True
        else:
            graph[a-1].append(b-1)
            tmp[b-1] = True

    return graph


graph = get_graph(N)
#print(graph)

# 指示はまとめる
dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x


def inc(p, x, graph):
    # 0 index
    counts[p] += x

    for c in graph[p]:
        inc(c, x, graph)


for P, X in dd.items():
    P -= 1
    inc(P, X, graph)

print(*counts)

Submission status: Wrong Answer

Solution code:


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

from collections import defaultdict

N, Q = map(int, input().split())
counts = [0] * N

# これだと N がでかすぎる時に TLE になるので、入ってきた値だけの配列を作る
#graph = [ [0 for _ in range(N)] for _ in range(N) ]
def get_graph(N):
    tmp = [False] * N
    graph = [ [] for _ in range(1) for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())

        # 1 - 3 - 2 みたいなケース
        if tmp[b-1]:
            graph[b-1].append(a-1)
        else:
            graph[a-1].append(b-1)
            tmp[b-1] = True

    return graph


graph = get_graph(N)
#print(graph)

# 指示はまとめる
dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x


def inc(p, x, graph):
    # 0 index
    counts[p] += x

    for c in graph[p]:
        inc(c, x, graph)


for P, X in dd.items():
    P -= 1
    inc(P, X, graph)

print(*counts)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
tree = [[1,0,0]] # 0=number, 1=parent, 2=count
for i in range(n-1):
	ai,bi = map(int,input().split())
	tree.append([bi,ai,0])

for i in range(q):
	pi,xi = map(int,input().split())
	check = [pi]
	for i in range(n):
		if tree[i][0] in check:
			tree[i][2] += xi
		if tree[i][1] in check:
			tree[i][2] += xi
			check.append(tree[i][0])
		#print("===== check =====")
		#print(check)
	#print("===== tree =====")
	#print(tree)

tree.sort()
for i in range(n):
	if i != n-1:
		print(tree[i][2], end=" ")
	else:
		print(tree[i][2], end="\n")

Submission status: Wrong Answer

Solution code:

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

from collections import defaultdict

N, Q = map(int, input().split())
counts = [0] * N

# これだと N がでかすぎる時に TLE になるので、入ってきた値だけの配列を作る
#graph = [ [0 for _ in range(N)] for _ in range(N) ]
def get_graph(N):
    tmp = [ [] for _ in range(1) for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())

        # 1 - 3 - 2 みたいなケース
        if len(tmp[b-1]) > 0:
            tmp[b-1].append(a-1)
        else:
            tmp[a-1].append(b-1)

    return tmp


graph = get_graph(N)
#print(graph)

# 指示はまとめる
dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x


def inc(p, x, graph):
    # 0 index
    counts[p] += x

    for c in graph[p]:
        inc(c, x, graph)


for P, X in dd.items():
    P -= 1
    inc(P, X, graph)

print(*counts)

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter

N, Q = map(int,input().split())
tree_list = [list(map(int, input().split())) for i in range(N-1)]
counter = [0] * N

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

#sort
tree_list.sort(key=itemgetter(0))

for pa, ch in tree_list:
    counter[ch-1] += counter[pa-1]

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

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce
from operator import itemgetter

N, Q = map(int,input().split())
tree_list = [list(map(int, input().split())) for i in range(N-1)]
counter = [0] * N

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

#sort
tree_list.sort(key=itemgetter(0))
for pa, ch in tree_list:
    counter[ch-1] += counter[pa-1]

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

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce
from operator import itemgetter

N, Q = map(int,input().split())
tree_list = [list(map(int, input().split())) for i in range(N-1)]
counter = [0] * N

for i in range(Q):
    node ,inc = input().split()
    node = int(node)
    counter[node-1] = int(inc)

#sort
tree_list.sort(key=itemgetter(0))
for pa, ch in tree_list:
    counter[ch-1] += counter[pa-1]

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

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce

N, Q = map(int,input().split())
tree_list = [list(map(int, input().split())) for i in range(N-1)]
counter = [0] * N

for i in range(Q):
    node ,inc = list(map(int, input().split()))
    counter[node-1] = inc

for pa, ch in tree_list:

    counter[ch-1] += counter[pa-1]

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

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce

N, Q = map(int,input().split())
tree_list = [list(map(int, input().split())) for i in range(N-1)]
counter = [0] * len(set(reduce(lambda x,y: x+y, tree_list)))

for i in range(Q):
    node ,inc = list(map(int, input().split()))
    counter[node-1] = inc

for pa, ch in tree_list:

    counter[ch-1] += counter[pa-1]

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

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from functools import reduce

N, Q = map(int,input().split())
tree_list = [list(map(int, input().split())) for i in range(N-1)]
counter = [0] * len(set(reduce(lambda x,y: x+y, tree_list)))

for i in range(Q):
    node ,inc = list(map(int, input().split()))
    counter[node-1] = inc

for pa, ch in tree_list:

    counter[ch-1] += counter[pa-1]

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from itertools import *
import sys
import time


#################### library ####################


#################### cNode ####################

class cNode:
	def __init__(self, i):
		self.i = i
		self.counter = 0
		self.children = []
	
	def add(self, n):
		self.counter += n
	
	def walk(self, n):
		yield (self.i, self.counter + n)
		for child in self.children:
			for a in child.walk(self.counter + n):
				yield a


#################### naive ####################

def make_tree_naive(edges):
	tree = [ None ] + map(cNode, range(len(edges)+1))
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node2 = tree[v2]
		node1.children.append(node2)
	return tree

def f_naive():
	edges, operations = read_input()
	tree = make_tree_naive(edges)
	for v, n in operations:
		tree[v].add(n)
	a = sorted(tree[1].walk(0))
	print ' '.join(str(n) for i, n in a)


#################### process ####################

def read_input():
	N, Q = map(int, raw_input().split())
	edges = [ map(int, raw_input().split()) for _ in range(N-1) ]
	return (edges, Q)

def make_tree(edges):
	tree = [ [] ] + [ [0] for _ in range(len(edges)+1) ]
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node1.append(v2)
	return tree

def walk(tree):
	table = []
	stack = [(1, tree[1][0])]
	while stack:
		v, n = stack.pop()
		table.append((v, n))
		for child in tree[v][1:]:
			a = (child, tree[child][0]+n)
			stack.append(a)
	table.sort()
	return table

def f():
	edges, Q = read_input()
	tree = make_tree(edges)
	for _ in range(Q):
		v, n = map(int, raw_input().split())
		tree[v][0] += n
	a = walk(tree)
	print ' '.join(str(n) for i, n in a)


#################### main ####################

f()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from functools import reduce

N, Q = map(int,input().split())
tree_list = [list(map(int, input().split())) for i in range(N-1)]
treat_list = [list(map(int, input().split())) for i in range(Q)]



counter = [0] * len(set(reduce(lambda x,y: x+y, tree_list)))
for node, ad in treat_list:
    counter[node-1] += ad

for pa, ch in tree_list:
    
    counter[ch-1] += counter[pa-1]

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from itertools import *
import sys
import time

sys.setrecursionlimit(10**6)

#################### library ####################


#################### cNode ####################

class cNode:
	def __init__(self, i):
		self.i = i
		self.counter = 0
		self.children = []
	
	def add(self, n):
		self.counter += n
	
	def walk(self, n):
		yield (self.i, self.counter + n)
		for child in self.children:
			for a in child.walk(self.counter + n):
				yield a


#################### naive ####################

def make_tree_naive(edges):
	tree = [ None ] + map(cNode, range(len(edges)+1))
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node2 = tree[v2]
		node1.children.append(node2)
	return tree

def f_naive():
	edges, operations = read_input()
	tree = make_tree_naive(edges)
	for v, n in operations:
		tree[v].add(n)
	a = sorted(tree[1].walk(0))
	print ' '.join(str(n) for i, n in a)


#################### process ####################

def read_input():
	N, Q = map(int, raw_input().split())
	edges = [ map(int, raw_input().split()) for _ in range(N-1) ]
	return (edges, Q)

def make_tree(edges):
	tree = [ [] ] + [ [0] for _ in range(len(edges)+1) ]
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node1.append(v2)
	return tree

def walk(tree, v, n):
	yield (v, tree[v][0] + n)
	for child in tree[v][1:]:
		for a in walk(tree, child, tree[v][0] + n):
			yield a

def f():
	edges, Q = read_input()
	tree = make_tree(edges)
	for _ in range(Q):
		v, n = map(int, raw_input().split())
		tree[v][0] += n
	a = sorted(walk(tree, 1, 0))
	print ' '.join(str(n) for i, n in a)


#################### main ####################

f()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from functools import reduce

N, Q = map(int,input().split())
tree_list = [list(map(int, input().split())) for i in range(N-1)]
treat_list = [list(map(int, input().split())) for i in range(Q)]



counter = [0] * len(set(reduce(lambda x,y: x+y, tree_list)))
for node, ad in treat_list:
    counter[node-1] += ad

for pa, ch in tree_list:
    counter[ch-1] += counter[pa-1]
print(counter)

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
a=[]
for i in range(0,N-1):
    A,B=map(int,input().split())
    X=[B,A]
    a.append(X)
s=[0]*N
for i in range(Q):
    p,x=map(int,input().split())
    s[p-1]+=x
a=sorted(a)
for i in range(N-1):
    s[a[i][0]-1]+=s[a[i][1]-1]
Y=""
for i in range(N-1):
    Y=Y+str(s[i])+" "
Y=Y+str(s[N-1])
print(Y)

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
a=[]
for i in range(0,N-1):
    A,B=map(int,input().split())
    X=[B,A]
    a.append(X)
s=[0]*N
for i in range(Q):
    p,x=map(int,input().split())
    s[p-1]+=x
a=sorted(a)
print(a)
for i in range(N-1):
    s[a[i][0]-1]+=s[a[i][1]-1]
Y=""
for i in range(N-1):
    Y=Y+str(s[i])+" "
Y=Y+str(s[N-1])
print(Y)

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
a=[]
b=[]
for i in range(N-1):
    A,B=map(int,input().split())
    a.append(A)
    b.append(B)
s=[0]*N
for i in range(Q):
    p,x=map(int,input().split())
    s[p-1]+=x
for i in range(1,N-1):
    k=i
    for j in range(i):
        if b[k]>=b[k-1]:
            break
        else:
            a[k],a[k-1]=a[k-1],a[k]
            b[k],b[k-1]=b[k-1],b[k]
            k-=1
for i in range(N-1):
    s[b[i]-1]+=s[a[i]-1]
Y=""
for i in range(N-1):
    Y=Y+str(s[i])+" "
Y=Y+str(s[N-1])
print(Y)

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
a=[]
b=[]
for i in range(N-1):
    A,B=map(int,input().split())
    a.append(A)
    b.append(B)
s=[0]*N
for i in range(Q):
    p,x=map(int,input().split())
    s[p-1]+=x
for i in range(1,N-1):
    k=i
    for j in range(i):
        if a[k]>=a[k-1]:
            break
        else:
            a[k],a[k-1]=a[k-1],a[k]
            b[k],b[k-1]=b[k-1],b[k]
            k-=1
for i in range(N-1):
    s[b[i]-1]+=s[a[i]-1]
Y=""
for i in range(N-1):
    Y=Y+str(s[i])+" "
Y=Y+str(s[N-1])
print(Y)

Submission status: Wrong Answer

Solution code:

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

from collections import defaultdict

N, Q = map(int, input().split())
graph = [ [0 for _ in range(N)] for _ in range(N) ]
counts = [0] * N

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

# 指示はまとめる
dd = defaultdict(int)
for i in range(Q):
    p, x = map(int, input().split())
    dd[p] += x


#print(graph)

def inc(p, x):
    # 0 index
    counts[p] += x

    # p は自分自身
    for i in range(p+1, N):
        if graph[p][i] == 0:
            continue
        inc(i, x)

for P, X in dd.items():
    P -= 1
    inc(P, X)

print(*counts)

Submission status: Wrong Answer

Solution code:

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

N, Q = map(int, input().split())
graph = [ [0 for _ in range(N)] for _ in range(N) ]
counts = [0] * N

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

#print(graph)

def inc(p, x):
    # 0 index
    counts[p] += x

    # p は自分自身
    for i in range(p+1, N):
        if graph[p][i] == 0:
            continue
        inc(i, x)


for i in range(Q):
    P, X = map(int, input().split())
    P -= 1
    inc(P, X)

print(*counts)

Submission status: Wrong Answer

Solution code:
from queue import Queue

N, Q = map(int, input().split())
G = [tuple(map(int, input().split())) for _ in range(N-1)]
W = [tuple(map(int, input().split())) for _ in range(Q)]


# 親に付いている重み
r = [0,] * (N+1)
for p, x in W:
    r[p] += x



# 木の隣接関係 (a<bは保証されている)
M = [[] for _ in range(N+1)]
for g in G:
    M[g[0]].append(g[1])

# print(M)

# 木の親子関係
P = [[] for _ in range(N+1)]
visited = [0,] * (N+1)

def bfs(start):
    q = Queue()
    q.put(start)
    while not q.empty():
        now = q.get()
        visited[now] = 1
        for child in M[now]:
            if not visited[child]:
                r[child] += r[now]
                q.put(child)


bfs(1)

print(*r[1:])

Submission status: Wrong Answer

Solution code:
def main():
    n, q = map(int, input().split())
    parents = {index + 1: [] for index in range(n)}
    children = {index + 1: [] for index in range(n)}
    values = {index + 1: 0 for index in range(n)}

    for _ in range(n - 1):
        parent, index = map(int, input().split())
        parents[index] = parents[parent] + [parent]
        for parent in parents[index]:
            children[parent].append(index)

    for _ in range(q):
        index, value = map(int, input().split())
        values[index] += value
        for child in children[index]:
            values[child] += value

    res = []
    for index in range(1, n + 1):
        res.append(values[index])
    print(' '.join(map(str, res)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q = [int(zz) for zz in input().split()]

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

for i in range(N):
    tmp = []
    q = deque([i])
    while q:
        p = q.pop()
        for j in G[p]:
            q.append(j)
            tmp.append(j)
    for j in tmp:
        if j in G[i]:
            continue
        G[i].append(j)

V = [0]*N
query = [0]*N
for i in range(Q):
    q,x = [int(zz) for zz in input().split()]
    q -= 1
    query[q] += x

for i in range(N):
    V[i] += query[i]
    for j in G[i]:
        V[j] += query[i]
[print(i,end=' ') for i in V]

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q = [int(zz) for zz in input().split()]

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

for i in range(N):
    tmp = []
    q = deque([i])
    while q:
        p = q.pop()
        for j in G[p]:
            q.append(j)
            tmp.append(j)
    for j in tmp:
        if j in G[i]:
            continue
        G[i].append(j)

V = [0]*N
for i in range(Q):
    q,x = [int(zz) for zz in input().split()]
    q -= 1
    V[q] += x
    for j in G[q]:
        V[j] += x

[print(i,end=' ') for i in V]

Submission status: Wrong Answer

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

Cnt = [0 for i in range(N+1)]
G = [[] 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())
    Cnt[p] += x

q = collections.deque()
q.append(1)
flag = [False for i in range(N+1)]
while q:
    v = q.pop()
    flag[v] = True
    for i in G[v]:
        if flag[i]:
            continue
        Cnt[i] += Cnt[v]
        q.append(i)

print(*Cnt[1:])