Submission #1132988

#TimeUsernameProblemLanguageResultExecution timeMemory
1132988vladiliusPaths (RMI21_paths)C++20
68 / 100
1095 ms22340 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3,unroll-loops") using namespace std; using ll = long long; using pii = pair<int, int>; using pli = pair<ll, int>; #define pb push_back #define ff first #define ss second struct ST{ vector<ll> t, p, a; int n; ST(int ns){ n = ns; t.resize(4 * n); p.resize(4 * n); } void build(int v, int tl, int tr){ if (tl == tr){ t[v] = a[tl]; return; } int tm = (tl + tr) / 2, vv = 2 * v; build(vv, tl, tm); build(vv + 1, tm + 1, tr); t[v] = max(t[vv], t[vv + 1]); } void build(){ build(1, 1, n); } void push(int v){ if (!p[v]) return; int vv = 2 * v; t[vv] += p[v]; t[vv + 1] += p[v]; p[vv] += p[v]; p[vv + 1] += p[v]; p[v] = 0; } void upd(int v, int tl, int tr, int l, int r, int x){ if (l > tr || r < tl) return; if (l <= tl && tr <= r){ t[v] += x; p[v] += x; return; } int tm = (tl + tr) / 2, vv = 2 * v; push(v); upd(vv, tl, tm, l, r, x); upd(vv + 1, tm + 1, tr, l, r, x); t[v] = max(t[vv], t[vv + 1]); } void upd(int l, int r, int x){ upd(1, 1, n, l, r, x); } pli get(int v, int tl, int tr){ if (tl == tr) return {t[v], tl}; int tm = (tl + tr) / 2, vv = 2 * v; push(v); if (t[vv] > t[vv + 1]){ return get(vv, tl, tm); } return get(vv + 1, tm + 1, tr); } pli get(){ return get(1, 1, n); } void clear(){ fill(t.begin(), t.end(), 0); fill(p.begin(), p.end(), 0); } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin>>n>>k; vector<pii> g[n + 1]; for (int i = 1; i < n; i++){ int x, y, w; cin>>x>>y>>w; g[x].pb({y, w}); g[y].pb({x, w}); } vector<int> tin(n + 1), tout(n + 1), W(n + 1), P(n + 1); vector<ll> d(n + 1); int timer = 0; function<void(int, int)> fill = [&](int v, int pr){ P[v] = pr; tin[v] = ++timer; for (auto [i, w]: g[v]){ if (i == pr) continue; W[i] = w; d[i] = d[v] + w; fill(i, v); } tout[v] = timer; }; if (k == 1){ fill(1, 0); vector<ll> out(n + 1), a(n + 1); for (int i = 1; i <= n; i++){ a[tin[i]] = d[i]; } ST T(n); T.a = a; T.build(); function<void(int, int)> solve = [&](int v, int pr){ out[v] = T.get().ff; for (auto [i, w]: g[v]){ if (i == pr) continue; T.upd(1, n, w); T.upd(tin[i], tout[i], -2 * w); solve(i, v); T.upd(1, n, -w); T.upd(tin[i], tout[i], 2 * w); } }; solve(1, 0); for (int i = 1; i <= n; i++){ cout<<out[i]<<"\n"; } return 0; } ST T(n); vector<int> inv(n + 1); vector<ll> a(n + 1); vector<bool> used(n + 1); for (int i = 1; i <= n; i++){ timer = d[i] = 0; fill(i, 0); T.clear(); for (int j = 1; j <= n; j++){ if (g[j].size() == 1){ a[tin[j]] = d[j]; } else a[tin[j]] = 0; inv[tin[j]] = j; used[j] = 0; } T.a = a; T.build(); used[i] = 1; int tt = k; ll out = 0; while (tt--){ auto [vv, p] = T.get(); if (!vv) break; out += vv; p = inv[p]; while (!used[p]){ used[p] = 1; T.upd(tin[p], tout[p], -W[p]); p = P[p]; } } cout<<out<<"\n"; } }
#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...