結果

問題 No.1637 Easy Tree Query
ユーザー tktk_snsntktk_snsn
提出日時 2021-08-06 22:36:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 566 ms / 2,000 ms
コード長 629 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 11,896 KB
実行使用メモリ 32,656 KB
最終ジャッジ日時 2023-10-17 05:14:24
合計ジャッジ時間 13,301 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,076 KB
testcase_01 AC 29 ms
10,076 KB
testcase_02 AC 465 ms
30,524 KB
testcase_03 AC 29 ms
10,076 KB
testcase_04 AC 215 ms
20,556 KB
testcase_05 AC 316 ms
17,572 KB
testcase_06 AC 226 ms
15,588 KB
testcase_07 AC 103 ms
10,560 KB
testcase_08 AC 173 ms
18,440 KB
testcase_09 AC 387 ms
25,640 KB
testcase_10 AC 168 ms
14,616 KB
testcase_11 AC 469 ms
27,304 KB
testcase_12 AC 414 ms
23,188 KB
testcase_13 AC 115 ms
14,872 KB
testcase_14 AC 342 ms
25,944 KB
testcase_15 AC 483 ms
27,940 KB
testcase_16 AC 455 ms
29,840 KB
testcase_17 AC 140 ms
14,868 KB
testcase_18 AC 314 ms
17,564 KB
testcase_19 AC 325 ms
19,144 KB
testcase_20 AC 417 ms
22,816 KB
testcase_21 AC 275 ms
20,788 KB
testcase_22 AC 545 ms
30,180 KB
testcase_23 AC 153 ms
15,808 KB
testcase_24 AC 250 ms
20,848 KB
testcase_25 AC 316 ms
17,112 KB
testcase_26 AC 448 ms
24,076 KB
testcase_27 AC 566 ms
29,436 KB
testcase_28 AC 249 ms
16,648 KB
testcase_29 AC 358 ms
22,272 KB
testcase_30 AC 237 ms
22,176 KB
testcase_31 AC 198 ms
10,176 KB
testcase_32 AC 210 ms
17,272 KB
testcase_33 AC 273 ms
14,608 KB
testcase_34 AC 272 ms
32,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

par = [-1] * N
topo = []
que = [0]
while que:
    s = que.pop()
    topo.append(s)
    for t in G[s]:
        if t == par[s]:
            continue
        par[t] = s
        que.append(t)

sz = [1] * N
for s in topo[::-1][:-1]:
    p = par[s]
    sz[p] += sz[s]

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