結果

問題 No.1488 Max Score of the Tree
ユーザー umezoumezo
提出日時 2021-04-24 02:03:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,084 bytes
コンパイル時間 2,289 ms
コンパイル使用メモリ 210,248 KB
実行使用メモリ 110,052 KB
最終ジャッジ日時 2023-09-17 13:20:48
合計ジャッジ時間 8,576 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

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 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){
    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];
  }
  
  map<int,int> m;
  m[0]=0;
  rep(i,n-1){
    map<int,int> mm;
    for(auto x:m){
      mm[x.first]=max(x.second,mm[x.first]);
      mm[x.first+C[i]]=max(mm[x.first+C[i]],x.second+D[i]);
    }
    m=mm;
  }
  
  int ma=0;
  for(auto x:m){
    if(x.first<=k) ma=max(ma,x.second);
  }
  cout<<ans+ma<<endl;
  
  return 0;
}
0