#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100010;
int n, K;
ll down[maxn], up[maxn], ans[maxn];
vector<array<int, 2>> G[maxn];
namespace DS {
ll sum;
multiset<ll> S, cand;
void ins(ll x) {
if (S.size() < K || *S.begin() < x) {
if (S.size() == K) sum -= *S.begin(), cand.insert(*S.begin()), S.erase(S.begin());
sum += x, S.insert(x);
} else {
cand.insert(x);
}
}
void del(ll x) {
if (cand.find(x) != cand.end()) {
cand.erase(cand.find(x));
} else {
sum -= x, S.erase(S.find(x));
if (S.size() < K) sum += *cand.rbegin(), S.insert(*cand.rbegin()), cand.erase(prev(cand.end()));
}
}
} // namespace DS
int main() {
scanf("%d %d", &n, &K);
for (int i = 1, u, v, w; i < n; i++) {
scanf("%d %d %d", &u, &v, &w);
G[u].push_back({v, w}), G[v].push_back({u, w});
}
function<void(int, int)> dfs1 = [&](int u, int fa) {
for (auto &e : G[u]) if (e[0] ^ fa) {
dfs1(e[0], u), down[u] = max(down[u], down[e[0]] + e[1]);
}
};
function<void(int, int)> dfs2 = [&](int u, int fa) {
vector<ll> W = {0};
for (auto &e : G[u]) if (e[0] ^ fa) {
W.push_back(down[e[0]] + e[1]);
}
sort(W.begin(), W.end(), greater<>());
for (int i = 1; i < W.size(); i++) {
DS::ins(W[i]);
}
for (auto &e : G[u]) if (e[0] ^ fa) {
up[e[0]] = max(up[u], down[e[0]] + e[1] == W[0] ? W[1] : W[0]) + e[1];
dfs2(e[0], u);
}
};
function<void(int, int)> dfs3 = [&](int u, int fa) {
ans[u] = DS::sum;
for (auto &e : G[u]) if (e[0] ^ fa) {
DS::ins(up[e[0]]), DS::ins(down[e[0]]);
DS::del(up[e[0]] - e[1]), DS::del(down[e[0]] + e[1]);
dfs3(e[0], u);
DS::del(up[e[0]]), DS::del(down[e[0]]);
DS::ins(up[e[0]] - e[1]), DS::ins(down[e[0]] + e[1]);
}
};
dfs1(1, 0), dfs2(1, 0);
DS::ins(down[1]), dfs3(1, 0);
for (int i = 1; i <= n; i++) {
printf("%lld\n", ans[i]);
}
return 0;
}
Compilation message
Main.cpp: In function 'void DS::ins(ll)':
Main.cpp:15:18: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
15 | if (S.size() < K || *S.begin() < x) {
| ~~~~~~~~~^~~
Main.cpp:16:22: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
16 | if (S.size() == K) sum -= *S.begin(), cand.insert(*S.begin()), S.erase(S.begin());
| ~~~~~~~~~^~~~
Main.cpp: In function 'void DS::del(ll)':
Main.cpp:28:22: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
28 | if (S.size() < K) sum += *cand.rbegin(), S.insert(*cand.rbegin()), cand.erase(prev(cand.end()));
| ~~~~~~~~~^~~
Main.cpp: In lambda function:
Main.cpp:50:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 1; i < W.size(); i++) {
| ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d %d", &n, &K);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d %d %d", &u, &v, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
5 ms |
2772 KB |
Output is correct |
9 |
Correct |
4 ms |
2772 KB |
Output is correct |
10 |
Correct |
5 ms |
2772 KB |
Output is correct |
11 |
Correct |
5 ms |
2772 KB |
Output is correct |
12 |
Correct |
4 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
5 ms |
2772 KB |
Output is correct |
9 |
Correct |
4 ms |
2772 KB |
Output is correct |
10 |
Correct |
5 ms |
2772 KB |
Output is correct |
11 |
Correct |
5 ms |
2772 KB |
Output is correct |
12 |
Correct |
4 ms |
2772 KB |
Output is correct |
13 |
Correct |
5 ms |
2900 KB |
Output is correct |
14 |
Correct |
7 ms |
3028 KB |
Output is correct |
15 |
Correct |
5 ms |
2896 KB |
Output is correct |
16 |
Correct |
6 ms |
2824 KB |
Output is correct |
17 |
Correct |
6 ms |
2900 KB |
Output is correct |
18 |
Correct |
7 ms |
2772 KB |
Output is correct |
19 |
Correct |
6 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
383 ms |
14476 KB |
Output is correct |
2 |
Correct |
349 ms |
17520 KB |
Output is correct |
3 |
Correct |
289 ms |
14104 KB |
Output is correct |
4 |
Correct |
319 ms |
14484 KB |
Output is correct |
5 |
Correct |
329 ms |
15780 KB |
Output is correct |
6 |
Correct |
282 ms |
14408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
5 ms |
2772 KB |
Output is correct |
9 |
Correct |
4 ms |
2772 KB |
Output is correct |
10 |
Correct |
5 ms |
2772 KB |
Output is correct |
11 |
Correct |
5 ms |
2772 KB |
Output is correct |
12 |
Correct |
4 ms |
2772 KB |
Output is correct |
13 |
Correct |
5 ms |
2900 KB |
Output is correct |
14 |
Correct |
7 ms |
3028 KB |
Output is correct |
15 |
Correct |
5 ms |
2896 KB |
Output is correct |
16 |
Correct |
6 ms |
2824 KB |
Output is correct |
17 |
Correct |
6 ms |
2900 KB |
Output is correct |
18 |
Correct |
7 ms |
2772 KB |
Output is correct |
19 |
Correct |
6 ms |
2900 KB |
Output is correct |
20 |
Correct |
383 ms |
14476 KB |
Output is correct |
21 |
Correct |
349 ms |
17520 KB |
Output is correct |
22 |
Correct |
289 ms |
14104 KB |
Output is correct |
23 |
Correct |
319 ms |
14484 KB |
Output is correct |
24 |
Correct |
329 ms |
15780 KB |
Output is correct |
25 |
Correct |
282 ms |
14408 KB |
Output is correct |
26 |
Correct |
377 ms |
14840 KB |
Output is correct |
27 |
Correct |
440 ms |
19544 KB |
Output is correct |
28 |
Correct |
346 ms |
20216 KB |
Output is correct |
29 |
Correct |
263 ms |
16476 KB |
Output is correct |
30 |
Correct |
329 ms |
16932 KB |
Output is correct |
31 |
Correct |
410 ms |
16216 KB |
Output is correct |
32 |
Correct |
344 ms |
18180 KB |
Output is correct |
33 |
Correct |
388 ms |
16928 KB |
Output is correct |
34 |
Correct |
209 ms |
16464 KB |
Output is correct |
35 |
Correct |
366 ms |
16904 KB |
Output is correct |
36 |
Correct |
314 ms |
21624 KB |
Output is correct |