#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
#define len(a) ((int) ((a).size()))
struct max_k_t {
priority_queue<int64_t, vector<int64_t>, greater<int64_t>> A, a;
priority_queue<int64_t> B, b;
int K;
int64_t S;
max_k_t(int _K = 0): A(), a(), B(), b(), K(_K), S(0) {}
void relax() {
while (!A.empty() && !a.empty() && A.top() == a.top()) A.pop(), a.pop();
while (!B.empty() && !b.empty() && B.top() == b.top()) B.pop(), b.pop();
}
void add(int64_t x) {
if (len(A) - len(a) < K) {
A.push(x), S += x;
} else if (A.top() >= x) {
B.push(x);
} else {
B.push(A.top());
S -= A.top(), A.pop();
A.push(x), S += x;
}
relax();
}
void del(int64_t x) {
if (B.empty() || B.top() >= x) {
b.push(x);
} else {
a.push(x); S -= x;
if (!B.empty()) {
A.push(B.top()), S += B.top();
B.pop();
}
}
relax();
}
};
const int MAXN = 100007;
int N, K;
int64_t up[MAXN], down[MAXN], ans[MAXN];
max_k_t max_k;
vector<pair<int, int>> G[MAXN];
void dfs_down(int u, int p) {
for (auto [w, v] : G[u]) if (v != p) {
dfs_down(v, u);
down[u] = max(down[u], down[v] + w);
}
}
void dfs_up(int u, int p) {
vector<pair<int64_t, int>> vec;
for (auto [w, v] : G[u]) if (v != p) vec.emplace_back(down[v] + w, v);
if (len(vec) >= 2) {
for (int i = 0; i < 2; ++i) swap(vec[i], *max_element(vec.begin() + i, vec.end()));
for (int i = 1; i < len(vec); ++i) max_k.add(vec[i].first);
vec.resize(2);
}
for (auto [w, v] : G[u]) if (v != p) {
up[v] = up[u] + w;
for (auto [x, y] : vec) if (y != v) {
up[v] = max(up[v], x + w);
}
dfs_up(v, u);
}
}
void dfs_ans(int u, int p) {
ans[u] = max_k.S;
for (auto [w, v] : G[u]) if (v != p) {
max_k.add(down[v]), max_k.add(up[v]);
max_k.del(down[v] + w), max_k.del(up[v] - w);
dfs_ans(v, u);
max_k.add(down[v] + w), max_k.add(up[v] - w);
max_k.del(down[v]), max_k.del(up[v]);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
for (int i = 0; i < N - 1; ++i) {
int u, v, w; cin >> u >> v >> w;
G[u].emplace_back(w, v);
G[v].emplace_back(w, u);
}
max_k = max_k_t(K);
dfs_down(1, 0), dfs_up(1, 0);
max_k.add(down[1]);
if (len(G[1]) == 1) max_k.add(0);
dfs_ans(1, 0);
for (int u = 1; u <= N; ++u) cout << ans[u] << '\n';
}
Compilation message
Main.cpp: In function 'void dfs_down(int, int)':
Main.cpp:58:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for (auto [w, v] : G[u]) if (v != p) {
| ^
Main.cpp: In function 'void dfs_up(int, int)':
Main.cpp:66:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for (auto [w, v] : G[u]) if (v != p) vec.emplace_back(down[v] + w, v);
| ^
Main.cpp:73:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | for (auto [w, v] : G[u]) if (v != p) {
| ^
Main.cpp:75:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
75 | for (auto [x, y] : vec) if (y != v) {
| ^
Main.cpp: In function 'void dfs_ans(int, int)':
Main.cpp:84:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
84 | for (auto [w, v] : G[u]) if (v != p) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
9 |
Correct |
3 ms |
2892 KB |
Output is correct |
10 |
Correct |
3 ms |
2892 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
9 |
Correct |
3 ms |
2892 KB |
Output is correct |
10 |
Correct |
3 ms |
2892 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
13 |
Correct |
4 ms |
3020 KB |
Output is correct |
14 |
Correct |
4 ms |
3148 KB |
Output is correct |
15 |
Correct |
4 ms |
3020 KB |
Output is correct |
16 |
Correct |
4 ms |
3020 KB |
Output is correct |
17 |
Correct |
4 ms |
3072 KB |
Output is correct |
18 |
Correct |
3 ms |
3020 KB |
Output is correct |
19 |
Correct |
4 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
13680 KB |
Output is correct |
2 |
Correct |
164 ms |
17256 KB |
Output is correct |
3 |
Correct |
131 ms |
13084 KB |
Output is correct |
4 |
Correct |
133 ms |
13900 KB |
Output is correct |
5 |
Correct |
152 ms |
17372 KB |
Output is correct |
6 |
Correct |
126 ms |
13560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
9 |
Correct |
3 ms |
2892 KB |
Output is correct |
10 |
Correct |
3 ms |
2892 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
13 |
Correct |
4 ms |
3020 KB |
Output is correct |
14 |
Correct |
4 ms |
3148 KB |
Output is correct |
15 |
Correct |
4 ms |
3020 KB |
Output is correct |
16 |
Correct |
4 ms |
3020 KB |
Output is correct |
17 |
Correct |
4 ms |
3072 KB |
Output is correct |
18 |
Correct |
3 ms |
3020 KB |
Output is correct |
19 |
Correct |
4 ms |
3064 KB |
Output is correct |
20 |
Correct |
139 ms |
13680 KB |
Output is correct |
21 |
Correct |
164 ms |
17256 KB |
Output is correct |
22 |
Correct |
131 ms |
13084 KB |
Output is correct |
23 |
Correct |
133 ms |
13900 KB |
Output is correct |
24 |
Correct |
152 ms |
17372 KB |
Output is correct |
25 |
Correct |
126 ms |
13560 KB |
Output is correct |
26 |
Correct |
181 ms |
18512 KB |
Output is correct |
27 |
Correct |
158 ms |
21536 KB |
Output is correct |
28 |
Correct |
168 ms |
22232 KB |
Output is correct |
29 |
Correct |
125 ms |
16620 KB |
Output is correct |
30 |
Correct |
151 ms |
19448 KB |
Output is correct |
31 |
Correct |
144 ms |
19100 KB |
Output is correct |
32 |
Correct |
163 ms |
21020 KB |
Output is correct |
33 |
Correct |
148 ms |
18032 KB |
Output is correct |
34 |
Correct |
124 ms |
16220 KB |
Output is correct |
35 |
Correct |
155 ms |
19776 KB |
Output is correct |
36 |
Correct |
155 ms |
22404 KB |
Output is correct |