結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
![]() |
提出日時 | 2023-06-19 14:42:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 83 ms / 2,000 ms |
コード長 | 1,082 bytes |
コンパイル時間 | 2,251 ms |
コンパイル使用メモリ | 207,580 KB |
最終ジャッジ日時 | 2025-02-14 22:55:08 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;ll N, K;vector<vector<pair<ll, ll>>> E;vector<ll> subtree;vector<vector<ll>> dp;vector<pair<ll, ll>> D;ll subtree_size(ll from, ll p){ll cnt = 0;for (auto [to, C] : E[from]){if (to == p) continue;cnt += subtree_size(to, from);D.push_back({C, C*subtree[to]});}if (cnt == 0) cnt = 1;return subtree[from] = cnt;}int main(){ll A, B, C, x, y, ans=0;cin >> N >> K;E.resize(N);subtree.resize(N);for (int i=0; i < N-1; i++){cin >> A >> B >> C;A--; B--;E[A].push_back({B, C});E[B].push_back({A, C});}subtree_size(0, -1);vector<vector<ll>> dp(N, vector<ll>(K+1));for (int i=1; i<=N-1; i++){tie(x, y) = D[i-1];ans += y;for (int j=0; j<=K; j++){if (j-x>=0) dp[i][j] = max(dp[i][j], dp[i-1][j-x]+y);dp[i][j] = max(dp[i][j], dp[i-1][j]);}}cout << ans + dp[N-1][K] << endl;return 0;}