結果

問題 No.1488 Max Score of the Tree
ユーザー startcppstartcpp
提出日時 2021-04-23 22:33:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,627 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 73,964 KB
実行使用メモリ 81,792 KB
最終ジャッジ日時 2023-09-17 12:39:22
合計ジャッジ時間 3,312 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
79,112 KB
testcase_01 AC 48 ms
77,104 KB
testcase_02 AC 50 ms
81,200 KB
testcase_03 AC 52 ms
81,544 KB
testcase_04 AC 52 ms
81,524 KB
testcase_05 AC 12 ms
54,556 KB
testcase_06 AC 16 ms
31,780 KB
testcase_07 AC 32 ms
58,472 KB
testcase_08 AC 23 ms
39,996 KB
testcase_09 AC 25 ms
71,712 KB
testcase_10 AC 32 ms
54,436 KB
testcase_11 AC 52 ms
81,792 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 20 ms
71,384 KB
testcase_14 AC 28 ms
50,320 KB
testcase_15 AC 19 ms
39,724 KB
testcase_16 AC 6 ms
12,652 KB
testcase_17 AC 13 ms
25,288 KB
testcase_18 AC 39 ms
72,716 KB
testcase_19 AC 22 ms
39,920 KB
testcase_20 AC 14 ms
40,964 KB
testcase_21 AC 6 ms
13,024 KB
testcase_22 AC 22 ms
53,548 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
5,444 KB
testcase_25 AC 3 ms
13,596 KB
testcase_26 AC 13 ms
21,872 KB
testcase_27 AC 7 ms
26,156 KB
testcase_28 AC 14 ms
42,716 KB
testcase_29 AC 14 ms
36,884 KB
testcase_30 AC 42 ms
69,152 KB
testcase_31 AC 52 ms
81,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int n, k;
int from[100], to[100], cst[100];
vector<int> et[100];
vector<int> ec[100];
int chnum[100];
int parent[100];

int dfs(int p, int v) {
	parent[v] = p;
	if (et[v].size() == 1 && p != -1) return chnum[v] = 1;
	
	int i;
	int ret = 0;
	rep(i, et[v].size()) {
		int nv = et[v][i];
		if (nv == p) continue;
		ret += dfs(v, nv);
	}
	return chnum[v] = ret;
}

int dfs2(int p, int v) {
	if (et[v].size() == 1 && p != -1) { return 0; }
	
	int i;
	int ret = 0;
	rep(i, et[v].size()) {
		int nv = et[v][i];
		if (nv == p) continue;
		ret += dfs2(v, nv) + ec[v][i] * chnum[nv];
	}
	return ret;
}

int dp[101][100001];

signed main() {
	int i, j;
	
	cin >> n >> k;
	rep(i, n - 1) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		from[i] = a; to[i] = b; cst[i] = c;
		et[a].push_back(b);
		et[b].push_back(a);
		ec[a].push_back(c);
		ec[b].push_back(c);
	}
	
	dfs(-1, 0);
	
	vector<int> weight(n - 1), value(n - 1);
	rep(i, n - 1) {
		weight[i] = cst[i];
		value[i] = min(chnum[from[i]], chnum[to[i]]) * cst[i];
		//cout << weight[i] << ", " << value[i] << endl;
	}
	
	int INF = 1e+15;
	rep(i, n) rep(j, k + 1) dp[i][j] = -INF;
	
	dp[0][0] = 0;
	rep(i, n - 1) {
		rep(j, k + 1) {
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
			if (j + weight[i] <= k) {
				dp[i + 1][j + weight[i]] = max(dp[i + 1][j + weight[i]], dp[i][j] + value[i]);
			}
		}
	}
	
	int ans = -INF;
	rep(i, k + 1) ans = max(ans, dp[n - 1][i]);
	
	int ans2 = dfs2(-1, 0);
	
	cout << ans + ans2 << endl;
	return 0;
}
0