結果

問題 No.1488 Max Score of the Tree
ユーザー 👑 NachiaNachia
提出日時 2021-04-23 22:01:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,033 bytes
コンパイル時間 1,544 ms
コンパイル使用メモリ 88,244 KB
実行使用メモリ 5,164 KB
最終ジャッジ日時 2023-09-17 12:14:01
合計ジャッジ時間 3,179 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
5,164 KB
testcase_01 AC 12 ms
5,064 KB
testcase_02 AC 13 ms
4,520 KB
testcase_03 AC 13 ms
5,100 KB
testcase_04 AC 14 ms
5,072 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 5 ms
4,472 KB
testcase_07 AC 8 ms
4,504 KB
testcase_08 AC 7 ms
4,844 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 13 ms
5,104 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 7 ms
4,716 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 8 ms
4,436 KB
testcase_19 AC 7 ms
4,804 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 5 ms
4,460 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 6 ms
4,584 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 11 ms
4,640 KB
testcase_31 AC 14 ms
5,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0; i<(n); i++)

struct Edge{ int to, d; };

int N, K;
vector<vector<Edge>> E;
vector<int> I;
vector<int> P;
vector<int> D;
vector<int> Z;

int main(){
  scanf("%d%d",&N,&K);
  E.resize(N);
  rep(i,N-1){
    int u,v,d; scanf("%d%d%d",&u,&v,&d); u--; v--;
    E[u].push_back({v,d});
    E[v].push_back({u,d});
  }


  P.assign(N,-1);
  D.assign(N,0);
  Z.assign(N,1);
  queue<int> Q;
  Q.push(0);
  while(Q.size()){
    int p = Q.front(); Q.pop();
    I.push_back(p);
    for(Edge e : E[p]) if(P[p] != e.to){
      P[e.to] = p;
      D[e.to] = e.d;
      Q.push(e.to);
      Z[p] = 0;
    }
  }
  for(int i=N-1; i>=1; i--){ int p=I[i]; Z[P[p]]+=Z[p]; }
  
  ll ans = 0;
  rep(i,N) ans += (ll)Z[i] * D[i];
  vector<ll> dp(K+1,0);
  rep(i,N){
    vector<ll> tmp = dp;
    for(int k=D[i]; k<=K; k++) tmp[k] = max(tmp[k],dp[k-D[i]]+Z[i]*D[i]);
    dp = move(tmp);
  }

  printf("%lld\n",ans+dp[K]);

  return 0;
}
0