結果

問題 No.1488 Max Score of the Tree
ユーザー 夜
提出日時 2021-04-23 22:04:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,659 bytes
コンパイル時間 1,065 ms
コンパイル使用メモリ 106,356 KB
実行使用メモリ 4,404 KB
最終ジャッジ日時 2023-09-17 12:19:22
合計ジャッジ時間 2,363 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
#define int long long
vector<vector<pair<int, int>>> vec(110);
int co[110] = {}, co1[110] = {};
int a[110], b[110], c[110];
bool bo[110] = { false };
int dp[101000] = {};
signed main() {
	int n, k;
	cin >> n >> k;
	for (int i = 0; i <= k; i++) {
		dp[i] = -1000000007;
	}
	for (int i = 0; i < n - 1; i++) {
		cin >> a[i] >> b[i] >> c[i];
		vec[a[i]].emplace_back(make_pair(b[i], i));
		vec[b[i]].emplace_back(make_pair(a[i], i));
		co[a[i]]++, co[b[i]]++;
	}
	stack<pair<int,int>> s;
	s.push(make_pair(1,-1));
	set<int> st;
	bo[1] = true;
	int ans = 0;
	while (!s.empty()) {
		int now = s.top().first;
		bool bo1 = false;
		for (int i = 0; i < vec[now].size(); i++) {
			if (!bo[vec[now][i].first]) {
				bo[vec[now][i].first] = true;
				bo1 = true;
				st.insert(vec[now][i].second);
				s.push(make_pair(vec[now][i].first, vec[now][i].second));
				break;
			}
		}
		if (!bo1) {
			if (co[now] == 1) {
				for (auto itr = st.begin(); itr != st.end(); itr++) {
					co1[*itr]++;
					ans += c[*itr];
				}
			}
			st.erase(s.top().second);
			s.pop();
		}
	}
	dp[0] = 0;
	for (int i = 0; i < n - 1; i++) {
		for (int j = k; j >= c[i]; j--) {
			dp[j] = max(dp[j], dp[j - c[i]] + c[i] * co1[i]);
		}
	}
	int ans1 = -1;
	for (int i = 0; i <= k; i++) {
		ans1 = max(ans1, dp[i]);
	}
	cout << ans + ans1 << endl;
}
0