結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
|
提出日時 | 2021-04-23 21:34:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 19 ms / 2,000 ms |
コード長 | 1,517 bytes |
コンパイル時間 | 1,724 ms |
コンパイル使用メモリ | 106,592 KB |
最終ジャッジ日時 | 2025-01-20 23:37:46 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:33:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 33 | scanf("%d %d %d", &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208; template <class F> struct REC { F f; REC(F &&f_) : f(std::forward<F>(f_)) {} template <class... Args> auto operator()(Args &&... args) const { return f(*this, std::forward<Args>(args)...); } }; int main() { int n, k; cin >> n >> k; vector<vector<pair<int, int>>> G(n); for (int i = 0; i < n-1; ++i) { int a, b, c; scanf("%d %d %d", &a, &b, &c); a--; b--; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } vector<ll> cnt(n, 0), val(n); int sum = 0; REC([&](auto &&f, int x, int p, int d) -> void { int leaf = 1; for (auto &&[y, c] : G[x]) { if(y == p) continue; val[y] = c; f(y, x, d+c); cnt[x] += cnt[y]; leaf = 0; } if(leaf) cnt[x]++, sum += d; })(0, -1, 0); vector<ll> dp(k+1, -INF<ll>); dp[0] = 0; for (int i = 1; i < n; ++i) { ll c = val[i], v = cnt[i]*val[i]; for (ll j = k-c; j >= 0; --j) { dp[j+c] = max(dp[j+c], dp[j]+v); } } cout << sum+*max_element(dp.begin(),dp.end()) << "\n"; return 0; }