結果

問題 No.1637 Easy Tree Query
ユーザー hiragnhiragn
提出日時 2022-11-29 23:25:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 828 ms / 2,000 ms
コード長 822 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 54,440 KB
最終ジャッジ日時 2024-04-16 13:39:54
合計ジャッジ時間 17,590 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,752 KB
testcase_01 AC 29 ms
10,880 KB
testcase_02 AC 748 ms
40,992 KB
testcase_03 AC 27 ms
10,752 KB
testcase_04 AC 282 ms
22,988 KB
testcase_05 AC 512 ms
25,920 KB
testcase_06 AC 350 ms
21,568 KB
testcase_07 AC 166 ms
14,464 KB
testcase_08 AC 237 ms
19,652 KB
testcase_09 AC 554 ms
30,420 KB
testcase_10 AC 257 ms
18,484 KB
testcase_11 AC 678 ms
34,000 KB
testcase_12 AC 620 ms
31,568 KB
testcase_13 AC 150 ms
16,828 KB
testcase_14 AC 477 ms
28,368 KB
testcase_15 AC 708 ms
34,004 KB
testcase_16 AC 623 ms
35,556 KB
testcase_17 AC 198 ms
17,844 KB
testcase_18 AC 481 ms
25,280 KB
testcase_19 AC 498 ms
27,336 KB
testcase_20 AC 632 ms
31,700 KB
testcase_21 AC 397 ms
25,416 KB
testcase_22 AC 794 ms
39,268 KB
testcase_23 AC 222 ms
18,492 KB
testcase_24 AC 365 ms
24,520 KB
testcase_25 AC 515 ms
25,536 KB
testcase_26 AC 664 ms
32,592 KB
testcase_27 AC 828 ms
39,908 KB
testcase_28 AC 390 ms
22,816 KB
testcase_29 AC 542 ms
29,256 KB
testcase_30 AC 311 ms
23,884 KB
testcase_31 AC 331 ms
18,176 KB
testcase_32 AC 297 ms
21,056 KB
testcase_33 AC 439 ms
23,604 KB
testcase_34 AC 456 ms
54,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from sys import setrecursionlimit


def main():
    setrecursionlimit(10 ** 6)

    n, q = map(int, input().split())
    adj = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        adj[a].append(b)
        adj[b].append(a)

    visited = [0] * n  # 0 = False
    subnodes = [0] * n

    def dfs(i):
        visited[i] = True
        res = 1
        for j in adj[i]:
            if visited[j]:
                continue
            res += dfs(j)
        subnodes[i] = res
        return res

    dfs(0)

    ans = []
    tmp = 0
    for _ in range(q):
        p, x = map(int, input().split())
        tmp += subnodes[p - 1] * x
        ans.append(tmp)
    print(*ans, sep='\n')


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