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:
from collections import defaultdict

N, Q = map(int, input().split())
parent = [0] * N
for _ in range(N-1):
    a, b = tuple(map(int, input().split()))
    if a > b:
        parent[a-1] = b-1
    else:
        parent[b-1] = a-1

px_list = []
for _ in range(Q):
    px_list.append(tuple(map(int, input().split())))

def is_parent(target_node, current_node):
    node = current_node
    if node == target_node:
        return True
    for _ in range(N):
        node = parent[node]
        if node == target_node:
            return True
    return False

counter = [0] * N
for p, x in px_list:
    for i in range(N):
        if is_parent(p-1, i):
            counter[i] += x

print(*counter)

Submission status: Wrong Answer

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

N, Q = map(int, input().strip().split())
connections = [[] for _ in range(N)]

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

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

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

search_list = [0, ]

while len(search_list) > 0:
    now = search_list.pop()
    now_count = counter[now]
    for p in connections[now]:
        search_list.append(p)
        counter[p] += now_count
print(" ".join(map(str, counter)))

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
p=[0 for i in range(Q)]
q=[0 for i in range(Q)]
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
  a[i],b[i]=min(a[i],b[i]),max(a[i],b[i])
for i in range(Q):
  p[i],q[i]=map(int,input().split())
#print(N,Q)
#print(a)
#print(b)
#print(p)
#print(q)

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(Q):
  K[p[i]-1]+=q[i]
#print(K)

for i in range(N-1):
  K[b[i]-1]+=K[a[i]-1]
#print(K)

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

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
p=[0 for i in range(Q)]
q=[0 for i in range(Q)]
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
for i in range(Q):
  p[i],q[i]=map(int,input().split())
#print(N,Q)
#print(a)
#print(b)
#print(p)
#print(q)

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(Q):
  K[p[i]-1]+=q[i]
#print(K)

for i in range(N-1):
  K[b[i]-1]+=K[a[i]-1]
#print(K)

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N, Q = list(map(int,input().split()))
A = []
S = [0]*N
for i in range(N-1):
    A.append(list(map(int,input().split())))
A = np.array(A)
def search(X,c):
    B = []
    for i in range(N-1):
        if X[i][0] == c:
            B.append(X[i][1])
    return(B)
for j in range(Q):
    x, p = list(map(int,input().split()))
    F = search(A,x)
    E = [x] + F
    while F != []:
        G = F
        F = []
        for i in G:
            F += search(A,i)
            E += F
    else:
        E = list(set(E))
        for i in E:
            S[i-1] += p
print(S)

Submission status: Wrong Answer

Solution code:
# abc138D - Ki
import numpy as np
N, Q = list(map(int,input().split()))
A = []
S = [0]*N
for i in range(N-1):
    A.append(list(map(int,input().split())))
A = np.array(A)
def search(X,c):
    B = []
    for i in range(N-1):
        if X[i][0] == c:
            B.append(X[i][1])
    return(B)
#https://note.nkmk.me/python-list-unique-duplicate/
#重ふs区した要素を持たないset型オブジェクトset(A),list(set(A))が使いやすいか？
for j in range(Q):
    x, p = list(map(int,input().split()))
    F = search(A,x)
    E = [x] + F
    while F != []:
        G = F
        F = []
        for i in G:
            F += search(A,i)
            E += F
    else:
        E = list(set(E))
        for i in E:
            S[i-1] += p
print(S)

Submission status: Wrong Answer

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

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


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

    px[p-1] += x

visited = [False] * N
for i in range(N):
    ans[i] += px[i]
    visited[i] = True
    for j in es[i]:
        #print(i,j,es)
        if visited[j] == True:
            continue
        ans[j] += ans[i]
    

print(*ans)

Submission status: Wrong Answer

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

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


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

    px[p-1] += x

visited = [False] * N
for i in range(N):
    ans[i] += px[i]
    for j in es[i]:
        #print(i,j,es)
        if visited[j] == False:
            ans[j] += ans[i]
    visited[i] = True

print(*ans)

Submission status: Wrong Answer

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

es.sort()
ans = [0] * N
px = []
for i in range(Q):
    p, x = map(int, input().split())
    #px.append((p-1,x))
    ans[p-1] += x


for a,b in es:
    ans[b] += ans[a]

