結果

問題 No.1637 Easy Tree Query
ユーザー yvay5cqeyvay5cqe
提出日時 2021-08-18 21:28:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 888 ms / 2,000 ms
コード長 539 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 61,056 KB
最終ジャッジ日時 2024-04-20 02:33:34
合計ジャッジ時間 19,957 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,752 KB
testcase_01 AC 27 ms
10,624 KB
testcase_02 AC 854 ms
51,712 KB
testcase_03 AC 29 ms
10,752 KB
testcase_04 AC 302 ms
27,904 KB
testcase_05 AC 591 ms
34,688 KB
testcase_06 AC 405 ms
27,264 KB
testcase_07 AC 196 ms
16,256 KB
testcase_08 AC 242 ms
24,320 KB
testcase_09 AC 598 ms
40,704 KB
testcase_10 AC 295 ms
22,784 KB
testcase_11 AC 754 ms
46,976 KB
testcase_12 AC 707 ms
42,112 KB
testcase_13 AC 162 ms
18,944 KB
testcase_14 AC 473 ms
36,864 KB
testcase_15 AC 759 ms
46,720 KB
testcase_16 AC 646 ms
45,312 KB
testcase_17 AC 219 ms
20,864 KB
testcase_18 AC 555 ms
33,664 KB
testcase_19 AC 569 ms
35,072 KB
testcase_20 AC 718 ms
42,752 KB
testcase_21 AC 432 ms
32,000 KB
testcase_22 AC 857 ms
51,712 KB
testcase_23 AC 231 ms
22,144 KB
testcase_24 AC 382 ms
30,464 KB
testcase_25 AC 573 ms
34,176 KB
testcase_26 AC 735 ms
44,160 KB
testcase_27 AC 888 ms
52,480 KB
testcase_28 AC 446 ms
29,440 KB
testcase_29 AC 599 ms
38,400 KB
testcase_30 AC 328 ms
29,312 KB
testcase_31 AC 404 ms
20,608 KB
testcase_32 AC 323 ms
26,240 KB
testcase_33 AC 521 ms
30,464 KB
testcase_34 AC 469 ms
61,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
N, Q = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(N-1)]
qx = [tuple(map(int, input().split())) for _ in range(Q)]

g = [[] for _ in range(N)]

for a, b in ab:
    g[a-1].append(b-1)
    g[b-1].append(a-1)

cnts = [0] * N


def dfs(now, prev):
    cnt = 1
    for nx in g[now]:
        if nx == prev:
            continue
        cnt += dfs(nx, now)
    cnts[now] = cnt
    return cnt


dfs(0, -1)

ans = 0
for q, x in qx:
    ans += cnts[q-1] * x
    print(ans)
0