結果

問題 No.1488 Max Score of the Tree
ユーザー ゆにぽけゆにぽけ
提出日時 2023-12-25 06:00:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,524 bytes
コンパイル時間 1,468 ms
コンパイル使用メモリ 136,456 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-25 06:00:37
合計ジャッジ時間 3,527 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
6,676 KB
testcase_01 AC 21 ms
6,676 KB
testcase_02 AC 22 ms
6,676 KB
testcase_03 AC 22 ms
6,676 KB
testcase_04 AC 22 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 8 ms
6,676 KB
testcase_07 AC 14 ms
6,676 KB
testcase_08 AC 11 ms
6,676 KB
testcase_09 AC 8 ms
6,676 KB
testcase_10 AC 15 ms
6,676 KB
testcase_11 AC 23 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 5 ms
6,676 KB
testcase_14 AC 13 ms
6,676 KB
testcase_15 AC 9 ms
6,676 KB
testcase_16 AC 4 ms
6,676 KB
testcase_17 AC 6 ms
6,676 KB
testcase_18 AC 16 ms
6,676 KB
testcase_19 AC 10 ms
6,676 KB
testcase_20 AC 6 ms
6,676 KB
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 8 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 7 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 4 ms
6,676 KB
testcase_29 AC 5 ms
6,676 KB
testcase_30 AC 19 ms
6,676 KB
testcase_31 AC 24 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cctype>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <functional>
using namespace std;
int N,K;
int ch[100];
vector<pair<int,int>> G[100];
void dfs(int u,int p,vector<pair<long long,long long>> &W,long long &ans)
{
	ch[u] = 0;
	if(p != -1 && G[u].size() == 1) ch[u] = 1;
	for(const auto &[v,w]:G[u]) if(v != p)
	{
		dfs(v,u,W,ans);
		ch[u] += ch[v];
		ans += (long long)w*ch[v];
		W.push_back(make_pair(w,(long long)ch[v]*w));
	}
}
void solve()
{
	cin >> N >> K;
	for(int i = 1;i < N;i++)
	{
		int u,v,w;
		cin >> u >> v >> w;
		u--; v--;
		G[u].push_back(make_pair(v,w));
		G[v].push_back(make_pair(u,w));
	}
	vector<pair<long long,long long>> W;
	long long ans = 0;
	dfs(0,-1,W,ans);
	vector<long long> dp(K+1,-(long long)1e18);
	dp[0] = 0;
	for(int i = 0;i < N-1;i++)
	{
		vector<long long> n_dp(K+1,-(long long)1e18);
		const auto &[w,v] = W[i];
		for(int j = 0;j <= K;j++)
		{
			n_dp[j] = max(n_dp[j],dp[j]);
			if(j+w <= K) n_dp[j+w] = max(n_dp[j+w],dp[j] + v);
		}
		swap(dp,n_dp);
	}
	long long mx = 0;
	for(int i = 0;i <= K;i++) mx = max(mx,dp[i]);
	ans += mx;
	cout << ans << "\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) solve();
}
0