#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const int MAXN = 1e4 + 123;
vector<pair<int, int>> edg[MAXN];
ll cost[MAXN * 2];
pair<int, int> edges[MAXN * 2];
vector<ll> dp[MAXN * 2];
void get_dp(int e, int k) {
if (dp[e].size())
return;
auto [rt, v] = edges[e];
for (auto [u, ne] : edg[v]) {
if (u == rt)
continue;
get_dp(ne, k);
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() {
int n, k;
cin >> n >> k;
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 = 0; e < n * 2 - 1; ++e)
get_dp(e, k);
for (int v = 0; v < n; ++v) {
vector<ll> ans;
for (auto [u, e] : edg[v]) {
vector<ll> ndp(ans.size() + dp[e].size());
merge(dp[e].rbegin(), dp[e].rend(), ans.rbegin(), ans.rend(), ndp.rbegin());
ndp.resize(min(int(ndp.size()), k));
ans = ndp;
}
ll anssm = 0;
for (auto x : ans)
anssm += x;
cout << anssm << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1008 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1008 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
1876 KB |
Output is correct |
9 |
Correct |
3 ms |
1748 KB |
Output is correct |
10 |
Correct |
3 ms |
1492 KB |
Output is correct |
11 |
Correct |
49 ms |
1852 KB |
Output is correct |
12 |
Correct |
3 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1008 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
1876 KB |
Output is correct |
9 |
Correct |
3 ms |
1748 KB |
Output is correct |
10 |
Correct |
3 ms |
1492 KB |
Output is correct |
11 |
Correct |
49 ms |
1852 KB |
Output is correct |
12 |
Correct |
3 ms |
1748 KB |
Output is correct |
13 |
Correct |
19 ms |
11092 KB |
Output is correct |
14 |
Correct |
9 ms |
4356 KB |
Output is correct |
15 |
Correct |
4 ms |
1748 KB |
Output is correct |
16 |
Execution timed out |
684 ms |
9200 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1748 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1008 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
1876 KB |
Output is correct |
9 |
Correct |
3 ms |
1748 KB |
Output is correct |
10 |
Correct |
3 ms |
1492 KB |
Output is correct |
11 |
Correct |
49 ms |
1852 KB |
Output is correct |
12 |
Correct |
3 ms |
1748 KB |
Output is correct |
13 |
Correct |
19 ms |
11092 KB |
Output is correct |
14 |
Correct |
9 ms |
4356 KB |
Output is correct |
15 |
Correct |
4 ms |
1748 KB |
Output is correct |
16 |
Execution timed out |
684 ms |
9200 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |