結果
問題 | No.1488 Max Score of the Tree |
ユーザー | sten_san |
提出日時 | 2021-04-23 22:44:13 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 214 ms / 2,000 ms |
コード長 | 3,150 bytes |
コンパイル時間 | 2,373 ms |
コンパイル使用メモリ | 225,636 KB |
実行使用メモリ | 239,544 KB |
最終ジャッジ日時 | 2024-07-04 08:26:55 |
合計ジャッジ時間 | 6,172 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 197 ms
229,240 KB |
testcase_01 | AC | 188 ms
219,972 KB |
testcase_02 | AC | 204 ms
231,688 KB |
testcase_03 | AC | 206 ms
239,544 KB |
testcase_04 | AC | 203 ms
239,488 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 51 ms
61,652 KB |
testcase_07 | AC | 124 ms
138,264 KB |
testcase_08 | AC | 83 ms
97,192 KB |
testcase_09 | AC | 60 ms
69,420 KB |
testcase_10 | AC | 122 ms
142,648 KB |
testcase_11 | AC | 210 ms
239,544 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 32 ms
37,120 KB |
testcase_14 | AC | 102 ms
115,776 KB |
testcase_15 | AC | 63 ms
72,940 KB |
testcase_16 | AC | 13 ms
15,952 KB |
testcase_17 | AC | 41 ms
46,508 KB |
testcase_18 | AC | 140 ms
160,732 KB |
testcase_19 | AC | 78 ms
90,256 KB |
testcase_20 | AC | 30 ms
37,996 KB |
testcase_21 | AC | 17 ms
20,748 KB |
testcase_22 | AC | 61 ms
71,148 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 48 ms
56,564 KB |
testcase_27 | AC | 8 ms
11,264 KB |
testcase_28 | AC | 21 ms
28,032 KB |
testcase_29 | AC | 29 ms
36,304 KB |
testcase_30 | AC | 165 ms
192,920 KB |
testcase_31 | AC | 214 ms
239,540 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } } iofast; struct uns_t {} uns; template <typename Element, typename Head, typename ...Args> auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template <typename Element, typename Head, typename ...Args> auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template <typename T, typename Compare = less<T>> T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); } template <typename T, typename Compare = less<T>> T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); } int main() { #define int int64_t int n, k; cin >> n >> k; auto g = vec<tuple<int, int>>(uns, n, 0); auto e = vec<tuple<int, int, int>>(uns, n - 1); for (int i = 0; i < n - 1; ++i) { int a, b, c; cin >> a >> b >> c; --a; --b; g[a].emplace_back(b, c); g[b].emplace_back(a, c); e[i] = { a, b, c }; } auto c = vec<int>(uns, n); auto d = vec<bool>(false, n, n); auto dfs = [&](int v, int p, auto &&dfs) -> int { auto &ans = c[v]; ans = 0; for (auto [next, _] : g[v]) { if (next == p) continue; ans += dfs(next, v, dfs); d[v][next] = true; } ans += ans == 0; return ans; }; dfs(0, -1, dfs); for (auto &[a, b, c] : e) { if (!d[a][b]) { swap(a, b); } } auto value = vec<int>(uns, n - 1); auto weight = vec<int>(uns, n - 1); for (int i = 0; i < n - 1; ++i) { auto [a, b, cost] = e[i]; value[i] = cost * c[b]; weight[i] = cost; } auto dp = vec<int64_t>(0, n, k + 1); auto prev = vec<tuple<int, int>>(uns, n, k + 1); for (int i = 1; i <= n - 1; ++i) { for (int j = 0; j <= k; ++j) { if (weight[i - 1] <= j) { if (auto v = dp[i - 1][j - weight[i - 1]] + value[i - 1]; dp[i][j] < v) { dp[i][j] = v; prev[i][j] = { i - 1, j - weight[i - 1] }; } } if (dp[i][j] < dp[i - 1][j]) { dp[i][j] = dp[i - 1][j]; prev[i][j] = { i - 1, j }; } } } auto choosed = vec<tuple<int, int>>(uns, 0); auto back = [&](int i, int j, auto &&back) -> void { if (i == 0 && j == 0) return; auto [pi, pj] = prev[i][j]; back(pi, pj, back); if (j != pj && weight[i - 1] <= j) { auto [a, b, c] = e[i - 1]; choosed.emplace_back(a, b); } }; back(n - 1, k, back); for (auto [a, b] : choosed) { for (auto &[c, d, cost] : e) { if (a == c && b == d) { cost *= 2; } } } int64_t ans = 0; for (auto [a, b, cost] : e) { ans += c[b] * cost; } cout << ans << endl; }