結果

問題 No.1488 Max Score of the Tree
ユーザー kwm_tkwm_t
提出日時 2021-04-24 18:05:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 1,583 bytes
コンパイル時間 1,556 ms
コンパイル使用メモリ 170,648 KB
実行使用メモリ 83,224 KB
最終ジャッジ日時 2023-09-17 13:42:23
合計ジャッジ時間 4,182 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
80,116 KB
testcase_01 AC 93 ms
77,028 KB
testcase_02 AC 99 ms
80,848 KB
testcase_03 AC 100 ms
83,224 KB
testcase_04 AC 101 ms
83,204 KB
testcase_05 AC 3 ms
6,116 KB
testcase_06 AC 27 ms
24,744 KB
testcase_07 AC 59 ms
49,908 KB
testcase_08 AC 42 ms
36,244 KB
testcase_09 AC 30 ms
27,808 KB
testcase_10 AC 60 ms
51,464 KB
testcase_11 AC 101 ms
83,160 KB
testcase_12 AC 3 ms
6,132 KB
testcase_13 AC 18 ms
17,480 KB
testcase_14 AC 49 ms
42,428 KB
testcase_15 AC 32 ms
28,580 KB
testcase_16 AC 8 ms
9,572 KB
testcase_17 AC 21 ms
19,548 KB
testcase_18 AC 67 ms
57,596 KB
testcase_19 AC 38 ms
33,952 KB
testcase_20 AC 17 ms
17,152 KB
testcase_21 AC 10 ms
10,888 KB
testcase_22 AC 31 ms
27,988 KB
testcase_23 AC 3 ms
6,000 KB
testcase_24 AC 3 ms
5,816 KB
testcase_25 AC 3 ms
6,132 KB
testcase_26 AC 24 ms
22,476 KB
testcase_27 AC 6 ms
8,456 KB
testcase_28 AC 13 ms
14,040 KB
testcase_29 AC 17 ms
16,720 KB
testcase_30 AC 83 ms
68,288 KB
testcase_31 AC 102 ms
83,208 KB
権限があれば一括ダウンロードができます

ソースコード

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