// 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
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) {
| ~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
4 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
4 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2700 KB |
Output is correct |
13 |
Correct |
4 ms |
2832 KB |
Output is correct |
14 |
Correct |
5 ms |
2920 KB |
Output is correct |
15 |
Correct |
4 ms |
2844 KB |
Output is correct |
16 |
Correct |
5 ms |
2856 KB |
Output is correct |
17 |
Correct |
5 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
5 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
11404 KB |
Output is correct |
2 |
Correct |
236 ms |
13660 KB |
Output is correct |
3 |
Correct |
193 ms |
9164 KB |
Output is correct |
4 |
Correct |
224 ms |
11524 KB |
Output is correct |
5 |
Correct |
239 ms |
12400 KB |
Output is correct |
6 |
Correct |
242 ms |
11568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
3 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
4 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2700 KB |
Output is correct |
13 |
Correct |
4 ms |
2832 KB |
Output is correct |
14 |
Correct |
5 ms |
2920 KB |
Output is correct |
15 |
Correct |
4 ms |
2844 KB |
Output is correct |
16 |
Correct |
5 ms |
2856 KB |
Output is correct |
17 |
Correct |
5 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
5 ms |
2816 KB |
Output is correct |
20 |
Correct |
256 ms |
11404 KB |
Output is correct |
21 |
Correct |
236 ms |
13660 KB |
Output is correct |
22 |
Correct |
193 ms |
9164 KB |
Output is correct |
23 |
Correct |
224 ms |
11524 KB |
Output is correct |
24 |
Correct |
239 ms |
12400 KB |
Output is correct |
25 |
Correct |
242 ms |
11568 KB |
Output is correct |
26 |
Correct |
272 ms |
12364 KB |
Output is correct |
27 |
Correct |
288 ms |
15600 KB |
Output is correct |
28 |
Correct |
267 ms |
16020 KB |
Output is correct |
29 |
Correct |
191 ms |
11340 KB |
Output is correct |
30 |
Correct |
260 ms |
13912 KB |
Output is correct |
31 |
Correct |
281 ms |
12364 KB |
Output is correct |
32 |
Correct |
301 ms |
14700 KB |
Output is correct |
33 |
Correct |
308 ms |
13872 KB |
Output is correct |
34 |
Correct |
169 ms |
11124 KB |
Output is correct |
35 |
Correct |
324 ms |
13932 KB |
Output is correct |
36 |
Correct |
249 ms |
16052 KB |
Output is correct |