#include <bits/stdc++.h>
using namespace std;
const int kN = 1e5;
int n, k;
int64_t sumK, sol[1 + kN];
pair<int64_t, int> dmax[1 + kN];
vector<pair<int, int>> g[1 + kN];
set<pair<int64_t, int>> paths[1 + kN];
multiset<int64_t> low, big;
void fix() {
while ((int)big.size() < k && !low.empty()) {
auto it = --low.end();
big.emplace(*it);
sumK += *it;
low.erase(it);
}
if (low.empty() || big.empty()) {
return;
}
int64_t last = *(--low.end()), first = *big.begin();
while (first < last) {
big.erase(big.begin());
low.erase(--low.end());
low.emplace(first);
sumK -= first;
big.emplace(last);
sumK += last;
last = *(--low.end());
first = *big.begin();
}
}
void add(int64_t x) {
if ((int)big.size() < k) {
big.emplace(x);
sumK += x;
} else {
low.emplace(x);
fix();
}
}
void rem(int64_t x) {
if (low.count(x)) {
low.erase(low.find(x));
} else {
big.erase(big.find(x));
sumK -= x;
fix();
}
}
void dfs1(int u, int par) {
for (auto it : g[u]) {
int v, w;
tie(v, w) = it;
if (v != par) {
dfs1(v, u);
if (paths[v].empty()) {
paths[u].emplace(w, v);
} else {
paths[u].emplace((--paths[v].end())->first + w, v);
}
}
}
}
void dfs2(int u, int par) {
sol[u] = sumK;
for (auto it : g[u]) {
int v, w;
tie(v, w) = it;
if (v != par) {
auto it1 = --paths[u].end();
pair<int64_t, int> path1, path2{dmax[v].first + w, v}, removed{-1, -1};
if (it1->second == v) {
if ((int)paths[u].size() == 1) {
path1 = {w, u};
} else {
auto it2 = --it1;
path1 = {it2->first + w, u};
removed = *it2;
}
} else {
path1 = {it1->first + w, u};
removed = *it1;
}
paths[v].emplace(path1);
add(path1.first);
if (removed.first != -1) {
paths[u].erase(removed);
rem(removed.first);
}
paths[u].erase(path2);
rem(path2.first);
paths[v].emplace(dmax[v]);
add(dmax[v].first);
dfs2(v, u);
paths[v].erase(path1);
rem(path1.first);
if (removed.first != -1) {
paths[u].emplace(removed);
add(removed.first);
}
paths[u].emplace(path2);
add(path2.first);
paths[v].erase(dmax[v]);
rem(dmax[v].first);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
dfs1(1, 0);
for (int i = 1; i <= n; ++i) {
if (!paths[i].empty()) {
auto it = *(--paths[i].end());
dmax[i] = it;
if (i != 1) {
paths[i].erase(it);
}
}
for (auto it : paths[i]) {
add(it.first);
}
}
dfs2(1, 0);
for (int i = 1; i <= n; ++i) {
cout << sol[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
6 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
6 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
6 ms |
7508 KB |
Output is correct |
9 |
Correct |
6 ms |
7508 KB |
Output is correct |
10 |
Correct |
6 ms |
7508 KB |
Output is correct |
11 |
Correct |
6 ms |
7636 KB |
Output is correct |
12 |
Correct |
6 ms |
7480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
6 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
6 ms |
7508 KB |
Output is correct |
9 |
Correct |
6 ms |
7508 KB |
Output is correct |
10 |
Correct |
6 ms |
7508 KB |
Output is correct |
11 |
Correct |
6 ms |
7636 KB |
Output is correct |
12 |
Correct |
6 ms |
7480 KB |
Output is correct |
13 |
Correct |
9 ms |
7644 KB |
Output is correct |
14 |
Correct |
8 ms |
7776 KB |
Output is correct |
15 |
Correct |
10 ms |
7640 KB |
Output is correct |
16 |
Correct |
8 ms |
7740 KB |
Output is correct |
17 |
Correct |
7 ms |
7636 KB |
Output is correct |
18 |
Correct |
7 ms |
7636 KB |
Output is correct |
19 |
Correct |
8 ms |
7640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
322 ms |
23448 KB |
Output is correct |
2 |
Correct |
294 ms |
26956 KB |
Output is correct |
3 |
Correct |
201 ms |
22608 KB |
Output is correct |
4 |
Correct |
326 ms |
23756 KB |
Output is correct |
5 |
Correct |
293 ms |
25048 KB |
Output is correct |
6 |
Correct |
325 ms |
24148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
6 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
6 ms |
7508 KB |
Output is correct |
9 |
Correct |
6 ms |
7508 KB |
Output is correct |
10 |
Correct |
6 ms |
7508 KB |
Output is correct |
11 |
Correct |
6 ms |
7636 KB |
Output is correct |
12 |
Correct |
6 ms |
7480 KB |
Output is correct |
13 |
Correct |
9 ms |
7644 KB |
Output is correct |
14 |
Correct |
8 ms |
7776 KB |
Output is correct |
15 |
Correct |
10 ms |
7640 KB |
Output is correct |
16 |
Correct |
8 ms |
7740 KB |
Output is correct |
17 |
Correct |
7 ms |
7636 KB |
Output is correct |
18 |
Correct |
7 ms |
7636 KB |
Output is correct |
19 |
Correct |
8 ms |
7640 KB |
Output is correct |
20 |
Correct |
322 ms |
23448 KB |
Output is correct |
21 |
Correct |
294 ms |
26956 KB |
Output is correct |
22 |
Correct |
201 ms |
22608 KB |
Output is correct |
23 |
Correct |
326 ms |
23756 KB |
Output is correct |
24 |
Correct |
293 ms |
25048 KB |
Output is correct |
25 |
Correct |
325 ms |
24148 KB |
Output is correct |
26 |
Correct |
340 ms |
24196 KB |
Output is correct |
27 |
Correct |
328 ms |
26732 KB |
Output is correct |
28 |
Correct |
352 ms |
27208 KB |
Output is correct |
29 |
Correct |
206 ms |
22864 KB |
Output is correct |
30 |
Correct |
350 ms |
24188 KB |
Output is correct |
31 |
Correct |
328 ms |
22548 KB |
Output is correct |
32 |
Correct |
389 ms |
25376 KB |
Output is correct |
33 |
Correct |
334 ms |
23996 KB |
Output is correct |
34 |
Correct |
192 ms |
22784 KB |
Output is correct |
35 |
Correct |
367 ms |
24272 KB |
Output is correct |
36 |
Correct |
310 ms |
27964 KB |
Output is correct |