結果
問題 | No.1488 Max Score of the Tree |
ユーザー | H3PO4 |
提出日時 | 2022-07-30 08:47:06 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 177 ms / 2,000 ms |
コード長 | 1,016 bytes |
コンパイル時間 | 252 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 142,208 KB |
最終ジャッジ日時 | 2024-07-20 04:05:43 |
合計ジャッジ時間 | 4,585 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 147 ms
137,216 KB |
testcase_01 | AC | 150 ms
134,528 KB |
testcase_02 | AC | 159 ms
138,496 KB |
testcase_03 | AC | 161 ms
140,928 KB |
testcase_04 | AC | 171 ms
141,184 KB |
testcase_05 | AC | 43 ms
54,144 KB |
testcase_06 | AC | 76 ms
82,304 KB |
testcase_07 | AC | 115 ms
108,032 KB |
testcase_08 | AC | 99 ms
93,440 KB |
testcase_09 | AC | 83 ms
85,632 KB |
testcase_10 | AC | 113 ms
109,056 KB |
testcase_11 | AC | 155 ms
142,208 KB |
testcase_12 | AC | 47 ms
60,416 KB |
testcase_13 | AC | 69 ms
76,160 KB |
testcase_14 | AC | 94 ms
99,584 KB |
testcase_15 | AC | 84 ms
85,248 KB |
testcase_16 | AC | 55 ms
66,048 KB |
testcase_17 | AC | 70 ms
76,928 KB |
testcase_18 | AC | 121 ms
114,560 KB |
testcase_19 | AC | 87 ms
91,264 KB |
testcase_20 | AC | 68 ms
74,240 KB |
testcase_21 | AC | 58 ms
67,456 KB |
testcase_22 | AC | 90 ms
84,864 KB |
testcase_23 | AC | 45 ms
53,504 KB |
testcase_24 | AC | 45 ms
53,632 KB |
testcase_25 | AC | 48 ms
53,760 KB |
testcase_26 | AC | 80 ms
79,744 KB |
testcase_27 | AC | 58 ms
65,024 KB |
testcase_28 | AC | 69 ms
71,040 KB |
testcase_29 | AC | 74 ms
73,600 KB |
testcase_30 | AC | 141 ms
125,568 KB |
testcase_31 | AC | 177 ms
141,824 KB |
ソースコード
import sys from collections import deque 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 = [-10 ** 9] * (K + 1) dp[0] = 0 for a, b, c in edges: new_dp = dp.copy() value = c * (min(desc_leaves[a], desc_leaves[b])) for i in range(K + 1 - c): new_dp[i + c] = max(new_dp[i + c], dp[i] + value) dp = new_dp print(max(dp) + sum(ds * l for ds, l in zip(dist, is_leaf)))