#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
using ll = long long;
//struct node {
//node* l = nullptr;
//node* r = nullptr;
//ll val = 0;
//ll sm = 0;
//};
//node* copy(node* nd) {
//node* v = new node();
//v->l = nd->l;
//v->r = nd->r;
//v->val = nd->val;
//return v;
//}
//node* merge(node* l, node* r) {
//if (!l)
//return r;
//if (!r)
//return l;
//}
const int MAXN = 1e5 + 123;
vector<pair<int, int>> edg[MAXN];
ll cost[MAXN * 3];
pair<int, int> edges[MAXN * 3];
vector<ll> dp[MAXN * 3];
void get_dp(int e, int k) {
if (dp[e].size())
return;
auto [rt, v] = edges[e];
if (k == 1)
dp[e] = {0};
for (auto [u, ne] : edg[v]) {
if (u == rt)
continue;
get_dp(ne, k);
if (k == 1) {
dp[e][0] = max(dp[e][0], dp[ne][0]);
continue;
}
vector<ll> ndp(dp[e].size() + dp[ne].size());
merge(dp[e].rbegin(), dp[e].rend(), dp[ne].rbegin(), dp[ne].rend(), ndp.rbegin());
ndp.resize(min(int(ndp.size()), k));
dp[e] = ndp;
}
if (dp[e].empty()) {
dp[e] = {cost[e]};
} else {
dp[e][0] += cost[e];
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, k;
cin >> n >> k;
assert(k == 1);
for (int i = 0; i + 1 < n; ++i) {
int u, v, c;
cin >> u >> v >> c;
--u, --v;
edg[u].push_back({v, i * 2});
edg[v].push_back({u, i * 2 + 1});
edges[i * 2] = {u, v};
edges[i * 2 + 1] = {v, u};
cost[i * 2] = cost[i * 2 + 1] = c;
}
for (int e = 2 * n; e < 3 * n; ++e) {
edges[e] = {n, e - 2 * n};
get_dp(e, k);
ll ans = 0;
for (auto x : dp[e])
ans += x;
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
19540 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
19540 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
19540 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
19540 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
27848 KB |
Output is correct |
2 |
Correct |
151 ms |
30856 KB |
Output is correct |
3 |
Correct |
101 ms |
27564 KB |
Output is correct |
4 |
Execution timed out |
1057 ms |
20536 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
19540 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |