# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
954384 |
2024-03-27T18:15:26 Z |
TAhmed33 |
Paths (RMI21_paths) |
C++ |
|
433 ms |
46184 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 25;
vector <pair <int, ll>> adj[MAXN];
pair <ll, ll> dp[MAXN], dq[MAXN];
ll edge[MAXN];
int n, k;
void dfs (int pos, int par) {
if ((int)adj[pos].size() == 1) {
dp[pos] = {edge[pos], pos};
return;
}
for (auto j : adj[pos]) {
if (j.first == par) continue;
edge[j.first] = j.second;
dfs(j.first, pos);
dp[pos] = max(dp[pos], dp[j.first]);
}
dp[pos].first += edge[pos];
}
void dfs2 (int pos, int par, pair <ll, ll> x = {}) {
x.first += edge[pos];
dq[pos] = x;
deque <pair <ll, ll>> u;
for (auto j : adj[pos]) {
if (j.first == par) continue;
u.push_back(dp[j.first]);
}
for (int i = (int)u.size() - 2; i >= 0; i--) u[i] = max(u[i], u[i + 1]);
for (auto j : adj[pos]) {
if (j.first == par) continue;
u.pop_front();
auto g = x; if (!u.empty()) g = max(g, u.front());
dfs2(j.first, pos, g);
x = max(x, dp[j.first]);
}
}
struct DS {
ll val[MAXN], sum;
set <pair <ll, ll>> inside, outside;
void init () {
for (int i = 1; i <= k; i++) {
inside.insert({val[i], i});
}
for (int i = k + 1; i <= n; i++) {
outside.insert({val[i], i});
}
}
void upd (int a, ll b) {
if (inside.count({val[a], a})) {
inside.erase({val[a], a});
sum -= val[a];
} else {
outside.erase({val[a], a});
}
while ((int)inside.size() < k) {
if (outside.empty()) break;
auto g = *(--outside.end());
inside.insert(g); sum += g.first;
outside.erase(g);
}
val[a] += b;
inside.insert({val[a], a}); sum += val[a];
while ((int)inside.size() > k) {
auto g = *(inside.begin());
outside.insert(g);
inside.erase(g);
sum -= g.first;
}
}
ll get () {
return sum;
}
} cur;
ll ans[MAXN];
void dfs3 (int pos, int par) {
if (par != -1) {
cur.upd(dp[pos].second, -edge[pos]);
cur.upd(dq[pos].second, edge[pos]);
}
ans[pos] = cur.get();
for (auto j : adj[pos]) {
if (j.first != par) {
dfs3(j.first, pos);
}
}
if (par != -1) {
cur.upd(dq[pos].second, -edge[pos]);
cur.upd(dp[pos].second, edge[pos]);
}
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
if (n == 1) {
cout << "0\n";
return 0;
}
if (n == 2) {
int a, b, c; cin >> a >> b >> c;
cout << c << '\n' << c << '\n';
return 0;
}
for (int i = 1; i < n; i++) {
int a, b, c; cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
int root = -1;
for (int i = 1; i <= n; i++) {
if ((int)adj[i].size() >= 2) root = i;
}
dfs(root, -1);
dfs2(root, -1);
cur.init();
for (int i = 1; i <= n; i++) {
if (i != root) {
cur.upd(dp[i].second, edge[i]);
}
}
dfs3(root, -1);
for (int i = 1; i <= n; i++) {
cout << ans[i] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
4 ms |
8028 KB |
Output is correct |
9 |
Correct |
4 ms |
8536 KB |
Output is correct |
10 |
Correct |
4 ms |
8280 KB |
Output is correct |
11 |
Correct |
4 ms |
8024 KB |
Output is correct |
12 |
Correct |
5 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
4 ms |
8028 KB |
Output is correct |
9 |
Correct |
4 ms |
8536 KB |
Output is correct |
10 |
Correct |
4 ms |
8280 KB |
Output is correct |
11 |
Correct |
4 ms |
8024 KB |
Output is correct |
12 |
Correct |
5 ms |
8028 KB |
Output is correct |
13 |
Correct |
6 ms |
8372 KB |
Output is correct |
14 |
Correct |
6 ms |
8796 KB |
Output is correct |
15 |
Correct |
5 ms |
8280 KB |
Output is correct |
16 |
Correct |
6 ms |
8284 KB |
Output is correct |
17 |
Correct |
7 ms |
8388 KB |
Output is correct |
18 |
Correct |
5 ms |
8284 KB |
Output is correct |
19 |
Correct |
6 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
267 ms |
20820 KB |
Output is correct |
2 |
Correct |
289 ms |
30548 KB |
Output is correct |
3 |
Correct |
235 ms |
22612 KB |
Output is correct |
4 |
Correct |
290 ms |
22732 KB |
Output is correct |
5 |
Correct |
292 ms |
24728 KB |
Output is correct |
6 |
Correct |
279 ms |
22476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
4 ms |
8028 KB |
Output is correct |
9 |
Correct |
4 ms |
8536 KB |
Output is correct |
10 |
Correct |
4 ms |
8280 KB |
Output is correct |
11 |
Correct |
4 ms |
8024 KB |
Output is correct |
12 |
Correct |
5 ms |
8028 KB |
Output is correct |
13 |
Correct |
6 ms |
8372 KB |
Output is correct |
14 |
Correct |
6 ms |
8796 KB |
Output is correct |
15 |
Correct |
5 ms |
8280 KB |
Output is correct |
16 |
Correct |
6 ms |
8284 KB |
Output is correct |
17 |
Correct |
7 ms |
8388 KB |
Output is correct |
18 |
Correct |
5 ms |
8284 KB |
Output is correct |
19 |
Correct |
6 ms |
8284 KB |
Output is correct |
20 |
Correct |
267 ms |
20820 KB |
Output is correct |
21 |
Correct |
289 ms |
30548 KB |
Output is correct |
22 |
Correct |
235 ms |
22612 KB |
Output is correct |
23 |
Correct |
290 ms |
22732 KB |
Output is correct |
24 |
Correct |
292 ms |
24728 KB |
Output is correct |
25 |
Correct |
279 ms |
22476 KB |
Output is correct |
26 |
Correct |
418 ms |
23136 KB |
Output is correct |
27 |
Correct |
350 ms |
29636 KB |
Output is correct |
28 |
Correct |
356 ms |
30436 KB |
Output is correct |
29 |
Correct |
302 ms |
23176 KB |
Output is correct |
30 |
Correct |
343 ms |
23252 KB |
Output is correct |
31 |
Correct |
331 ms |
22356 KB |
Output is correct |
32 |
Correct |
433 ms |
27132 KB |
Output is correct |
33 |
Correct |
341 ms |
23048 KB |
Output is correct |
34 |
Correct |
230 ms |
22868 KB |
Output is correct |
35 |
Correct |
395 ms |
22944 KB |
Output is correct |
36 |
Correct |
333 ms |
46184 KB |
Output is correct |