print(*ans)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
# 4 3
T = [0]*(N+1)
V = [0]*(N+1)
for _ in range(N-1):
  a,b = map(int,input().split())
  # 1 2
  # 2 3
  # 2 4
  T[b] = a
  # T[2] = 1, T[3] = 2, T[4] = 2

for _ in range(Q):
  p,x = map(int,input().split())
  # 2 10
  # 1 100
  # 3 1
  V[p]+=x
  # V[2] += 10
  # V[1] += 100
  # V[3] += 1

for i in range(1,N+1):
  V[i]+=V[T[i]]
  # V[1] += V[T[1]>>0]>>0
  # V[2] += V[T[2]>>1]>>100
  # V[3] += V[T[3]>>2]>>10+100
  # V[4] += V[T[4]>>2]>>10+100
print(*V[1:])

Submission status: Wrong Answer

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

edge = []
for i in range(1,n):
    a, b = list(map(int,input().split()))
    if a>b:
        a, b = b, a
    edge.append([a,b])
edge = sorted(edge)
    
score = []
for i in range(q):
    a, b = list(map(int,input().split()))
    score.append([a,b])


class Node(object):
    def __init__(self, idx):
        self.idx = idx
        self.child = []
        self.score = 0
        
    def addChild(self,child_node):
        self.child.append(child_node)
    
    def addScore(self, score):
        self.score += score
        for it in self.child:
            it.addScore(score)
            
    def getScore(self):
        return self.score
    
    def __repr__(self):
        s = 'Node(idx:{} value:{}, child:{}'.format(self.idx, self.score, len(self.child))
        return s


tree = {i:Node(i) for i in range(1, n+1)}

for it in edge:
    tree[it[0]].addChild(tree[it[1]])

for it in score:
    tree[it[0]].addScore(it[1])
    
print(' '.join([str(tree[i].getScore()) for i in range(1, n+1)]))

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n-1)]
px = [list(map(int,input().split())) for i in range(q)]

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

point = [0]*n
for p,x in px:
	point[p-1] += x

ans = [0]*n
ans[0] = point[0]
visited = [False]*n
stock = [0]

while stock:
	# print('while')
	# print(stock)
	# print(ans)
	s = stock.pop()
	for p in pea[s]:
		# print('for')
		if visited[p]:
			continue
		# print(s,p)
		# print()
		visited[p] = True
		stock.append(p)
		ans[p] = ans[s]+point[p]
print(*ans)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n-1)]
px = [list(map(int,input().split())) for i in range(q)]

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

point = [0]*n
for p,x in px:
	point[p-1] += x

ans = [0]*n
ans[0] = point[0]
visited = [False]*n
stock = [0]

while stock:
	# print('while')
	# print(stock)
	# print(ans)
	s = stock.pop()
	for p in pea[s]:
		# print('for')
		if visited[p]:
			continue
		# print(s,p)
		# print()
		visited[p] = True
		stock.append(p)
		ans[p] = ans[s]+point[p]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n+1)]
ab = [[x for x in map(int, input().split(' '))] for _ in range(n-1)]
for a,b in ab:
    inf[a].append(b)
    inf[b].append(a)
point = [0]*(n+1)
px = [[x for x in map(int, input().split(' '))] for _ in range(q)]
for p,x in px:
    point[p] += x
def add_p(num, oya,aaa):
    for k in inf[num]:
        if k != oya:
            point[k] += aaa
            add_p(k, num, point[k])  
for i in inf[1]:
    point[i] += point[1]
    add_p(i,1,point[1])
point.pop(0)
print(*point)

Submission status: Wrong Answer

Solution code:
import sys

N, Q = (int(x) for x in input().split())
a = []
b = []
tree = [[t+1] for t in range(N)]
count = [0 for c in range(N)]

for i in range(N-1):                                                                                                                                                                                        
    a_tmp, b_tmp = (int(x) for x in input().split())                                                                                                                                                        
                                                                                                                                                                                                            
    a.append(a_tmp)                                                                                                                                                                                         
    b.append(b_tmp)                                                                                                                                                                                         
                                                                                                                                                                                                            
    tree[a_tmp-1].append(b_tmp)                                                                                                                                                                             

