結果

問題 No.1488 Max Score of the Tree
ユーザー 👑 kmjpkmjp
提出日時 2021-04-23 23:13:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,452 bytes
コンパイル時間 2,122 ms
コンパイル使用メモリ 206,240 KB
実行使用メモリ 7,816 KB
最終ジャッジ日時 2023-09-17 13:05:55
合計ジャッジ時間 3,536 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
7,696 KB
testcase_01 AC 9 ms
7,696 KB
testcase_02 AC 9 ms
7,668 KB
testcase_03 AC 9 ms
7,668 KB
testcase_04 AC 9 ms
7,676 KB
testcase_05 AC 4 ms
6,952 KB
testcase_06 AC 5 ms
7,460 KB
testcase_07 AC 7 ms
7,580 KB
testcase_08 AC 6 ms
7,664 KB
testcase_09 AC 5 ms
7,336 KB
testcase_10 AC 7 ms
7,580 KB
testcase_11 AC 9 ms
7,740 KB
testcase_12 AC 3 ms
7,076 KB
testcase_13 AC 4 ms
6,992 KB
testcase_14 AC 7 ms
7,568 KB
testcase_15 AC 5 ms
7,436 KB
testcase_16 AC 4 ms
7,192 KB
testcase_17 AC 5 ms
7,400 KB
testcase_18 AC 7 ms
7,480 KB
testcase_19 AC 6 ms
7,816 KB
testcase_20 AC 5 ms
7,224 KB
testcase_21 AC 4 ms
7,408 KB
testcase_22 AC 5 ms
7,268 KB
testcase_23 AC 3 ms
7,128 KB
testcase_24 AC 4 ms
6,868 KB
testcase_25 AC 3 ms
6,888 KB
testcase_26 AC 6 ms
7,692 KB
testcase_27 AC 3 ms
7,000 KB
testcase_28 AC 4 ms
7,064 KB
testcase_29 AC 4 ms
7,324 KB
testcase_30 AC 8 ms
7,648 KB
testcase_31 AC 10 ms
7,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------


int N,K;
vector<pair<int,int>> E[101010];
ll D[202020];
int C[202020];
vector<pair<int,ll>> V;
ll ret=0;
int dfs(int cur,int pre,ll d) {
	D[cur]=d;
	if(cur&&E[cur].size()==1) {
		C[cur]=1;
		ret+=d;
	}
	ll sum=0;
	int up=0;
	FORR2(e,c,E[cur]) if(e==pre) up=c;
	FORR2(e,c,E[cur]) if(e!=pre) {
		C[cur]+=dfs(e,cur,d+c);
	}
	V.push_back({up,1LL*C[cur]*up});
	return C[cur];
	
}

ll dp[101010];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N-1) {
		cin>>x>>y>>r;
		E[x-1].push_back({y-1,r});
		E[y-1].push_back({x-1,r});
	}
	dfs(0,0,0);
	FORR2(c,v,V) {
		for(i=K;i>=c;i--) dp[i]=max(dp[i],dp[i-c]+v);
	}
	cout<<ret+dp[K]<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0