1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #include <bits/stdc++.h> #define V vector #define Vi vector<int> #define sz(a) ((int)a.size()) #define fi first #define se second #define Int pair<ll, int> #define Inf ((ll)1e18) #define pb push_back #define ins insert #define For(i, x, y) for (auto i = (x); i <= (y); i++) #define Rep(i, x, y) for (auto i = (x); i >= (y); i--) #define all(a) a.begin(), a.end() #define IO(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout); using namespace std;
#define ll long long #define seg int p, int l, int r #define lid p << 1, l, mid #define rid p << 1 | 1, mid + 1, r #define mid ((l + r) / 2)
const int N = 1e6 + 5; Int mn[N * 4]; ll la[N * 4];
struct Ace_taffy { Ace_taffy(int n) {}
void work(int p, ll z) { la[p] += z, mn[p].fi += z; } void pd(int p) { work(p << 1, la[p]), work(p << 1 | 1, la[p]), la[p] = 0; }
void chg(seg, int x, ll z) { if (l == r) { mn[p] = {z, l}; return; } x <= mid ? chg(lid, x, z) : chg(rid, x, z); mn[p] = min(mn[p << 1], mn[p << 1 | 1]); } void add(seg, int L, int R, ll z) { if (l >= L && r <= R) return work(p, z); if (la[p]) pd(p); if (L <= mid) add(lid, L, R, z); if (R > mid) add(rid, L, R, z); mn[p] = min(mn[p << 1], mn[p << 1 | 1]); } void del(seg, int x) { if (l == r) { mn[p].fi = Inf; return; } if (la[p]) pd(p); x <= mid ? del(lid, x) : del(rid, x); mn[p] = min(mn[p << 1], mn[p << 1 | 1]); } };
struct Spade { Vi c; int n; Spade(int _n) : c(_n + 5) { n = _n; For(i, 1, n) add(i, 1); }
void add(int x, int z) { for (; x <= n; x += x & -x) c[x] += z; } int ask(int x) { int ans = 0; for (; x; x -= x & -x) ans += c[x]; return ans; } int ask(int l, int r) { return ask(r) - ask(l - 1); } int kth(int k) { int r = 0; Rep(i, __lg(n), 0) if (r + (1 << i) <= n && k > c[r + (1 << i)]) { k -= c[r + (1 << i)], r += 1 << i; } return r + 1; } };
int siz[N], in[N], out[N], D[N], pos[N], ti = 0, rt = 0, n, sn[N], tp[N], f[N]; ll dep[N]; V<Int> e[N];
void pre(int x, int fa) { siz[x] = 1, in[x] = ++ti, D[x] = D[f[x] = fa] + 1, pos[ti] = x, sn[x] = 0; for (auto [y, z] : e[x]) if (y ^ fa) { dep[y] = dep[x] + z, pre(y, x), siz[x] += siz[y]; if (siz[y] > siz[sn[x]]) sn[x] = y; } if (max(siz[sn[x]], n - siz[x]) <= n / 2) rt = x; out[x] = ti; }
void dfs(int x, int Tp) { tp[x] = Tp; if (!sn[x]) return; dfs(sn[x], Tp); for (auto [y, z] : e[x]) if (D[y] > D[x] && y != sn[x]) dfs(y, y); }
signed main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n; for (int i = 1, x, y, z; i < n; i++) cin >> x >> y >> z, e[x].pb({y, z}), e[y].pb({x, z}); pre(1, 0); ti = 0, dep[rt] = 0, D[rt] = 1; pre(rt, 0), dfs(rt, rt);
auto lca = [&](int x, int y) { if (!x || !y) return 0; for (; tp[x] != tp[y]; x = f[tp[x]]) if (D[tp[x]] < D[tp[y]]) swap(x, y); return D[x] < D[y] ? x : y; };
auto jump = [&](int x, int y) { for (; D[f[tp[x]]] > D[y];) x = f[tp[x]]; return tp[x] == tp[y] ? sn[y] : tp[x]; };
Ace_taffy tr(n); Spade tt(n); V<ll> Ans(n + 5); ll res = 0; For(i, 1, n) tr.chg(1, 1, n, in[i], dep[i]), res += dep[i]; if (!(n & 1)) Ans[n / 2] = res;
Rep(i, n - 1, 2) { Int T = mn[1]; res -= T.fi, tr.del(1, 1, n, T.se); tt.add(T.se, -1);
auto move = [&](int p) { tr.work(1, dep[p] - dep[rt]); int T = dep[p] > dep[rt] ? p : rt; tr.add(1, 1, n, in[T], out[T], 2 * (dep[rt] - dep[p])); res -= abs(dep[p] - dep[rt]); rt = p; };
if (tt.ask(in[rt], out[rt]) * 2 < i) { int x = pos[tt.kth(tt.ask(in[rt] - 1))]; int y = pos[tt.kth(tt.ask(out[rt]) + 1)]; x = lca(x, rt), y = lca(y, rt); move(D[x] > D[y] ? x : y); }
int y = pos[tt.kth((tt.ask(in[rt]) + 1 + tt.ask(out[rt])) / 2)]; if (y != rt) { y = jump(y, rt); if (tt.ask(in[y], out[y]) * 2 > i) { int L = pos[tt.kth(tt.ask(in[y] - 1) + 1)]; int R = pos[tt.kth(tt.ask(out[y]))]; move(lca(L, R)); } }
if (!(i & 1)) Ans[i / 2] = res; } For(i, 1, n / 2) cout << Ans[i] << ' '; }
|