結果

問題 No.1488 Max Score of the Tree
ユーザー kotatsugame
提出日時 2021-04-24 03:36:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 736 bytes
コンパイル時間 724 ms
コンパイル使用メモリ 74,148 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-04 08:57:01
合計ジャッジ時間 1,789 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   29 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int N,K;
vector<pair<int,int> >G[100];
vector<pair<int,int> >E;
int ans=0;
int dfs(int u,int p)
{
	int ch=0;
	bool isr=true;
	for(pair<int,int>e:G[u])
	{
		int v=e.first;
		if(v!=p)
		{
			int c=dfs(v,u);
			ans+=c*e.second;
			E.push_back(make_pair(e.second,c));
			ch+=c;
			isr=false;
		}
	}
	if(isr)ch=1;
	return ch;
}
int dp[1<<17];
main()
{
	cin>>N>>K;
	for(int i=1;i<N;i++)
	{
		int a,b,c;cin>>a>>b>>c;
		a--,b--;
		G[a].push_back(make_pair(b,c));
		G[b].push_back(make_pair(a,c));
	}
	dfs(0,-1);
	for(pair<int,int>e:E)
	{
		int c=e.second;
		int k=e.first;
		for(int i=K;i>=k;i--)
		{
			dp[i]=max(dp[i],dp[i-k]+c*k);
		}
	}
	cout<<ans+dp[K]<<endl;
}
0