for j in range(N):                                                                                                                                                                                          
    for k in tree[N-1-j]:                                                                                                                                                                                   
        if k != N-j:                                                                                                                                                                                        
            for l in tree[k-1]:                                                                                                                                                                             
                if l != k:                                                                                                                                                                                  
                    tree[N-1-j].append(l)                                                                                                                                                                   

for m in range(Q):                                                                                                                                                                                          
    p, x = (int(x) for x in input().split())                                                                                                                                                                
                                                                                                                                                                                                            
    for n in tree[p-1]:                                                                                                                                                                                     
        count[n-1] += x                                                                                                                                                                                     

count = [str(a) for a in count]
count = ' '.join(count)                                                                                                                                                                                      
print(count)

Submission status: Wrong Answer

Solution code:
import sys
import collections

sys.setrecursionlimit(1000000)

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

tree = collections.defaultdict(list)
counter = {}
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    counter[a] = 0
    counter[b] = 0

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

    def add(p_, x_):
        counter[p_] += x_
        for v in tree[p_]:
            add(v, x_)

    add(p, x)

print(' '.join(str(v) for _, v in sorted(counter.items(), key=lambda x: x[0])))

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
connect = {b:0 for b in range(1,n+1)}
ans = [0]*n

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


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

for i in range(2, n+1):
    ans[i-1] += ans[connect[i]-1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q=map(int, input().split())
 
edge=[list(map(int, input().split())) for i in range(N-1)]
adds=[list(map(int, input().split())) for i in range(Q)]
 
point=[0]*(N+1)
 
for i in range(Q):
  point[adds[i][0]]+=adds[i][1]
  
for i in range(N-1):
  point[edge[i][1]]+=point[edge[i][0]]
  
print(' '.join(map(str, map(int, point[1:]))))

Submission status: Wrong Answer

Solution code:
N, Q=map(int, input().split())
 
edge=[list(map(int, input().split())) for i in range(N-1)]
adds=[list(map(int, input().split())) for i in range(Q)]
 
point=[0]*N
 
for i in range(Q):
  point[adds[i][0]-1]+=adds[i][1]
  
for i in range(N-1):
  point[edge[i][1]-1]+=point[edge[i][0]-1]
  
print(' '.join(map(str, map(int, point))))

Submission status: Wrong Answer

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

N,Q = map(int,input().split())
AB = [[x for x in map(int,input().split())] for _ in range(N-1)]
PX = [[x for x in 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)

cnt = [0]*N
for p,x in PX:
    cnt[p-1] += x

def dfs(v,parent,add):
    for w in graph[v]:
        if w != parent:
             cnt[w-1] += add
             dfs(w,v,cnt[w-1])

dfs(1,0,0)

print(*cnt)

Submission status: Wrong Answer

Solution code:
N, Q = (int(x) for x in input().split())
AB = []
P = []
X = []

for i in range(0, N-1):
    a, b =  (int(x) for x in input().split())
    AB.append([a, b])
for i in range(0, Q):
    p, x =  (int(x) for x in input().split())
    P.append(p)
    X.append(x)
AB.sort(key=lambda x: x[1])

C = [0] * N
k = 0
for i in P:
    C[i-1] = C[i-1] + X[k]
    k = k + 1

for i in range(1,N):
    C[i] = C[i] + C[AB[i-1][0]-1]

print(*C)

Submission status: Wrong Answer

Solution code:
N, Q = (int(x) for x in input().split())
A = []
B = []
P = []
X = []

for i in range(0, N-1):
    a, b =  (int(x) for x in input().split())
    A.append(a)
    B.append(b)
for i in range(0, Q):
    p, x =  (int(x) for x in input().split())
    P.append(p)
    X.append(x)

C = [0] * N
k = 0
for i in P:
    C[i-1] = C[i-1] + X[k]
    k = k + 1

for i in range(1,N):
    C[i] = C[i] + C[A[i-1]-1]

print(*C)

Submission status: Wrong Answer

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################


'''from sys import stdin,setrecursionlimit
#from copy import deepcopy
 
setrecursionlimit(10**6)
input=stdin.readline'''

def dfs(p,father=-1):
    for i in tree[p]:
        if i == father:
            continue
        counter[i]+=counter[p]
        dfs(i,p)

n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    

#dfs(0)
stack = [0]
father = -1
while stack:
    yy = stack.pop()
    for i in tree[yy]:
        if i != father:
            counter[i]+=counter[yy]
            stack.append(i)
            father = yy
print(*counter)

Submission status: Wrong Answer

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
from collections import defaultdict
tree = defaultdict(list)


def inc(tree,counter,p):
    if tree[p] == []:
        return
    for i in tree[p]:
        counter[i-1]+=counter[p-1]
        inc(tree,counter,i)

n ,q = I0()
counter = [0]*n

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


for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    

#inc(tree,counter,1)
'''for i in range(1,n):
    for j in tree[i]:
        counter[j-1]+=counter[i-1]'''
stack = [1]
while stack:
    yy = stack.pop()
    for i in tree[yy]:
        counter[i-1]+=counter[yy-1]
        stack.append(i)


print(*counter)

Submission status: Wrong Answer

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
from collections import defaultdict
tree = defaultdict(list)


def inc(tree,counter,p):
    if tree[p] == []:
        return
    for i in tree[p]:
        counter[i-1]+=counter[p-1]
        inc(tree,counter,i)

n ,q = I0()
counter = [0]*n

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


for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    

#inc(tree,counter,1)
for i in range(1,n):
    for j in tree[i]:
        counter[j-1]+=counter[i-1]

print(*counter)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
dic = {}
dic_score = {}
ans = [0] * (n + 1)
for i in range(n - 1):
    a, b = map(int, input().split())
    dic.setdefault(b, a)

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

for k in range(n + 1):
    if k in dic:
        ans[k] = ans[k] + ans[dic[k]]

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
#前から順に詰めてけばいいんじゃないか？
#前から見ていくときに上が先に足されてればいいのでは
#input()ではなくreadline使う
#末尾の改行コードを取り除かねばならない
#intにするときは改行コード取らずにintにできる
#https://qiita.com/kyuna/items/8ee8916c2f4e36321a1c#3-sysstdin
#https://www.lifewithpython.com/2014/05/python-get-stdin-standard-input.html
import sys
n,q=map(int,input().split())
tree=[list(map(int,sys.stdin.readline().split())) for i in range(n-1)]
#f1は使ったのかどうかを表すフラッグ
#f2はそれまでに出てきたものを表すフラッグ
f1=[0]*(n-1)
f2=[1]

c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x
#print(tree)
for i in range(n*n):
    #print(c)
    for i,t in enumerate(tree):
        if f1[i]==0:
            if t[0] in f2:
                c[t[1]-1]+=c[t[0]-1]
                f1[i]=1
                f2.append(t[1])
            elif t[1] in f2:
                c[t[0]-1]+=c[t[1]-1]
                f1[i]=1
                f2.append(t[0])
            else:
                pass
    if f1!=[1]*(n-1):
        break

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

Submission status: Wrong Answer

Solution code:

import sys
n,q=map(int,input().split())
tree=[list(map(int,sys.stdin.readline().split())) for i in range(n-1)]
f1=[0]*(n-1)
f2=[1]

c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x
for i in range(2*n):
    for i,t in enumerate(tree):
        if f1[i]==0:
            if t[0] in f2:
                c[t[1]-1]+=c[t[0]-1]
                f1[i]=1
                f2.append(t[1])
            elif t[1] in f2:
                c[t[0]-1]+=c[t[1]-1]
                f1[i]=1
                f2.append(t[0])
            else:
                pass
    if f1!=[1]*(n-1):
        break

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

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tree=[list(map(int,input().split())) for i in range(n-1)]
f1=[0]*(n-1)
f2=[1]

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

while f1==[1]*(n-1):
    for i,t in enumerate(tree):
        if f1[i]==0:
            if t[0] in f2:
                c[t[1]-1]+=c[t[0]-1]
                f1[i]=1
                f2.append(i[1])
            elif i[1] in f2:
                c[t[0]-1]+=c[t[1]-1]
                f1[i]=1
                f2.append(i[0])
            else:
                pass

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

Submission status: Wrong Answer

Solution code:
import numpy as np
n, q = map(int, input().split(" "))
lis = np.array(list(set(list(map(tuple,[[int(j) for j in input().split(" ")] for i in range(n-1)])))))-1
lis = lis[np.argsort(lis[:,0])]

c = [0 for i in range(n)]
for i in range(q):
    u, v = map(int, input().split(" ")) 
    c[u-1] += v

for i in lis:
    c[i[1]] += c[i[0]]
print(" ".join(map(str, c)))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, q = map(int, input().split(" "))
lis = np.array([[int(j) for j in input().split(" ")] for i in range(n-1)])-1
lis = lis[np.argsort(lis[:,0])]

c = [0 for i in range(n)]
for i in range(q):
    u, v = map(int, input().split(" ")) 
    c[u-1] += v

for i in lis:
    c[i[1]] += c[i[0]]
print(" ".join(map(str, c)))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, q = map(int, input().split(" "))
lis = np.array([[int(j) for j in input().split(" ")] for i in range(n-1)])-1
lis = lis[np.argsort(lis[:,0])]

c = np.array([0 for i in range(n)])
for i in range(q):
    u, v = map(int, input().split(" ")) 
    c[u-1] += v
for i in lis:
    c[i[1]] += c[i[0]]
print(" ".join(map(str, c)))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, q = map(int, input().split(" "))
lis = np.array([[int(j) for j in input().split(" ")] for i in range(n-1)])

c = np.array([0 for i in range(n)])
for i in range(q):
    u, v = map(int, input().split(" ")) 
    c[u-1] += v
for i in range(n):
    tm = lis[np.where(lis[:,0]==i+1)[0]][:,1]
    c[tm-1] += c[i]
print(" ".join(map(str, c)))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, q = map(int, input().split(" "))
lis = np.array([[int(j) for j in input().split(" ")] for i in range(n-1)])

p = np.array([[int(j) for j in input().split(" ")] for i in range(q)])
s = np.argsort(p[:,0])
c = np.array([0 for i in range(n)])
i = 0
ind = 0
while i < n:
    tm = lis[np.where(lis[:,0]==i)[0]][:,1]
    while ind < q:
        t = p[s[ind]]
        if t[0] > i:
            break        
        c[t[0]-1] += t[1]
        c[tm-1] = c[t[0]-1]
        ind += 1
    c[tm-1] = c[i]
    i += 1
print(" ".join(map(str, c)))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, q = map(int, input().split(" "))
lis = [[] for i in range(n)]
for i in range(n-1):
    tmp = [int(i) for i in input().split(" ")]
    lis[tmp[0]-1].append(tmp[1]-1)

p = np.array([[int(j) for j in input().split(" ")] for i in range(q)])
s = np.argsort(p[:,0])
c = np.array([0 for i in range(n)])
i = 0
ind = 0
while i < n:
    while ind < q:
        t = p[s[ind]]
        if t[0] > i:
            break        
        c[t[0]-1] += t[1]
        c[lis[t[0]-1]] = c[t[0]-1]
        ind += 1
    c[lis[i]] = c[i]
    i += 1
print(" ".join(map(str, c)))

Submission status: Wrong Answer

Solution code:
import numpy as np
n, q = map(int, input().split(" "))
lis = [[] for i in range(n)]
for i in range(n-1):
    tmp = [int(i) for i in input().split(" ")]
    lis[tmp[0]-1].append(tmp[1]-1)

p = np.array([[int(j) for j in input().split(" ")] for i in range(q)])
s = np.argsort(p[:,0])
c = np.array([0 for i in range(n)])
i = 0
ind = 0
while i < n-1:
    i += 1
    c[lis[i-1]] = c[i-1]
    while ind < q:
        t = p[s[ind]]
        if t[0] > i:
            break        
        c[t[0]-1] += t[1]
        c[lis[t[0]-1]] = c[t[0]-1]
        ind += 1
print(" ".join(map(str, c)))

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))

        if A not in tree:
            tree[A] = []
        if B not in tree:
            tree[B] = [A]
        else:
            tree[B].append(A)

    point = [0] * (N + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] += B
        
    pointList = [0] * (N + 1)
    for idx, val in enumerate(tree):
        for v in tree[val]:
            pointList[val] += pointList[v]
        pointList[val] += point[val]
            
    print(*pointList[1:])

