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 <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
#include <cstdint>
#include <cassert>
#include <functional>
#include <complex>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double
const ll maxn = 1e6 + 10, maxm = 3e5 + 1;
const ll mod = 1e9 + 7, cmod = 998244353, inf = 1e9, blcok = 400, p2 = 31;
const ld eps = 1e-9;
int n, k;
ll dp[maxn];
vector<pair<int, int>> g[maxn];
vector<ll> vec;
void dfs(int v, int p = 0){
dp[v] = 0;
for (auto to : g[v]){
int go = to.f, w = to.s;
if (go != p){
dfs(go, v);
dp[v] = max(dp[v], dp[go] + w);
}
}
int is = 0;
for (auto to : g[v]){
int go = to.f, w = to.s;
if (go != p){
if (dp[v] != dp[go] + w || is){
vec.pb(dp[go] + w);
}
else{
is = 1;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int u, v, c, i = 1; i < n; ++i){
cin >> u >> v >> c;
g[u].pb(mp(v, c));
g[v].pb(mp(u, c));
}
for (int i = 1; i <= n; ++i){
dfs(i);
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
ll ans = dp[i];
//cout << ans << '\n';
for (int i = 0; i < k - 1; ++i){
ans += vec[i];
// cout << vec[i] << " ";
}
cout << ans << " ";
vec.clear();
// exit(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... |