結果
問題 | No.1488 Max Score of the Tree |
ユーザー | tamato |
提出日時 | 2021-04-23 21:48:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 297 ms / 2,000 ms |
コード長 | 1,480 bytes |
コンパイル時間 | 143 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 144,896 KB |
最終ジャッジ日時 | 2024-07-04 07:53:59 |
合計ジャッジ時間 | 6,102 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 284 ms
139,776 KB |
testcase_01 | AC | 270 ms
136,704 KB |
testcase_02 | AC | 288 ms
141,152 KB |
testcase_03 | AC | 297 ms
143,552 KB |
testcase_04 | AC | 295 ms
143,744 KB |
testcase_05 | AC | 42 ms
54,396 KB |
testcase_06 | AC | 111 ms
84,096 KB |
testcase_07 | AC | 192 ms
110,336 KB |
testcase_08 | AC | 142 ms
95,744 KB |
testcase_09 | AC | 128 ms
87,680 KB |
testcase_10 | AC | 198 ms
111,744 KB |
testcase_11 | AC | 290 ms
144,896 KB |
testcase_12 | AC | 51 ms
62,208 KB |
testcase_13 | AC | 108 ms
86,912 KB |
testcase_14 | AC | 167 ms
102,272 KB |
testcase_15 | AC | 129 ms
87,808 KB |
testcase_16 | AC | 73 ms
68,736 KB |
testcase_17 | AC | 95 ms
79,232 KB |
testcase_18 | AC | 210 ms
116,864 KB |
testcase_19 | AC | 138 ms
93,440 KB |
testcase_20 | AC | 91 ms
76,416 KB |
testcase_21 | AC | 69 ms
70,016 KB |
testcase_22 | AC | 122 ms
87,424 KB |
testcase_23 | AC | 42 ms
54,144 KB |
testcase_24 | AC | 41 ms
54,144 KB |
testcase_25 | AC | 45 ms
54,400 KB |
testcase_26 | AC | 108 ms
82,048 KB |
testcase_27 | AC | 63 ms
67,840 KB |
testcase_28 | AC | 83 ms
73,344 KB |
testcase_29 | AC | 90 ms
75,904 KB |
testcase_30 | AC | 251 ms
128,256 KB |
testcase_31 | AC | 296 ms
144,256 KB |
ソースコード
mod = 1000000007 eps = 10**-9 def main(): import sys from collections import deque input = sys.stdin.readline N, K = map(int, input().split()) adj = [[] for _ in range(N+1)] for _ in range(N-1): a, b, c = map(int, input().split()) adj[a].append((b, c)) adj[b].append((a, c)) que = deque() que.append(1) seen = [-1] * (N+1) seen[1] = 0 par = [None] * (N+1) child = [[] for _ in range(N+1)] seq = [] depth = [0] * (N+1) while que: v = que.popleft() seq.append(v) for u, c in adj[v]: if seen[u] == -1: seen[u] = seen[v] + 1 par[u] = (v, c) child[v].append(u) que.append(u) depth[u] = depth[v] + c seq.reverse() D = 0 for v in seq: if child[v]: continue D += depth[v] dp = [[D] * (K+1) for _ in range(N)] L = [0] * (N+1) E = [] for v in seq: if not child[v]: L[v] = 1 else: for u in child[v]: L[v] += L[u] if v != 1: p, c = par[v] E.append((c, c * L[v])) for i in range(N-1): w, v = E[i] for j in range(K+1): dp[i+1][j] = max(dp[i+1][j], dp[i][j]) if j + w <= K: dp[i+1][j+w] = max(dp[i+1][j+w], dp[i][j] + v) print(max(dp[-1])) if __name__ == '__main__': main()