Submission status: Wrong Answer

Solution code:
tree = {}
N, Q = map(int, input().split(' '))
for i in [input() for i in range(N-1)]:
    A, B = map(int, i.split(' '))

    if A not in tree:
        tree[A] = []

    if B not in tree:
        tree[B] = [A]
    else:
        tree[B].append(A)

point = [0] * (N + 1)
for i in [input() for i in range(Q)]:
    A, B = map(int, i.split(' '))
    point[A] += B
    
pointList = [0] * (N + 1)
for idx, val in enumerate(tree):
    for v in tree[val]:
        pointList[val] += pointList[v]
    pointList[val] += point[val]
        
print(*pointList[1:])

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))

        if A not in tree:
            tree[A] = []

        if B not in tree:
            tree[B] = [A]

    point = [0] * (N + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] += B
        
    pointList = [0] * (N + 1)
    for idx, val in enumerate(tree):
        if val is 1:
            pointList[val] = point[val]

        if val > 1:
            for v in tree[val]:
                pointList[val] += pointList[v]
            pointList[val] += point[val]
            
    print(*pointList[1:])

Submission status: Wrong Answer

Solution code:


#有向グラフ
#グラフの隣接リストによる実装
class edge:
#    def __init__(self,to,cost):
    def __init__(self,to):
        self.to = to
        #self.cost = cost

