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
| #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <utility> #include <set> #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 250000 + 5;
struct Heap { multiset<ll> st; ll sum; void insert(ll v) { st.insert(v); sum += v; } void del(ll v) { st.erase(st.find(v)); sum -= v; } void resize(int sz) { while (!st.empty() && (int)st.size() > sz) { sum -= *st.rbegin(); st.erase(--st.end()); } } void resize(int sz, vector<ll>& v) { while (!st.empty() && (int)st.size() > sz) { sum -= *st.rbegin(); v.push_back(*st.rbegin()); st.erase(--st.end()); } } } h[maxn];
int n, lim, deg[maxn], ptr[maxn], vis[maxn]; vector<PII> edge[maxn]; vector<int> bag[maxn], key[maxn];
ll dp[maxn][2]; void dfs(int u, int ff) { vis[u] = lim; int sz = deg[u] - lim; h[u].resize(sz); vector<ll> add, sub; ll tot = 0; int& p = ptr[u]; while (p < deg[u] && deg[edge[u][p].first] <= lim) p++; for (int i = p; i < deg[u]; i++) { int v = edge[u][i].first, w = edge[u][i].second; if (v == ff) continue; dfs(v, u); if (dp[v][1] + w < dp[v][0]) { tot += dp[v][1] + w; sz--; } else { tot += dp[v][0]; add.push_back(dp[v][1] + w - dp[v][0]); h[u].insert(dp[v][1] + w - dp[v][0]); } } h[u].resize(sz, sub); dp[u][0] = tot + h[u].sum; h[u].resize(sz - 1, sub); dp[u][1] = tot + h[u].sum; for (auto& x: sub) h[u].insert(x); for (auto& x: add) h[u].del(x); }
int main() { scanf("%d", &n); ll total = 0; for (int i = 2, u, v, w; i <= n; i++) { scanf("%d%d%d", &u, &v, &w); edge[u].push_back({v, w}); edge[v].push_back({u, w}); deg[u]++; deg[v]++; total += w; } for (int i = 1; i <= n; i++) { bag[deg[i]].push_back(i); sort(edge[i].begin(), edge[i].end(), [&](PII a, PII b) { return deg[a.first] < deg[b.first]; }); for (int j = 1; j < deg[i]; j++) key[j].push_back(i); } printf("%I64d", total); for (lim = 1; lim < n; lim++) { for (auto u: bag[lim]) { for (auto x: edge[u]) { int v = x.first, w = x.second; h[v].insert(w); } } ll ans = 0; for (auto& u: key[lim]) { if (vis[u] == lim) continue; dfs(u, 0); ans += dp[u][0]; } printf(" %I64d", ans); } return 0; }
|