#include <bits/stdc++.h>
using namespace std;
int n, k;
multiset<long long> L, R;
long long s = 0;
void Add(long long x) {
L.insert(x);
while ((int) R.size() < k && !L.empty()) {
long long v = *prev(L.end());
s += v;
R.insert(v);
L.erase(L.find(v));
}
while (!L.empty() && !R.empty() && *prev(L.end()) > *R.begin()) {
long long p = *prev(L.end());
long long q = *R.begin();
L.insert(q);
R.insert(p);
L.erase(L.find(p));
R.erase(R.find(q));
s += p - q;
}
}
void Remove(long long x) {
if (R.find(x) != R.end()) {
s -= x;
R.erase(R.find(x));
} else {
L.erase(L.find(x));
}
while (R.size() < k && !L.empty()) {
long long v = *prev(L.end());
s += v;
R.insert(v);
L.erase(L.find(v));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
vector<vector<pair<int, int>>> g(n);
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
--u; --v;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
vector<long long> mx(n);
function<void(int, int)> Dfs = [&](int v, int pv) {
for (auto& e : g[v]) {
int u = e.first;
int w = e.second;
if (u == pv) {
continue;
}
Dfs(u, v);
if (mx[u] != 0) {
Remove(mx[u]);
}
Add(mx[u] + w);
mx[v] = max(mx[v], mx[u] + w);
}
};
Dfs(0, 0);
vector<long long> res(n);
function<void(int, int)> Solve = [&](int v, int pv) {
long long save = mx[v];
for (auto& e : g[v]) {
int u = e.first;
int w = e.second;
if (u == pv) {
if (mx[u] != 0) {
Remove(mx[u]);
}
Add(mx[u] + w);
mx[v] = max(mx[v], mx[u] + w);
}
}
res[v] = s;
pair<long long, int> p = {-1, -1};
pair<long long, int> q = {-1, -1};
for (auto& e : g[v]) {
int u = e.first;
int w = e.second;
if (p.first < mx[u] + w) {
q = p;
p = {mx[u] + w, u};
} else if (q.first < mx[u] + w) {
q = {mx[u] + w, u};
}
}
for (auto& e : g[v]) {
int u = e.first;
int w = e.second;
if (u == pv) {
continue;
}
Remove(mx[u] + w);
Add(mx[u]);
if (u != p.second) {
Solve(u, v);
Remove(mx[u]);
Add(mx[u] + w);
continue;
}
long long t = mx[v];
if (q.second != -1) {
mx[v] = q.first;
} else {
mx[v] = 0;
}
Solve(u, v);
Remove(mx[u]);
Add(mx[u] + w);
mx[v] = t;
}
for (auto& e : g[v]) {
int u = e.first;
int w = e.second;
if (u == pv) {
if (mx[u] != 0) {
Add(mx[u]);
}
Remove(mx[u] + w);
}
}
mx[v] = save;
};
Solve(0, 0);
for (int i = 0; i < n; i++) {
cout << res[i] << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'void Remove(long long int)':
Main.cpp:36:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
36 | while (R.size() < k && !L.empty()) {
| ~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
484 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
484 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
484 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
12756 KB |
Output is correct |
2 |
Correct |
153 ms |
15896 KB |
Output is correct |
3 |
Correct |
105 ms |
10580 KB |
Output is correct |
4 |
Correct |
143 ms |
12876 KB |
Output is correct |
5 |
Correct |
146 ms |
14160 KB |
Output is correct |
6 |
Correct |
138 ms |
13008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
484 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
708 KB |
Output is correct |
20 |
Correct |
154 ms |
12756 KB |
Output is correct |
21 |
Correct |
153 ms |
15896 KB |
Output is correct |
22 |
Correct |
105 ms |
10580 KB |
Output is correct |
23 |
Correct |
143 ms |
12876 KB |
Output is correct |
24 |
Correct |
146 ms |
14160 KB |
Output is correct |
25 |
Correct |
138 ms |
13008 KB |
Output is correct |
26 |
Correct |
217 ms |
13132 KB |
Output is correct |
27 |
Correct |
182 ms |
15940 KB |
Output is correct |
28 |
Correct |
194 ms |
16728 KB |
Output is correct |
29 |
Correct |
129 ms |
10580 KB |
Output is correct |
30 |
Correct |
204 ms |
13336 KB |
Output is correct |
31 |
Correct |
235 ms |
11760 KB |
Output is correct |
32 |
Correct |
200 ms |
14420 KB |
Output is correct |
33 |
Correct |
191 ms |
13136 KB |
Output is correct |
34 |
Correct |
117 ms |
10320 KB |
Output is correct |
35 |
Correct |
203 ms |
13248 KB |
Output is correct |
36 |
Correct |
183 ms |
16892 KB |
Output is correct |