Guest User

Untitled

a guest
Jun 16th, 2026
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <cassert>
  3.  
  4. using namespace std;
  5. using ll = long long;
  6.  
  7. #define all(x) (x).begin(), (x).end()
  8. #define sz(x) (int)(x).size()
  9. #define pb push_back
  10. #define int ll
  11.  
  12. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  13. template<typename T>
  14. void shuf(vector<T>& a) {
  15.     for (int i = 1; i < sz(a); i++) swap(a[i], a[rnd() % (i + 1)]);
  16. }
  17.  
  18. vector<ll> maxconv(const vector<ll>& a, const vector<ll>& b) {
  19.     assert(!a.empty() && !b.empty());
  20.     vector<ll> rs = {a[0] + b[0]};
  21.     int ia = 1, ib = 1;
  22.     while (ia < sz(a) || ib < sz(b)) {
  23.         if (ia < sz(a) && (ib == sz(b) || a[ia] - a[ia - 1] > b[ib] - b[ib - 1])) {
  24.             rs.pb(rs.back() + a[ia] - a[ia - 1]);
  25.             ia++;
  26.         } else {
  27.             rs.pb(rs.back() + b[ib] - b[ib - 1]);
  28.             ib++;
  29.         }
  30.     }
  31.     return rs;
  32. }
  33.  
  34. void solve() {
  35.     int n; cin >> n;
  36.     vector<vector<pair<int, ll>>> g(n);
  37.     for (int i = 0; i < n - 1; i++) {
  38.         int u, v, w; cin >> u >> v >> w;
  39.         u--; v--;
  40.         g[u].pb({v, w});
  41.         g[v].pb({u, w});
  42.     }
  43.  
  44.     vector<int> used_ct(n), sub(n), from(n, -1), ct_lvl(n);
  45.     from[0] = 0;
  46.     auto rs = [&](int v, int p, auto&&self) -> void {
  47.         sub[v] = 1;
  48.         for (auto [u, w] : g[v]) {
  49.             if (u != p && !used_ct[u]) {
  50.                 self(u, v, self);
  51.                 sub[v] += sub[u];
  52.             }
  53.         }  
  54.     };  
  55.     auto fc = [&](int v, int p, int is, auto&&self) -> int {
  56.         for (auto [u, w] : g[v]) {
  57.             if (u != p && !used_ct[u] && sub[u] > is / 2) {
  58.                 return self(u, v, is, self);
  59.             }
  60.         }
  61.         return v;
  62.     };  
  63.  
  64.     vector<vector<int>> g_ct(n);
  65.     while (true) {
  66.         vector<pair<int, int>> sf;
  67.         for (int i = 0; i < n; i++) {
  68.             if (from[i] != -1) {
  69.                 rs(i, i, rs);
  70.                 int ct = fc(i, i, sub[i], fc);
  71.                 ct_lvl[ct] = (from[i] == i ? 0 : ct_lvl[from[i]] + 1);
  72.                 if (from[i] != i) g_ct[from[i]].pb(ct);
  73.                 used_ct[ct] = 1;
  74.                 for (auto [u, w] : g[ct]) {
  75.                     if (!used_ct[u]) sf.pb({u, ct});
  76.                 }
  77.                 from[i] = -1;
  78.             }
  79.         }
  80.         if (sf.empty()) break;
  81.         for (auto u : sf) from[u.first] = u.second;
  82.     }
  83.  
  84.     vector<int> ord(n);
  85.     iota(all(ord), 0);
  86.     sort(all(ord), [&](int i, int j) {
  87.         return ct_lvl[i] < ct_lvl[j];
  88.     });
  89.  
  90.     vector<vector<int>> lvls(n);
  91.     vector<vector<vector<ll>>> dps(n);
  92.     vector<int> pos(20);
  93.     for (int ps = n - 1; ps >= 0; ps--) {
  94.         int ct = ord[ps];
  95.         if (g_ct[ct].empty()) {
  96.             for (auto [u, w] : g[ct]) {
  97.                 lvls[ct].pb(ct_lvl[u]);
  98.                 for (int j = 0; j < sz(lvls[ct]) - 1; j++) assert(lvls[ct][j] != lvls[ct].back());
  99.             }
  100.             dps[ct].assign((1 << sz(lvls[ct])), vector<ll>(1, 0));
  101.             for (int j = 0; j < sz(g[ct]); j++) {
  102.                 dps[ct][(1 << j)].pb(g[ct][j].second);
  103.             }
  104.         } else {
  105.             for (int u : g_ct[ct]) {
  106.                 for (int vl : lvls[u]) {
  107.                     if (vl != ct_lvl[ct]) lvls[ct].pb(vl);
  108.                 }
  109.             }
  110.             int sind = sz(lvls[ct]);
  111.             vector<int> ws;
  112.             for (auto [u, w] : g[ct]) {
  113.                 if (ct_lvl[u] < ct_lvl[ct]) {
  114.                     lvls[ct].pb(ct_lvl[u]);
  115.                     ws.pb(w);
  116.                 }
  117.             }
  118.             for (int j = 0; j < sz(lvls[ct]); j++) {
  119.                 pos[lvls[ct][j]] = j;
  120.             }
  121.             dps[ct].resize((1 << sz(lvls[ct])), vector<ll>(1, 0));
  122.             for (int mask = 0; mask < (1 << sz(lvls[ct])); mask++) {
  123.                 int cntout = 0, addd = 0;
  124.                 for (int j = sind; j < sz(lvls[ct]); j++) {
  125.                     if (((mask >> j) & 1)) {
  126.                         cntout++;
  127.                         addd += ws[j - sind];
  128.                     }
  129.                 }
  130.                 if (cntout > 1) continue;
  131.                 auto rec = [&](int l, int r, auto&&self) -> array<vector<ll>, 2> {
  132.                     if (l + 1 == r) {
  133.                         vector<ll> dp0, dp1;
  134.                         int M = 0, pps = 0;
  135.                         for (int j = 0; j < sz(lvls[g_ct[ct][l]]); j++) {
  136.                             if (lvls[g_ct[ct][l]][j] == ct_lvl[ct]) {
  137.                                 pps = j; continue;
  138.                             }
  139.                             int pp = pos[lvls[g_ct[ct][l]][j]];
  140.                             M += (((mask >> pp) & 1) << j);
  141.                         }
  142.                         dp0 = dps[g_ct[ct][l]][M]; dp1 = (!cntout ? dps[g_ct[ct][l]][M + (1 << pps)] : vector<ll>(0));
  143.                         for (int j = 0; j < sz(dp0); j++) {
  144.                             if (j < sz(dp1)) {
  145.                                 dp1[j] = max(dp1[j], dp0[j]);
  146.                             } else {
  147.                                 dp1.pb(dp0[j]);
  148.                             }
  149.                         }
  150.                         return {dp0, dp1};
  151.                     }
  152.                     int mid = (l + r) >> 1;
  153.                     auto rsl = self(l, mid, self), rsr = self(mid, r, self);
  154.                     auto newdp0 = maxconv(rsl[0], rsr[0]);
  155.                     if (cntout) return {newdp0, {}};
  156.  
  157.                     auto dp11 = maxconv(rsl[1], rsr[0]), dp12 = maxconv(rsl[0], rsr[1]);
  158.                     for (int j = 0; j < sz(dp12); j++) {
  159.                         if (j < sz(dp11)) dp11[j] = max(dp11[j], dp12[j]);
  160.                         else dp11.pb(dp12[j]);
  161.                     }
  162.                     for (int j = 0; j < sz(newdp0); j++) {
  163.                         if (j < sz(dp11)) dp11[j] = max(dp11[j], newdp0[j]);
  164.                         else dp11.pb(newdp0[j]);
  165.                     }
  166.                     return {newdp0, dp11};
  167.                 };
  168.                 auto lol = rec(0, sz(g_ct[ct]), rec);
  169.                 for (int j = 0; j < sz(lol[1]); j++) {
  170.                     if (j < sz(lol[0])) lol[0][j] = max(lol[0][j], lol[1][j]);
  171.                     else lol[0].pb(lol[1][j]);
  172.                 }
  173.                 if (!cntout) {
  174.                     dps[ct][mask] = lol[0];
  175.                 } else {
  176.                     dps[ct][mask] = {0};
  177.                     for (ll &xx : lol[0]) dps[ct][mask].pb(xx + addd);
  178.                 }
  179.  
  180.                 int cm = mask;
  181.                 while (cm) {
  182.                     int bit = 31 - __builtin_clz(cm);
  183.                     for (int j = 0; j < sz(dps[ct][mask - (1 << bit)]); j++) {
  184.                         if (dps[ct][mask].size() > j) dps[ct][mask][j] = max(dps[ct][mask][j], dps[ct][mask - (1 << bit)][j]);
  185.                         else dps[ct][mask].pb(dps[ct][mask - (1 << bit)][j]);
  186.                     }
  187.                     cm -= (1 << bit);
  188.                 }
  189.             }  
  190.  
  191.             for (int u : g_ct[ct]) { dps[u].clear(); lvls[u].clear(); }
  192.         }
  193.     }
  194.  
  195.     for (int cnt = 1; cnt <= n - 1; cnt++) {
  196.         if (cnt < sz(dps[ord[0]][0])) {
  197.             cout << dps[ord[0]][0][cnt] << ' ';
  198.         } else cout << "? ";
  199.     }
  200.     cout << '\n';
  201. }
  202.  
  203. signed main() {
  204.     int tt = 1;
  205.     #ifdef LOCAL
  206.         freopen("in.txt", "r", stdin);
  207.         freopen("out.txt", "w", stdout);
  208.         cin >> tt;
  209.     #else
  210.         ios::sync_with_stdio(false);
  211.         cin.tie(0); cout.tie(0);
  212.     #endif
  213.  
  214.     while (tt--) {
  215.         solve();
  216.     }
  217.  
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment