結果
| 問題 | No.1488 Max Score of the Tree |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-04-23 22:04:14 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 7 ms / 2,000 ms |
| コード長 | 1,659 bytes |
| 記録 | |
| コンパイル時間 | 1,029 ms |
| コンパイル使用メモリ | 110,544 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-04 08:06:12 |
| 合計ジャッジ時間 | 1,998 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
#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;
}