#N個の頂点、N-1個の辺、Q操作回数
N,Q = map(int,(input().split()))

#添字と入力が1ずれるので注意
#グラフ (iがグラフの頂点、G[i]に隣接するグラフが入っている)
G = [ [] for row in range(N)]
for i in range(N-1):
    #sからtへ辺を張る
    s,t = map(int,input().split())
    #添字調整
    s -= 1
    t -= 1
    G[s].append(edge(t))

#各頂点以下に加算する重み
ans = [0] * N
for i in range(Q):
    #頂点pにxの重みをつける
    p,x = map(int,input().split())
    #添字調整
    p -= 1
    ans[p] += x

#各頂点以下の重みの和を上から順に計算する
for i in range(N):
    for j in range(len(G[i])):
            ans[G[i][j].to] += ans[i]

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

Submission status: Wrong Answer

Solution code:


#有向グラフ
#グラフの隣接リストによる実装
class edge:
#    def __init__(self,to,cost):
    def __init__(self,to):
        self.to = to
        #self.cost = cost

#N個の頂点、N-1個の辺、Q操作回数
N,Q = map(int,(input().split()))

#添字と入力が1ずれるので注意
#グラフ (iがグラフの頂点、G[i]に隣接するグラフが入っている)
G = [ [] for row in range(N)]
for i in range(N-1):
    #sからtへ辺を張る
    s,t = map(int,input().split())
    #添字調整
    s -= 1
    t -= 1
    G[s].append(edge(t))

