結果

問題 No.1488 Max Score of the Tree
ユーザー umezoumezo
提出日時 2021-04-24 02:15:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,096 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 201,460 KB
実行使用メモリ 46,624 KB
最終ジャッジ日時 2023-09-17 13:20:57
合計ジャッジ時間 3,986 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
46,384 KB
testcase_01 AC 39 ms
46,356 KB
testcase_02 AC 39 ms
46,396 KB
testcase_03 AC 40 ms
46,328 KB
testcase_04 AC 39 ms
46,460 KB
testcase_05 AC 15 ms
46,448 KB
testcase_06 AC 18 ms
46,456 KB
testcase_07 AC 29 ms
46,592 KB
testcase_08 AC 21 ms
46,332 KB
testcase_09 AC 22 ms
46,336 KB
testcase_10 AC 27 ms
46,340 KB
testcase_11 AC 40 ms
46,392 KB
testcase_12 AC 15 ms
46,376 KB
testcase_13 AC 18 ms
46,608 KB
testcase_14 AC 26 ms
46,336 KB
testcase_15 AC 21 ms
46,328 KB
testcase_16 AC 16 ms
46,320 KB
testcase_17 AC 18 ms
46,332 KB
testcase_18 AC 30 ms
46,348 KB
testcase_19 AC 22 ms
46,344 KB
testcase_20 AC 18 ms
46,332 KB
testcase_21 AC 16 ms
46,444 KB
testcase_22 AC 21 ms
46,380 KB
testcase_23 AC 14 ms
46,624 KB
testcase_24 AC 14 ms
46,392 KB
testcase_25 AC 15 ms
46,332 KB
testcase_26 AC 18 ms
46,392 KB
testcase_27 AC 15 ms
46,336 KB
testcase_28 AC 16 ms
46,356 KB
testcase_29 AC 18 ms
46,344 KB
testcase_30 AC 31 ms
46,332 KB
testcase_31 AC 39 ms
46,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

vector<int> G[110];
int dep[110];
int chi[110];
int A[110],B[110],C[110],D[110];
int dp[110][100010];

int dfs(int v,int p,int d){
  dep[v]=d;
  int tmp=0;
  if(G[v].size()==1 && v!=0){
    return chi[v]=1;
  }
  for(auto nv:G[v]){
    if(nv==p) continue;
    tmp+=dfs(nv,v,d+1);
  }
  return chi[v]=tmp;
}

int main(){
  int n,k;
  cin>>n>>k;
  
  int ans=0;
  rep(i,n-1){
    int a,b,c;
    cin>>a>>b>>c;
    a--,b--;
    A[i]=a,B[i]=b,C[i]=c;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  dfs(0,-1,0);
  rep(i,n-1){
    if(dep[A[i]]<dep[B[i]]) D[i]=C[i]*chi[B[i]];
    else D[i]=C[i]*chi[A[i]];
    ans+=D[i];
  }
  
  memset(dp,-1,sizeof(dp));
  dp[0][0]=0;
  rep(i,n){
    rep(j,k+1){
      if(dp[i][j]==-1) continue;
      dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
      if(j+C[i]<=k) dp[i+1][j+C[i]]=max(dp[i+1][j+C[i]],dp[i][j]+D[i]);
    }
  }
  int ma=0;
  rep(j,k+1) ma=max(ma,dp[n][j]);
  cout<<ans+ma<<endl;

  return 0;
}
0