結果

問題 No.1488 Max Score of the Tree
ユーザー kwm_t
提出日時 2021-04-24 18:05:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 1,583 bytes
コンパイル時間 1,542 ms
コンパイル使用メモリ 172,504 KB
実行使用メモリ 84,804 KB
最終ジャッジ日時 2024-07-04 09:10:00
合計ジャッジ時間 3,835 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
//#include "atcoder/all"
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
int child[100];
struct Edge {
	int to, dist;
	Edge(int _to, int _dist) :to(_to), dist(_dist) {}
};
long long deep;
vector<Edge>g[100005];
void dfs(int v, int p = -1) {
	int num = 0;
	if (1 == g[v].size()) num = 1;
	for (Edge e : g[v]) {
		if (p == e.to) continue;
		dfs(e.to, v);
		num += child[e.to];
		deep += child[e.to] * e.dist;
	}
	child[v] = num;
}
long long dp[105][100005];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, k; cin >> n >> k;
	vector<long long>a(n - 1), b(n - 1), c(n - 1);
	rep(i, n - 1) {
		cin >> a[i] >> b[i] >> c[i];
		a[i]--; b[i]--;
		g[a[i]].emplace_back(b[i], c[i]);
		g[b[i]].emplace_back(a[i], c[i]);
	}
	deep = 0;
	dfs(0);
	rep(i, n - 1) {
		int add = min(child[a[i]], child[b[i]])*c[i];
		rep(j, k + 1) {
			if (j + c[i] <= k) {
				dp[i + 1][j + c[i]] = max(dp[i + 1][j + c[i]], dp[i][j] + add);
			}
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
		}
	}
	long long ans = 0;
	rep(i, k + 1) ans = max(ans, dp[n - 1][i]);
	cout << deep + ans << endl;
	return 0;
}
0