#各頂点以下に加算する重み
ans = [0] * N

for i in range(Q):
    #頂点p以下にxの重みをつける
    p,x = map(int,input().split())
    #添字調整
    p -= 1
    ans[p] += x

#各頂点以下の重みの和を計算する
for i in range(len(G)):
    for j in range(len(G[i])):
            ans[G[i][j].to] += ans[i]

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

Submission status: Wrong Answer

Solution code:
#木の問題と書いてあるが、実際は木の構造がわからなくても解ける
#根の方から順に足していってあげればいい
#aiがbiの親であるとは限らないらしい
#これがある以上、木構造を作るしかない
#辞書で(他の人のコードを参照)親子関係を保持できる、初めて知った
#は？結局他人の使わないやんけ
from copy import deepcopy

n,q=map(int,input().split())
tree1=[list(map(int,input().split())) for i in range(n-1)]
tree2=[i for i in tree1 if i[0]==1]
tree1=[i for i in tree1 if i[0]!=1]

c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x
l=0
while tree1!=[]:
    for j in tree2[l:][:]:
        #l+=1
        #三項演算子でelseは省略できない
        #print(tree1)
        tree3=[(k if j[1]==k[0] else ([k[1],k[0]] if j[1]==k[1] else None)) for k in tree1]
        #print(tree3)
        tree3=[i for i in tree3 if i is not None]
        l+=len(tree3)
        tree1=[k for k in tree1 if j[1] not in k]
        tree2.extend(tree3)
#print(tree2)
for i in tree2:
    c[i[1]-1]+=c[i[0]-1]

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