結果
| 問題 |
No.1637 Easy Tree Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-10-01 16:58:32 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 976 bytes |
| コンパイル時間 | 468 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 56,904 KB |
| 最終ジャッジ日時 | 2024-10-01 16:59:00 |
| 合計ジャッジ時間 | 26,008 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 32 RE * 1 |
ソースコード
from functools import cache
from math import inf
import sys
sys.setrecursionlimit(5000000)
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()