結果
問題 |
No.1488 Max Score of the Tree
|
ユーザー |
|
提出日時 | 2022-10-15 07:40:46 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 21 ms / 2,000 ms |
コード長 | 1,319 bytes |
コンパイル時間 | 2,325 ms |
コンパイル使用メモリ | 201,904 KB |
最終ジャッジ日時 | 2025-02-08 06:17:38 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); // (w,v) = (c(e), leaf_cnt(e.c) * c(e)) int N,K; cin >> N >> K; vector<vector<pair<int,int>>> G(N); rep(i,N-1) { int a,b,c; cin >> a >> b >> c; a--; b--; G[a].push_back({b, c}); G[b].push_back({a, c}); } int ROOT = 0, SUM = 0; vector<int> leaf_cnt(N, 0); vector<pair<int,int>> wv; function<void(int,int)> get_leaf = [&](int v, int p) -> void { int is_leaf = 1; for(auto [to, c] : G[v]) { if(to != p) { is_leaf = 0; get_leaf(to, v); leaf_cnt[v] += leaf_cnt[to]; } } leaf_cnt[v] += is_leaf; for(auto [to, c] : G[v]) { if(to != p) { wv.push_back({c, leaf_cnt[to] * c}); SUM += leaf_cnt[to] * c; } } }; get_leaf(ROOT, -1); vector<int> dp(K + 1, 0); for(auto [w, v] : wv) { vector<int> nt = dp; for(int x = 0; x <= K; x++) if(x + w <= K) nt[x + w] = max(nt[x + w], dp[x] + v); swap(dp, nt); } cout << SUM + *max_element(dp.begin(), dp.end()) << endl; }