結果

問題 No.1488 Max Score of the Tree
ユーザー 沙耶花沙耶花
提出日時 2021-04-23 21:37:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,407 bytes
コンパイル時間 5,228 ms
コンパイル使用メモリ 268,392 KB
実行使用メモリ 5,200 KB
最終ジャッジ日時 2023-09-17 11:52:17
合計ジャッジ時間 6,057 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
4,676 KB
testcase_01 AC 19 ms
4,692 KB
testcase_02 AC 21 ms
5,200 KB
testcase_03 AC 20 ms
4,620 KB
testcase_04 AC 21 ms
4,696 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 7 ms
4,724 KB
testcase_07 AC 12 ms
4,676 KB
testcase_08 AC 10 ms
4,376 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 13 ms
4,796 KB
testcase_11 AC 21 ms
4,584 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 11 ms
4,376 KB
testcase_15 AC 8 ms
4,412 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 6 ms
4,376 KB
testcase_18 AC 14 ms
4,572 KB
testcase_19 AC 9 ms
4,404 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 4 ms
4,420 KB
testcase_22 AC 7 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 8 ms
4,920 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 5 ms
4,376 KB
testcase_30 AC 17 ms
4,892 KB
testcase_31 AC 20 ms
4,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

void dfs(int cur,int p,vector<vector<int>> &E,vector<int> &sz){
	sz[cur] = 0;
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		dfs(to,cur,E,sz);
		sz[cur] += sz[to];
	}
	if(sz[cur]==0)sz[cur] = 1;
}

int main(){
	
	int N,K;
	cin>>N>>K;
	
	map<pair<int,int>,long long> mp;
	vector<vector<int>> E(N);
	rep(i,N-1){
		int a,b,c;
		cin>>a>>b>>c;
		a--;b--;
		E[a].push_back(b);
		E[b].push_back(a);
		mp[make_pair(a,b)] = c;
		mp[make_pair(b,a)] = c;
	}
	
	vector<int> sz(N,-1);
	dfs(0,-1,E,sz);	
	
	vector<long long> w,v;
	
	for(auto a:mp){
		int x = a.first.first,y = a.first.second;
		if(x>y)continue;
		w.push_back(a.second);
		v.push_back((long long)a.second * min(sz[x],sz[y]));
	}
	
	
	vector<long long> dp(K+1,0LL);
	
	rep(i,w.size()){
		vector<long long> ndp(K+1,0LL);
		rep(j,K+1){
			ndp[j] = max(ndp[j],dp[j]);
			if(j+w[i]<=K){
				ndp[j+w[i]] = max(ndp[j+w[i]],dp[j] + v[i]);
			}
		}
		swap(dp,ndp);
	}
	
	long long ans = 0LL;
	rep(i,K+1){
		ans = max(ans,dp[i]);
	}
	
	for(auto a:mp){
		int x = a.first.first,y = a.first.second;
		if(x>y)continue;
		long long X = a.second;
		ans += (long long)a.second * min(sz[x],sz[y]);
	}
	
	cout<<ans<<endl;
	
    return 0;
}
0