#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair <int, LL> PILL;
typedef pair <LL, int> PLLI;
constexpr int NMAX = 1e5 + 5;
vector <PILL> G[NMAX];
int N, K;
int Root;
int cnt_Leaf;
bool Leaf[NMAX];
LL sol;
LL ans[NMAX];
multiset <LL> Chosen, NotChosen;
void AddPath (LL way) {
sol += way;
Chosen.insert(way);
if (Chosen.size() > K) {
LL worst = *Chosen.begin();
sol -= worst;
Chosen.erase(Chosen.begin());
NotChosen.insert(worst);
}
}
void DeletePath (LL way) {
if (NotChosen.find(way) == NotChosen.end()) {
if (Chosen.find(way) == Chosen.end())
return;
sol -= way;
Chosen.erase(Chosen.find(way));
LL best = *prev(NotChosen.end());
sol += best;
Chosen.insert(best);
NotChosen.erase(prev(NotChosen.end()));
return;
}
NotChosen.erase(NotChosen.find(way));
}
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K;
for (int i = 1; i < N; ++ i ) {
int x, y, c;
cin >> x >> y >> c;
G[x].push_back({y, c});
G[y].push_back({x, c});
}
for (int i = 1; i <= N; ++ i )
if (G[i].size() == 1) {
Leaf[i] = true;
++ cnt_Leaf;
}
for (int i = 1; i <= N; ++ i )
if (G[i].size() > 1) {
Root = i;
break;
}
}
LL Subtree[NMAX];
void DfsSubtree (int Node, int dad = -1) {
for (auto it : G[Node]) {
int to = it.first;
LL cost = it.second;
if (to == dad) continue;
DfsSubtree(to, Node);
if (Subtree[Node] < Subtree[to] + cost)
Subtree[Node] = Subtree[to] + cost;
}
}
LL Ancestor[NMAX];
void DfsAncestor (int Node, int dad = -1) {
LL Max_1 = 0, Max_2 = 0, who = 0;
for (auto it : G[Node]) {
int to = it.first;
LL cost = it.second;
if (to == dad) continue;
if (Max_1 <= Subtree[to] + cost) {
Max_2 = Max_1;
Max_1 = Subtree[to] + cost;
who = to;
}
else if (Max_2 < Subtree[to] + cost) Max_2 = Subtree[to] + cost;
}
for (auto it : G[Node]) {
int to = it.first;
LL cost = it.second;
if (to == dad) continue;
Ancestor[to] = max(Ancestor[Node], (to == who ? Max_2 : Max_1)) + cost;
DfsAncestor(to, Node);
if (who != to) {
AddPath(Subtree[to] + cost);
}
}
}
void Solve (int Node, int dad = -1) {
ans[Node] = sol;
for (auto it : G[Node]) {
int to = it.first;
LL cost = it.second;
if (to == dad) continue;
DeletePath(Subtree[to] + cost);
AddPath(Subtree[to]);
DeletePath(Ancestor[to] - cost);
AddPath(Ancestor[to]);
Solve(to, Node);
AddPath(Subtree[to] + cost);
DeletePath(Subtree[to]);
AddPath(Ancestor[to] - cost);
DeletePath(Ancestor[to]);
}
}
int main () {
Read();
DfsSubtree(Root);
DfsAncestor(Root);
AddPath(Subtree[Root]);
Solve(Root);
for (int i = 1; i <= N; ++ i )
cout << ans[i] << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'void AddPath(LL)':
Main.cpp:25:23: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
25 | if (Chosen.size() > K) {
| ~~~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2792 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2792 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2684 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2792 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2684 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
3 ms |
2828 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2804 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2792 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2684 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
3 ms |
2828 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2804 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
5 ms |
2900 KB |
Output is correct |
14 |
Correct |
4 ms |
3028 KB |
Output is correct |
15 |
Correct |
4 ms |
2900 KB |
Output is correct |
16 |
Correct |
4 ms |
2900 KB |
Output is correct |
17 |
Correct |
5 ms |
2900 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
5 ms |
2956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
15764 KB |
Output is correct |
2 |
Correct |
228 ms |
17552 KB |
Output is correct |
3 |
Correct |
151 ms |
13700 KB |
Output is correct |
4 |
Correct |
218 ms |
15684 KB |
Output is correct |
5 |
Correct |
216 ms |
16652 KB |
Output is correct |
6 |
Correct |
215 ms |
15812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2792 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2684 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
3 ms |
2828 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2804 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
5 ms |
2900 KB |
Output is correct |
14 |
Correct |
4 ms |
3028 KB |
Output is correct |
15 |
Correct |
4 ms |
2900 KB |
Output is correct |
16 |
Correct |
4 ms |
2900 KB |
Output is correct |
17 |
Correct |
5 ms |
2900 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
5 ms |
2956 KB |
Output is correct |
20 |
Correct |
239 ms |
15764 KB |
Output is correct |
21 |
Correct |
228 ms |
17552 KB |
Output is correct |
22 |
Correct |
151 ms |
13700 KB |
Output is correct |
23 |
Correct |
218 ms |
15684 KB |
Output is correct |
24 |
Correct |
216 ms |
16652 KB |
Output is correct |
25 |
Correct |
215 ms |
15812 KB |
Output is correct |
26 |
Correct |
232 ms |
16220 KB |
Output is correct |
27 |
Correct |
227 ms |
17632 KB |
Output is correct |
28 |
Correct |
241 ms |
17984 KB |
Output is correct |
29 |
Correct |
163 ms |
13932 KB |
Output is correct |
30 |
Correct |
262 ms |
16104 KB |
Output is correct |
31 |
Correct |
248 ms |
14604 KB |
Output is correct |
32 |
Correct |
237 ms |
17136 KB |
Output is correct |
33 |
Correct |
270 ms |
16228 KB |
Output is correct |
34 |
Correct |
168 ms |
13744 KB |
Output is correct |
35 |
Correct |
288 ms |
16140 KB |
Output is correct |
36 |
Correct |
269 ms |
17900 KB |
Output is correct |