This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, int> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
#define INF 1000000005
#define LINF 1000000000000000005ll
#define MAXN 100005
int n, k;
vii adj[MAXN];
multiset<ll> st;
multiset<ll, greater<ll>> ot;
ll cur;
void insert(ll x) {
st.insert(x);
cur += x;
if (st.size() > k) {
cur -= *st.begin();
ot.insert(*st.begin());
st.erase(st.begin());
}
}
void erase(ll x) {
auto it = st.find(x);
if (it != st.end()) {
cur -= x;
st.erase(it);
if (!ot.empty()) {
cur += *ot.begin();
st.insert(*ot.begin());
ot.erase(ot.begin());
}
} else {
if (ot.find(x) == ot.end()) return;
ot.erase(ot.find(x));
}
}
ll mx[MAXN];
void dfs(int u, int p) {
mx[u] = 0;
bool leaf = 1;
for (auto [v, w] : adj[u]) {
if (v == p) continue;
leaf = 0;
dfs(v, u);
cerr << u << ' ' << v << '\n';
erase(mx[v]);
cerr << u << ' ' << v << '\n';
insert(mx[v] + w);
mxto(mx[u], mx[v] + w);
}
if (leaf) {
insert(0);
}
}
ll ans[MAXN];
void reroot(int u, int p) {
pll fmx = MP(0, u), smx = MP(0, u);
for (auto [v, w] : adj[u]) {
if (mx[v] + w >= fmx.FI) {
smx = fmx;
fmx = MP(mx[v] + w, v);
} else if (mx[v] + w > smx.FI) {
smx = MP(mx[v] + w, v);
}
}
cerr << u << ": " << cur << '\n';
for (int i : st) {
cerr << i << ' ';
}
cerr << '\n';
ans[u] = cur;
for (auto [v, w] : adj[u]) {
if (v == p) continue;
insert(mx[v]);
erase(mx[v] + w);
ll omx = mx[u];
if (v == fmx.SE) {
mx[u] = smx.FI;
} else {
mx[u] = fmx.FI;
}
insert(mx[u] + w);
erase(mx[u]);
reroot(v, u);
insert(mx[u]);
erase(mx[u] + w);
mx[u] = omx;
insert(mx[v] + w);
erase(mx[v]);
}
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> k;
REP (i, 1, n) {
int a, b, c; cin >> a >> b >> c;
adj[a].pb(MP(b, c));
adj[b].pb(MP(a, c));
}
dfs(1, -1);
reroot(1, -1);
REP (i, 1, n + 1) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void insert(ll)':
Main.cpp:46:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
46 | if (st.size() > k) {
| ~~~~~~~~~~^~~
# | 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... |