Submission #1108765

#TimeUsernameProblemLanguageResultExecution timeMemory
1108765cpptowinPaths (RMI21_paths)C++17
19 / 100
191 ms121796 KiB
#include <bits/stdc++.h> #define fo(i, d, c) for (int i = d; i <= c; i++) #define fod(i, c, d) for (int i = c; i >= d; i--) #define maxn 1000010 #define N 1010 #define fi first #define se second #define pb emplace_back #define en cout << "\n"; #define ll long long #define bitcount(x) __builtin_popcountll(x) #define pii pair<int, int> #define vii vector<pii> #define lb(x) x & -x #define bit(i, j) ((i >> j) & 1) #define offbit(i, j) (i ^ (1LL << j)) #define onbit(i, j) (i | (1LL << j)) #define vi vector<int> #define all(x) x.begin(), x.end() #define ss(x) (int)x.size() #define UNIQUE(v) v.erase(unique(all(v)), v.end()) template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } using namespace std; const int nsqrt = 450; const int mod = 1e9 + 7; void add(int &x, int k) { x += k; x %= mod; if (x < 0) x += mod; } void del(int &x, int k) { x -= k; x %= mod; if (x < 0) x += mod; } vii ke[maxn]; int n, k; pair<ll,int> down[maxn], up[maxn]; ll f[maxn]; int par[maxn][20]; int a[maxn]; ll num[maxn]; int h[maxn]; vi leaves; ll ans, res[maxn]; set<pair<ll,int>> q, q1; void dfs(int u) { up[u].se = u; for (auto [v, w] : ke[u]) if (v != par[u][0]) { par[v][0] = u; h[v] = h[u] + 1; f[v] = f[u] + w; fo(i, 1, 19) par[v][i] = par[par[v][i - 1]][i - 1]; dfs(v); if (maximize(up[u].fi, up[v].fi + w)) up[u].se = up[v].se; } } void dfsdown(int u) { vector<pair<ll,int>> suf(ss(ke[u]) + 2, {0, 0}); suf[ss(ke[u]) + 1] = down[u]; pair<ll,int> pre = {0, 0}; fod(i, ss(ke[u]), 1) { auto [v, w] = ke[u][i - 1]; suf[i] = suf[i + 1]; if (v == par[u][0]) continue; if (maximize(suf[i].fi, up[v].fi + w)) suf[i].se = up[v].se; } fo(i, 1, ss(ke[u])) { auto [v, w] = ke[u][i - 1]; if (v == par[u][0]) continue; down[v] = {pre.fi + w, pre.se}; if (maximize(down[v].fi, suf[i + 1].fi + w)) down[v].se = suf[i + 1].se; if (maximize(pre.fi, up[v].fi + w)) pre.se = up[v].se; } for (auto [v, w] : ke[u]) if (v != par[u][0]) { dfsdown(v); } } int find(int u) { int cc = u; fod(i, 19, 0) if (up[par[u][i]].se == cc) { u = par[u][i]; } return par[u][0]; } vector<pair<int,ll>> event; void to(int x, int y) { int u = down[y].se; event.pb(u, num[u]); if (q.find({num[u], u}) != q.end()) { q.erase(q.find({num[u], u})); num[u] = down[y].fi; q.insert({num[u], u}); } else if (q1.find({num[u], u}) != q1.end()) { q1.erase(q1.find({num[u], u})); ans -= num[u]; num[u] = down[y].fi; ans += num[u]; q1.insert({num[u], u}); } u = up[y].se; event.pb(u, num[u]); if (q.find({num[u], u}) != q.end()) { q.erase(q.find({num[u], u})); num[u] = up[y].fi; q.insert({num[u], u}); } else if (q1.find({num[u], u}) != q1.end()) { q1.erase(q1.find({num[u], u})); ans -= num[u]; num[u] = up[y].fi; ans += num[u]; q1.insert({num[u], u}); } if (q1.begin()->fi < q.rbegin()->fi) { ans -= q1.begin()->fi; ans += q.rbegin()->fi; pii it = *q1.begin(); pii it1 = *q.rbegin(); q1.erase(q1.find(it)); q.erase(q.find(it1)); q1.insert(it1); q.insert(it); } } void rollback() { int u = event.back().fi; ll du = event.back().se; event.pop_back(); if (q.find({num[u], u}) != q.end()) { q.erase(q.find({num[u], u})); num[u] = du; q.insert({num[u], u}); } else if (q1.find({num[u], u}) != q1.end()) { q1.erase(q1.find({num[u], u})); ans -= num[u]; num[u] = du; ans += num[u]; q1.insert({num[u], u}); } u = event.back().fi; du = event.back().se; event.pop_back(); if (q.find({num[u], u}) != q.end()) { q.erase(q.find({num[u], u})); num[u] = du; q.insert({num[u], u}); } else if (q1.find({num[u], u}) != q1.end()) { q1.erase(q1.find({num[u], u})); ans -= num[u]; num[u] = du; ans += num[u]; q1.insert({num[u], u}); } if (q1.begin()->fi < q.rbegin()->fi) { ans -= q1.begin()->fi; ans += q.rbegin()->fi; pii it = *q1.begin(); pii it1 = *q.rbegin(); q1.erase(q1.find(it)); q.erase(q.find(it1)); q1.insert(it1); q.insert(it); } } void dfs1(int u) { res[u] = ans; for (auto [v, w] : ke[u]) if (v != par[u][0]) { to(u, v); dfs1(v); rollback(); } } main() { #define name "TASK" if (fopen(name ".inp", "r")) { freopen(name ".inp", "r", stdin); freopen(name ".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; fo(i, 1, n - 1) { int u, v, w; cin >> u >> v >> w; ke[u].pb(v, w); ke[v].pb(u, w); } dfs(1); dfsdown(1); fo(i, 1, n) if (ss(ke[i]) == 1) leaves.pb(i); for (int it : leaves) { a[it] = max(1, find(it)); num[it] = f[it] - f[a[it]]; q.insert({num[it], it}); } down[1] = {0, 1}; int cc = k; while (ss(q) and cc > 0) { ans += q.rbegin()->fi; q1.insert(*q.rbegin()); q.erase(q.find(*q.rbegin())); cc--; } dfs1(1); fo(i, 1, n) cout << res[i] << "\n"; }

Compilation message (stderr)

Main.cpp:231:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  231 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:236:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  236 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:237:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  237 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...