#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(), v.end()
#define pb push_back
#define F first
#define S second
const int N = 200010;
vector<pair<int,int>> adj[N];
vector<int> dep(N,0);
int root;
int dfs(int node,int p){
if(adj[node].size() == 1 && node != root) return node;
int res = 0,maxdep = -1;
for(auto pi : adj[node]){
int go = pi.F;
int c = pi.S;
if(go != p){
int curr = dfs(go,node);
dep[curr] += c;
if(maxdep < dep[curr]){
maxdep = dep[curr];
res = curr;
}
}
}
return res;
}
void _(){
int n,k;
cin >> n >> k;
for(int i = 0; i < n - 1; i++){
int u,v,w;
cin >> u >> v >> w;
u--,v--;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
/*dfs(0, -1);
for(int i = 0; i < n; i++){
cout << dep[i] << " ";
}*/
for(int i = 0; i < n; i++){
root = i;
dep.assign(n,0);
dfs(i,-1);
sort(all(dep),greater<int>());
int ans = 0;
for(int i = 0; i < k; i++) ans += dep[i];
cout << ans << endl;
}
}
int32_t main()
{
int t = 1;
//cin >> t;
while(t--) _();
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... |