結果

問題 No.1637 Easy Tree Query
ユーザー ThetaTheta
提出日時 2024-03-08 11:44:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 906 ms / 2,000 ms
コード長 975 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 126,336 KB
最終ジャッジ日時 2024-03-08 11:44:51
合計ジャッジ時間 19,861 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
9,984 KB
testcase_01 AC 24 ms
9,984 KB
testcase_02 AC 879 ms
53,304 KB
testcase_03 AC 24 ms
9,984 KB
testcase_04 AC 307 ms
33,712 KB
testcase_05 AC 619 ms
25,992 KB
testcase_06 AC 418 ms
22,424 KB
testcase_07 AC 235 ms
11,008 KB
testcase_08 AC 221 ms
27,372 KB
testcase_09 AC 591 ms
42,452 KB
testcase_10 AC 316 ms
19,316 KB
testcase_11 AC 769 ms
45,632 KB
testcase_12 AC 741 ms
38,116 KB
testcase_13 AC 154 ms
21,044 KB
testcase_14 AC 461 ms
43,148 KB
testcase_15 AC 753 ms
47,120 KB
testcase_16 AC 651 ms
55,300 KB
testcase_17 AC 218 ms
21,012 KB
testcase_18 AC 590 ms
25,976 KB
testcase_19 AC 621 ms
31,408 KB
testcase_20 AC 749 ms
37,456 KB
testcase_21 AC 457 ms
34,156 KB
testcase_22 AC 878 ms
55,960 KB
testcase_23 AC 228 ms
22,752 KB
testcase_24 AC 370 ms
34,292 KB
testcase_25 AC 641 ms
24,772 KB
testcase_26 AC 760 ms
39,492 KB
testcase_27 AC 906 ms
54,536 KB
testcase_28 AC 475 ms
23,912 KB
testcase_29 AC 637 ms
36,408 KB
testcase_30 AC 303 ms
36,212 KB
testcase_31 AC 476 ms
10,112 KB
testcase_32 AC 325 ms
25,288 KB
testcase_33 AC 569 ms
20,788 KB
testcase_34 AC 548 ms
126,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import cache
from math import inf
import sys
sys.setrecursionlimit(500000)


def printe(*args, end="\n", **kwargs):
    print(*args, end=end, file=sys.stderr, **kwargs)


def main():
    N, Q = map(int, input().split())
    tree = [set() for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(lambda n: int(n) - 1, input().split())
        tree[a].add(b)
        tree[b].add(a)
    tree_size = [inf for _ in range(N)]

    @cache
    def dfs_size(start_idx: int, parent: int = -1) -> int:
        cur_size = 1
        for neighbor in tree[start_idx]:
            if neighbor == parent:
                continue
            cur_size += dfs_size(neighbor, start_idx)
        tree_size[start_idx] = cur_size
        return cur_size

    dfs_size(0)

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


if __name__ == "__main__":
    main()
0