#include <bits/stdc++.h>
using namespace std;
#define int int64_t
void dfs(int s, int p, vector<vector<pair<int, int>>> const &adj,
vector<int> &submax, vector<int> &a) {
for (auto &[to, cost] : adj[s])
if (to != p) {
dfs(to, s, adj, submax, a);
submax[s] = max(submax[s], submax[to] + cost);
}
bool flag = false;
for (auto &[to, cost] : adj[s])
if (to != p) {
if (submax[to] + cost != submax[s] || flag)
a.push_back(submax[to] + cost);
else
flag = true;
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, k;
cin >> n >> k;
vector<vector<pair<int, int>>> adj(n);
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
for (int i = 0; i < n; i++) {
vector<int> submax(n, 0), a;
dfs(i, -1, adj, submax, a);
a.push_back(submax[i]);
sort(a.begin(), a.end(), greater<int>());
int ans = 0;
for (int j = 0; j < k; j++)
ans += a[i];
cout << ans << '\n';
}
}
| # | 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... |