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>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, k;
vector<pair<int, int>> adj[maxN];
int f[maxN];
void ReadInput()
{
cin >> n >> k;
for(int i=1; i<n; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
}
void predfs(int u, int par)
{
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(v == par) continue;
predfs(v, u);
f[u] = max(f[u], f[v] + w);
}
}
vector<int> vc;
void dfs(int u, int par, int root)
{
int _max = 0, id = 0;
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(v == par) continue;
dfs(v, u, root);
//if(u == 3) cout << f[4] << " ";
if(f[v] + w > _max)
{
_max = f[v] + w;
id = v;
}
}
// cout << u << " " << _max << "\n";
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(u != root && (v == par || v == id)) continue;
vc.pb(f[v] + w);
}
}
void Solve()
{
for(int i=1; i<=n; i++)
{
fill(f, f + n + 1, 0);
vc.clear();
predfs(i, 0);
dfs(i, 0, i);
sort(vc.begin(), vc.end(), greater<int>());
int res = 0;
for(int i=0; i<k; i++) res += vc[i];
cout << res << '\n';
}
}
int32_t main()
{
// freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# | 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... |