結果
問題 |
No.1637 Easy Tree Query
|
ユーザー |
|
提出日時 | 2024-03-08 11:44:28 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
RE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 975 bytes |
コンパイル時間 | 284 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 56,640 KB |
最終ジャッジ日時 | 2024-09-29 18:47:06 |
合計ジャッジ時間 | 22,003 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 32 RE * 1 |
ソースコード
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()