Submission #1132977

#TimeUsernameProblemLanguageResultExecution timeMemory
1132977vladiliusPaths (RMI21_paths)C++20
36 / 100
1094 ms15432 KiB
#include <bits/stdc++.h> 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; int n; ST(int ns){ n = ns; t.resize(4 * n); p.resize(4 * n); } void upd(int v, int tl, int tr, int& p, ll& x){ if (tl == tr){ t[v] = x; return; } int tm = (tl + tr) / 2, vv = 2 * v; if (p <= tm){ upd(vv, tl, tm, p, x); } else { upd(vv + 1, tm + 1, tr, p, x); } t[v] = max(t[vv], t[vv + 1]); } void upd(int p, ll x){ upd(1, 1, n, p, x); } 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(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; }; ST T(n); vector<int> inv(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++){ T.upd(tin[j], d[j]); inv[tin[j]] = j; used[j] = 0; } 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...