結果

問題 No.1637 Easy Tree Query
ユーザー ThetaTheta
提出日時 2024-03-08 11:43:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 945 bytes
コンパイル時間 2,877 ms
コンパイル使用メモリ 11,776 KB
実行使用メモリ 55,960 KB
最終ジャッジ日時 2024-03-08 11:43:31
合計ジャッジ時間 24,983 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
9,984 KB
testcase_01 AC 23 ms
9,984 KB
testcase_02 AC 893 ms
53,304 KB
testcase_03 AC 24 ms
9,984 KB
testcase_04 AC 317 ms
33,712 KB
testcase_05 AC 623 ms
25,992 KB
testcase_06 AC 438 ms
22,424 KB
testcase_07 AC 209 ms
11,008 KB
testcase_08 AC 220 ms
27,372 KB
testcase_09 AC 592 ms
42,452 KB
testcase_10 AC 321 ms
19,316 KB
testcase_11 AC 769 ms
45,632 KB
testcase_12 AC 746 ms
38,116 KB
testcase_13 AC 152 ms
21,044 KB
testcase_14 AC 474 ms
43,148 KB
testcase_15 AC 712 ms
47,120 KB
testcase_16 AC 634 ms
55,300 KB
testcase_17 AC 219 ms
21,012 KB
testcase_18 AC 574 ms
25,976 KB
testcase_19 AC 611 ms
31,408 KB
testcase_20 AC 765 ms
37,456 KB
testcase_21 AC 424 ms
34,156 KB
testcase_22 AC 900 ms
55,960 KB
testcase_23 AC 227 ms
22,752 KB
testcase_24 AC 383 ms
34,292 KB
testcase_25 AC 643 ms
24,772 KB
testcase_26 AC 768 ms
39,492 KB
testcase_27 AC 916 ms
54,536 KB
testcase_28 AC 484 ms
23,912 KB
testcase_29 AC 611 ms
36,408 KB
testcase_30 AC 302 ms
36,212 KB
testcase_31 AC 485 ms
10,112 KB
testcase_32 AC 331 ms
25,288 KB
testcase_33 AC 586 ms
20,788 KB
testcase_34 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import cache
from math import inf
import sys


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