# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
871103 |
2023-11-09T22:21:48 Z |
MinaRagy06 |
Paths (RMI21_paths) |
C++17 |
|
358 ms |
24600 KB |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
const int N = 100'005;
vector<array<int, 2>> adj[N];
tree<pair<ll, int>, null_type, greater<pair<ll, int>>, rb_tree_tag, tree_order_statistics_node_update> s;
int n, k;
ll vals[N];
pair<ll, int> dp[N][2];
void dfs(int i, int par, int parc) {
dp[i][0] = dp[i][1] = {-1e18, 0};
for (auto [nxt, w] : adj[i]) {
if (nxt == par) continue;
dfs(nxt, i, w);
pair<ll, int> nxtval = dp[nxt][0];
nxtval.first += w;
if (nxtval > dp[i][1]) {
dp[i][1] = nxtval;
}
if (dp[i][0] < dp[i][1]) {
swap(dp[i][0], dp[i][1]);
}
}
if (adj[i].size() == 1) {
dp[i][0] = {0, i};
}
vals[dp[i][0].second] += parc;
}
ll ans[N];
ll sum = 0;
bool rem(int i) {
int pos = s.order_of_key({vals[i], i});
s.erase({vals[i], i});
if (pos < k) {
sum -= vals[i];
return 1;
}
return 0;
}
void add(int i, bool gud) {
s.insert({vals[i], i});
int pos = s.order_of_key({vals[i], i});
if (gud) {
if (pos < k) {
sum += vals[i];
} else {
sum += (*s.find_by_order(k - 1)).first;
}
} else {
if (pos < k) {
sum += vals[i];
if (k < s.size()) {
sum -= (*s.find_by_order(k)).first;
}
}
}
}
void dfs2(int i, int par, int parc, pair<ll, int> dpup) {
dpup.first += parc;
bool gud = rem(dp[i][0].second);
vals[dp[i][0].second] -= parc;
add(dp[i][0].second, gud);
ans[i] = sum;
for (auto [nxt, w] : adj[i]) {
if (nxt == par) continue;
pair<ll, int> mx = dp[i][0];
if (dp[i][0].second == dp[nxt][0].second) mx = dp[i][1];
mx = max(mx, dpup);
gud = rem(mx.second);
vals[mx.second] += w;
add(mx.second, gud);
dfs2(nxt, i, w, mx);
gud = rem(mx.second);
vals[mx.second] -= w;
add(mx.second, gud);
}
gud = rem(dp[i][0].second);
vals[dp[i][0].second] += parc;
add(dp[i][0].second, gud);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 1, u, v, w; i < n; i++) {
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(1, 0, 0);
for (int i = 1; i <= n; i++) {
add(i, 0);
}
dfs2(1, 0, 0, {0, 0});
for (int i = 1; i <= n; i++) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'void add(int, bool)':
Main.cpp:56:10: warning: comparison of integer expressions of different signedness: 'int' and '__gnu_pbds::detail::bin_search_tree_set<std::pair<long long int, int>, __gnu_pbds::null_type, std::greater<std::pair<long long int, int> >, __gnu_pbds::detail::tree_traits<std::pair<long long int, int>, __gnu_pbds::null_type, std::greater<std::pair<long long int, int> >, __gnu_pbds::tree_order_statistics_node_update, __gnu_pbds::rb_tree_tag, std::allocator<char> >, std::allocator<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if (k < s.size()) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7336 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7336 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
3 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7512 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
11 |
Correct |
3 ms |
7260 KB |
Output is correct |
12 |
Correct |
3 ms |
7352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7336 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
3 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7512 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
11 |
Correct |
3 ms |
7260 KB |
Output is correct |
12 |
Correct |
3 ms |
7352 KB |
Output is correct |
13 |
Correct |
5 ms |
7516 KB |
Output is correct |
14 |
Correct |
5 ms |
7512 KB |
Output is correct |
15 |
Correct |
4 ms |
7512 KB |
Output is correct |
16 |
Correct |
5 ms |
7512 KB |
Output is correct |
17 |
Correct |
5 ms |
7512 KB |
Output is correct |
18 |
Correct |
4 ms |
7516 KB |
Output is correct |
19 |
Correct |
5 ms |
7344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
342 ms |
18772 KB |
Output is correct |
2 |
Correct |
320 ms |
23272 KB |
Output is correct |
3 |
Correct |
286 ms |
20308 KB |
Output is correct |
4 |
Correct |
331 ms |
20560 KB |
Output is correct |
5 |
Correct |
318 ms |
21776 KB |
Output is correct |
6 |
Correct |
321 ms |
20420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7336 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
3 ms |
7260 KB |
Output is correct |
9 |
Correct |
3 ms |
7512 KB |
Output is correct |
10 |
Correct |
3 ms |
7260 KB |
Output is correct |
11 |
Correct |
3 ms |
7260 KB |
Output is correct |
12 |
Correct |
3 ms |
7352 KB |
Output is correct |
13 |
Correct |
5 ms |
7516 KB |
Output is correct |
14 |
Correct |
5 ms |
7512 KB |
Output is correct |
15 |
Correct |
4 ms |
7512 KB |
Output is correct |
16 |
Correct |
5 ms |
7512 KB |
Output is correct |
17 |
Correct |
5 ms |
7512 KB |
Output is correct |
18 |
Correct |
4 ms |
7516 KB |
Output is correct |
19 |
Correct |
5 ms |
7344 KB |
Output is correct |
20 |
Correct |
342 ms |
18772 KB |
Output is correct |
21 |
Correct |
320 ms |
23272 KB |
Output is correct |
22 |
Correct |
286 ms |
20308 KB |
Output is correct |
23 |
Correct |
331 ms |
20560 KB |
Output is correct |
24 |
Correct |
318 ms |
21776 KB |
Output is correct |
25 |
Correct |
321 ms |
20420 KB |
Output is correct |
26 |
Correct |
339 ms |
20960 KB |
Output is correct |
27 |
Correct |
307 ms |
23376 KB |
Output is correct |
28 |
Correct |
325 ms |
24144 KB |
Output is correct |
29 |
Correct |
275 ms |
20420 KB |
Output is correct |
30 |
Correct |
358 ms |
20816 KB |
Output is correct |
31 |
Correct |
311 ms |
20148 KB |
Output is correct |
32 |
Correct |
316 ms |
22124 KB |
Output is correct |
33 |
Correct |
343 ms |
21024 KB |
Output is correct |
34 |
Correct |
287 ms |
20396 KB |
Output is correct |
35 |
Correct |
341 ms |
20944 KB |
Output is correct |
36 |
Correct |
311 ms |
24600 KB |
Output is correct |