結果

問題 No.1637 Easy Tree Query
ユーザー titiatitia
提出日時 2021-08-06 21:25:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 556 ms / 2,000 ms
コード長 885 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 11,832 KB
実行使用メモリ 32,868 KB
最終ジャッジ日時 2023-10-17 04:47:24
合計ジャッジ時間 12,451 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,084 KB
testcase_01 AC 31 ms
10,084 KB
testcase_02 AC 466 ms
29,696 KB
testcase_03 AC 29 ms
10,084 KB
testcase_04 AC 218 ms
20,240 KB
testcase_05 AC 329 ms
17,488 KB
testcase_06 AC 228 ms
15,344 KB
testcase_07 AC 107 ms
10,584 KB
testcase_08 AC 171 ms
18,024 KB
testcase_09 AC 372 ms
24,952 KB
testcase_10 AC 169 ms
14,232 KB
testcase_11 AC 467 ms
26,664 KB
testcase_12 AC 423 ms
22,764 KB
testcase_13 AC 115 ms
14,540 KB
testcase_14 AC 341 ms
25,272 KB
testcase_15 AC 481 ms
27,320 KB
testcase_16 AC 441 ms
29,284 KB
testcase_17 AC 143 ms
14,448 KB
testcase_18 AC 317 ms
17,404 KB
testcase_19 AC 324 ms
18,804 KB
testcase_20 AC 431 ms
22,444 KB
testcase_21 AC 273 ms
20,504 KB
testcase_22 AC 536 ms
29,616 KB
testcase_23 AC 148 ms
15,632 KB
testcase_24 AC 252 ms
20,528 KB
testcase_25 AC 321 ms
16,872 KB
testcase_26 AC 451 ms
23,388 KB
testcase_27 AC 556 ms
28,608 KB
testcase_28 AC 249 ms
16,196 KB
testcase_29 AC 360 ms
21,904 KB
testcase_30 AC 228 ms
21,808 KB
testcase_31 AC 203 ms
10,220 KB
testcase_32 AC 204 ms
17,112 KB
testcase_33 AC 278 ms
14,464 KB
testcase_34 AC 265 ms
32,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,Q=map(int,input().split())
E=[[] for i in range(N)]

for i in range(N-1):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E[y].append(x)

N+=1 # ROOTの親の点を一つ余計に見るため、総ノード数を一つ増やしておく

ROOT=0

QUE=[ROOT] 
Parent=[-1]*(N+1)
Parent[ROOT]=N # ROOTの親を定めておく.
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            QUE.append(to)

Children=[1]*(N+1)

for x in TOP_SORT[::-1]: #(自分を含む)子ノードの数を調べる
    Children[Parent[x]]+=Children[x]


ANS=0
for i in range(Q):
    x,y=map(int,input().split())
    x-=1
    ANS+=Children[x]*y
    print(ANS)
0