#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5;
ll weight[mn], dist[mn], par[mn];
vector<int> dfsOrder;
vector<pii> adj[mn];
bool pick[mn];
void dfs (int u, int p) {
par[u] = p, dfsOrder.push_back(u);
for (pii item : adj[u]) {
int v, w; tie(v, w) = item;
if (v != p) weight[v] = w, dfs(v, u);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
for (int i = 1; i < n; i++) {
int a, b, c; cin >> a >> b >> c;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
for (int root = 1; root <= n; root++) {
// reset values
fill(pick + 1, pick + 1 + n, 0);
fill(weight + 1, weight + 1 + n, 0);
// run dfs + get dfs order
dfsOrder.clear(), dfs(root, 0);
assert(dfsOrder.size() == n);
// pick k times
ll ans = 0;
for (int i = 0; i < k; i++) {
fill(dist + 1, dist + 1 + n, 0);
for (int u : dfsOrder) dist[u] = dist[par[u]] + weight[u];
int choosen = max_element(dist + 1, dist + 1 + n) - dist;
ans += dist[choosen];
for (int u = choosen; u != par[u] && !pick[u]; u = par[u]) weight[u] = 0, pick[u] = 1;
}
cout << ans << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |