結果
問題 | No.1488 Max Score of the Tree |
ユーザー | H3PO4 |
提出日時 | 2022-07-30 09:04:06 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 506 ms / 2,000 ms |
コード長 | 1,009 bytes |
コンパイル時間 | 96 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 46,268 KB |
最終ジャッジ日時 | 2024-07-20 04:29:23 |
合計ジャッジ時間 | 18,040 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 489 ms
45,400 KB |
testcase_01 | AC | 497 ms
45,380 KB |
testcase_02 | AC | 491 ms
45,648 KB |
testcase_03 | AC | 506 ms
45,796 KB |
testcase_04 | AC | 491 ms
45,584 KB |
testcase_05 | AC | 468 ms
44,116 KB |
testcase_06 | AC | 470 ms
44,384 KB |
testcase_07 | AC | 482 ms
44,944 KB |
testcase_08 | AC | 482 ms
44,680 KB |
testcase_09 | AC | 471 ms
43,996 KB |
testcase_10 | AC | 479 ms
44,564 KB |
testcase_11 | AC | 496 ms
45,388 KB |
testcase_12 | AC | 468 ms
44,256 KB |
testcase_13 | AC | 486 ms
43,868 KB |
testcase_14 | AC | 487 ms
44,544 KB |
testcase_15 | AC | 488 ms
44,500 KB |
testcase_16 | AC | 473 ms
44,244 KB |
testcase_17 | AC | 480 ms
44,512 KB |
testcase_18 | AC | 488 ms
44,812 KB |
testcase_19 | AC | 486 ms
44,804 KB |
testcase_20 | AC | 477 ms
44,004 KB |
testcase_21 | AC | 493 ms
44,120 KB |
testcase_22 | AC | 476 ms
44,124 KB |
testcase_23 | AC | 466 ms
44,376 KB |
testcase_24 | AC | 464 ms
43,864 KB |
testcase_25 | AC | 469 ms
44,120 KB |
testcase_26 | AC | 497 ms
45,188 KB |
testcase_27 | AC | 476 ms
43,996 KB |
testcase_28 | AC | 483 ms
44,380 KB |
testcase_29 | AC | 478 ms
43,868 KB |
testcase_30 | AC | 492 ms
45,408 KB |
testcase_31 | AC | 493 ms
46,268 KB |
ソースコード
import sys from collections import deque import numpy as np input = sys.stdin.buffer.readline N, K = map(int, input().split()) edges = [] T = [[] for _ in range(N)] for _ in range(N - 1): a, b, c = map(int, input().split()) a -= 1 b -= 1 edges.append((a, b, c)) T[a].append((b, c)) T[b].append((a, c)) dist = [None] * N dist[0] = 0 d = deque([0]) dfs_order = [] is_leaf = [True] * N while d: v = d.pop() dv = dist[v] for x, c in T[v]: if dist[x] is None: dfs_order.append((v, x)) dist[x] = dv + c d.append(x) is_leaf[v] = False desc_leaves = [int(l) for l in is_leaf] for v, x in reversed(dfs_order): desc_leaves[v] += desc_leaves[x] dp = np.full(K + 1, -10 ** 9) dp[0] = 0 for a, b, c in edges: new_dp = dp.copy() value = c * (min(desc_leaves[a], desc_leaves[b])) new_dp[c:] = np.maximum(new_dp[c:], dp[:-c] + value) dp = new_dp print(dp.max() + sum(ds * l for ds, l in zip(dist, is_leaf)))