This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <fstream>
#define endl '\n'
#define mod 998244353
#define INF 1000000000000000000
#define ll long long
///#define cin fin
///#define cout fout
#define fi first
#define se second
using namespace std;
///ofstream fout("herding.out");
///ifstream fin("herding.in");
const int N = 2e3 + 5;
vector<pair<int,long long>> v[N];
long long mx[N];
priority_queue<long long> pq;
void dfs(int s, int p) {
pair<int,long long> fin = {-1,-1};
for(auto i : v[s]) {
if(i.first != p) { dfs(i.first,s);
if(fin.second < mx[i.first]+i.second) {
fin.first = i.first;
fin.second = mx[i.first]+i.second;
}
}
}
for(auto i : v[s]) {
if(i.first != p && i.first != fin.first) {
pq.push(mx[i.first]+i.second);
}
}
if(fin.second != -1) mx[s] = fin.second;
if(p == 0) pq.push(fin.second);
}
int main()
{
ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
int n, k; cin >> n >> k;
for(int i = 0; i < n-1; i++) {
int a, b, c; cin >> a >> b >> c;
v[a].push_back({b,c});
v[b].push_back({a,c});
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) mx[j] = 0;
while(!pq.empty())pq.pop();
long long ans = 0; int num = k;
dfs(i,0);
while(num != 0 && !pq.empty()) {
ans += pq.top();
pq.pop(); num--;
}
cout << ans << endl;
}
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... |