結果

問題 No.1637 Easy Tree Query
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-08-06 21:36:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 387 ms / 2,000 ms
コード長 1,016 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 81,428 KB
実行使用メモリ 106,272 KB
最終ジャッジ日時 2023-10-17 05:02:44
合計ジャッジ時間 10,109 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
53,604 KB
testcase_01 AC 42 ms
53,644 KB
testcase_02 AC 202 ms
106,272 KB
testcase_03 AC 95 ms
55,260 KB
testcase_04 AC 200 ms
85,104 KB
testcase_05 AC 205 ms
91,072 KB
testcase_06 AC 164 ms
86,632 KB
testcase_07 AC 92 ms
79,256 KB
testcase_08 AC 174 ms
83,216 KB
testcase_09 AC 287 ms
93,632 KB
testcase_10 AC 138 ms
83,664 KB
testcase_11 AC 316 ms
98,452 KB
testcase_12 AC 285 ms
95,744 KB
testcase_13 AC 126 ms
79,964 KB
testcase_14 AC 279 ms
90,492 KB
testcase_15 AC 328 ms
97,416 KB
testcase_16 AC 342 ms
97,300 KB
testcase_17 AC 136 ms
81,252 KB
testcase_18 AC 207 ms
90,732 KB
testcase_19 AC 221 ms
90,696 KB
testcase_20 AC 274 ms
96,008 KB
testcase_21 AC 221 ms
88,732 KB
testcase_22 AC 387 ms
101,096 KB
testcase_23 AC 147 ms
82,392 KB
testcase_24 AC 215 ms
87,008 KB
testcase_25 AC 197 ms
90,768 KB
testcase_26 AC 294 ms
97,364 KB
testcase_27 AC 367 ms
101,984 KB
testcase_28 AC 177 ms
87,836 KB
testcase_29 AC 255 ms
93,040 KB
testcase_30 AC 214 ms
86,944 KB
testcase_31 AC 97 ms
83,344 KB
testcase_32 AC 177 ms
84,680 KB
testcase_33 AC 169 ms
88,352 KB
testcase_34 AC 131 ms
90,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin

from collections import deque
def NC_Dij(lis,start):

    ret = [float("inf")] * len(lis)
    ret[start] = 0
    
    q = deque([start])
    plis = [i for i in range(len(lis))]

    while len(q) > 0:
        now = q.popleft()

        for nex in lis[now]:

            if ret[nex] > ret[now] + 1:
                ret[nex] = ret[now] + 1
                plis[nex] = now
                q.append(nex)

    return ret,plis

N,Q = map(int,stdin.readline().split())

lis = [ [] for i in range(N) ]

for i in range(N-1):
    a,b = map(int,stdin.readline().split())
    a -= 1
    b -= 1

    lis[a].append(b)
    lis[b].append(a)

dlis,plis = NC_Dij(lis,0)

di = [(dlis[i],i) for i in range(N)]
di.sort()
di.reverse()

c = [1] * N
#print (di,plis)

for td,v in di:
    if plis[v] != v:
        c[plis[v]] += c[v]

#print (c)

ANS = []
ans = 0

for i in range(Q):

    p,x = map(int,stdin.readline().split())
    p -= 1
    ans += c[p] * x
    ANS.append(str(ans))

print ("\n".join(ANS))
0