#include <bits/stdc++.h> #define ll long long #define mp make_pair #define fi first #define se second #define pb push_back #define pi pair<int, int> #define mod 1000000007 template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;} template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;} ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;} using namespace std; const int maxn = 200005; #define vl vector<ll> const ll inf = 1e18; int mxinv = 0; const int hh = 1100; vl mpl(vl a, vl b) { // max plus product of convex a, b // size must be odd if (a.size() >= 2 * hh) { int md = a.size() / 2; a = vl(a.begin() + md - hh, a.begin() + md + hh + 1); } if (b.size() >= 2 * hh) { int md = b.size() / 2; b = vl(b.begin() + md - hh, b.begin() + md + hh + 1); } for (auto &v: a) if (v < -inf / 2) v = -inf; for (auto &v: b) if (v < -inf / 2) v = -inf; vl res(a.size() + b.size() - 1); fill(res.begin(), res.end(), -inf); if (min(a.size(), b.size()) < 10 && 0) { for (int i = 0; i < a.size(); i++) for (int j = 0; j < b.size(); j++) chkmax(res[i + j], a[i] + b[j]); } else { auto inv = [&](int l1, int r1, int l2, int r2) { int p[2] = {l1, l2}; for (int i = l1 + l2; i <= r1 + r2; i++) { chkmax(res[i], a[p[0]] + b[p[1]]); if (p[0] == r1) p[1] += 1; else if (p[1] == r2) p[0] += 1; else { if (a[p[0] + 1] - a[p[0]] > b[p[1] + 1] - b[p[1]]) p[0] += 1; else p[1] += 1; } } }; vector<int> h[2]; h[0].pb(0); h[1].pb(0); for (int i = 1; i < a.size() - 1; i++) if (a[i + 1] - a[i] > a[i] - a[i - 1]) h[0].pb(i); for (int i = 1; i < b.size() - 1; i++) if (b[i + 1] - b[i] > b[i] - b[i - 1]) h[1].pb(i); chkmax(mxinv, max((int)h[0].size(), (int)h[1].size())); h[0].pb(a.size() - 1); h[1].pb(b.size() - 1); for (int i = 0; i < h[0].size() - 1; i++) for (int j = 0; j < h[1].size() - 1; j++) inv(h[0][i], h[0][i + 1], h[1][j], h[1][j + 1]); } return res; } struct ans { vl x[2]; ans() {x[0] = vl{-inf}, x[1] = vl{-inf};} }; vl mx(vl a, vl b) { // max of two if (a.size() < b.size()) swap(a, b); int ma = a.size() / 2, mb = b.size() / 2; for (int j = -mb; j <= mb; j++) chkmax(a[ma + j], b[mb + j]); return a; } ans uni(ans a, ans b) { ans res; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) res.x[i ^ j] = mx(res.x[i ^ j], mpl(a.x[i], b.x[j])); return res; } ans cur[maxn]; ans cal(int l, int r) { if (l == r) return cur[l]; int mid = (l + r) >> 1; return uni(cal(l, mid), cal(mid + 1, r)); } int n, m; ll dp[maxn][4]; vector<pi> eg[maxn]; mt19937 rd; void dfs(int a, int fa) { for (int i = 0; i < 4; i++) dp[a][i] = -inf; for (auto v : eg[a]) { if (v.fi == fa) continue; dfs(v.fi, a); } int cnt = 0; cur[cnt].x[0] = vl{0}, cur[cnt].x[1] = vl{-inf}; cnt += 1; for (auto v : eg[a]) { if (v.fi == fa) continue; ans hh; hh.x[0].resize(3); for (int j = 0; j < 3; j++) hh.x[0][j] = -inf; for (int t = 0; t < 2; t++) for (int j = 0; j < 4; j++) { if ((t == 0) && (j != 0)) continue; int ntp = 0, npar = 0; if ((t + j) == 2) npar = 1; if (t + j == 1) ntp = 1; else if (t + j == 3) ntp = -1; ll nw = dp[v.fi][j]; if (t) nw += v.se; chkmax(hh.x[npar][hh.x[npar].size() / 2 + ntp], nw); } cur[cnt++] = hh; } shuffle(cur, cur + cnt, rd); ans cm = cal(0, cnt - 1); for (int j = 0; j < 4; j++) { int ntp = 0, npar = 0; if (j == 1) ntp = 1; else if (j == 3) ntp = -1; if (j == 2) npar = 1; int nsz = cm.x[npar].size() / 2; if (nsz >= abs(ntp)) chkmax(dp[a][j], cm.x[npar][nsz + ntp]); } //cout << a << ' ' << dp[a][0] << ' ' << cnt << ' ' << cm.x[0].size() << ' ' << cm.x[0][0] << endl; } int main() { //freopen("x.out", "r", stdin); cin >> n; for (int i = 1; i < n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); eg[u].pb(mp(v, w)); eg[v].pb(mp(u, w)); } dfs(1, 0); //cout << mxinv << ' ' << eg[1].size() << endl; cout << dp[1][0] << endl; //cout << 1.0 * clock() / CLOCKS_PER_SEC << endl; return (0-0); // <3 cxr }
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 | #include <bits/stdc++.h> #define ll long long #define mp make_pair #define fi first #define se second #define pb push_back #define pi pair<int, int> #define mod 1000000007 template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;} template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;} ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;} using namespace std; const int maxn = 200005; #define vl vector<ll> const ll inf = 1e18; int mxinv = 0; const int hh = 1100; vl mpl(vl a, vl b) { // max plus product of convex a, b // size must be odd if (a.size() >= 2 * hh) { int md = a.size() / 2; a = vl(a.begin() + md - hh, a.begin() + md + hh + 1); } if (b.size() >= 2 * hh) { int md = b.size() / 2; b = vl(b.begin() + md - hh, b.begin() + md + hh + 1); } for (auto &v: a) if (v < -inf / 2) v = -inf; for (auto &v: b) if (v < -inf / 2) v = -inf; vl res(a.size() + b.size() - 1); fill(res.begin(), res.end(), -inf); if (min(a.size(), b.size()) < 10 && 0) { for (int i = 0; i < a.size(); i++) for (int j = 0; j < b.size(); j++) chkmax(res[i + j], a[i] + b[j]); } else { auto inv = [&](int l1, int r1, int l2, int r2) { int p[2] = {l1, l2}; for (int i = l1 + l2; i <= r1 + r2; i++) { chkmax(res[i], a[p[0]] + b[p[1]]); if (p[0] == r1) p[1] += 1; else if (p[1] == r2) p[0] += 1; else { if (a[p[0] + 1] - a[p[0]] > b[p[1] + 1] - b[p[1]]) p[0] += 1; else p[1] += 1; } } }; vector<int> h[2]; h[0].pb(0); h[1].pb(0); for (int i = 1; i < a.size() - 1; i++) if (a[i + 1] - a[i] > a[i] - a[i - 1]) h[0].pb(i); for (int i = 1; i < b.size() - 1; i++) if (b[i + 1] - b[i] > b[i] - b[i - 1]) h[1].pb(i); chkmax(mxinv, max((int)h[0].size(), (int)h[1].size())); h[0].pb(a.size() - 1); h[1].pb(b.size() - 1); for (int i = 0; i < h[0].size() - 1; i++) for (int j = 0; j < h[1].size() - 1; j++) inv(h[0][i], h[0][i + 1], h[1][j], h[1][j + 1]); } return res; } struct ans { vl x[2]; ans() {x[0] = vl{-inf}, x[1] = vl{-inf};} }; vl mx(vl a, vl b) { // max of two if (a.size() < b.size()) swap(a, b); int ma = a.size() / 2, mb = b.size() / 2; for (int j = -mb; j <= mb; j++) chkmax(a[ma + j], b[mb + j]); return a; } ans uni(ans a, ans b) { ans res; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) res.x[i ^ j] = mx(res.x[i ^ j], mpl(a.x[i], b.x[j])); return res; } ans cur[maxn]; ans cal(int l, int r) { if (l == r) return cur[l]; int mid = (l + r) >> 1; return uni(cal(l, mid), cal(mid + 1, r)); } int n, m; ll dp[maxn][4]; vector<pi> eg[maxn]; mt19937 rd; void dfs(int a, int fa) { for (int i = 0; i < 4; i++) dp[a][i] = -inf; for (auto v : eg[a]) { if (v.fi == fa) continue; dfs(v.fi, a); } int cnt = 0; cur[cnt].x[0] = vl{0}, cur[cnt].x[1] = vl{-inf}; cnt += 1; for (auto v : eg[a]) { if (v.fi == fa) continue; ans hh; hh.x[0].resize(3); for (int j = 0; j < 3; j++) hh.x[0][j] = -inf; for (int t = 0; t < 2; t++) for (int j = 0; j < 4; j++) { if ((t == 0) && (j != 0)) continue; int ntp = 0, npar = 0; if ((t + j) == 2) npar = 1; if (t + j == 1) ntp = 1; else if (t + j == 3) ntp = -1; ll nw = dp[v.fi][j]; if (t) nw += v.se; chkmax(hh.x[npar][hh.x[npar].size() / 2 + ntp], nw); } cur[cnt++] = hh; } shuffle(cur, cur + cnt, rd); ans cm = cal(0, cnt - 1); for (int j = 0; j < 4; j++) { int ntp = 0, npar = 0; if (j == 1) ntp = 1; else if (j == 3) ntp = -1; if (j == 2) npar = 1; int nsz = cm.x[npar].size() / 2; if (nsz >= abs(ntp)) chkmax(dp[a][j], cm.x[npar][nsz + ntp]); } //cout << a << ' ' << dp[a][0] << ' ' << cnt << ' ' << cm.x[0].size() << ' ' << cm.x[0][0] << endl; } int main() { //freopen("x.out", "r", stdin); cin >> n; for (int i = 1; i < n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); eg[u].pb(mp(v, w)); eg[v].pb(mp(u, w)); } dfs(1, 0); //cout << mxinv << ' ' << eg[1].size() << endl; cout << dp[1][0] << endl; //cout << 1.0 * clock() / CLOCKS_PER_SEC << endl; return (0-0); // <3 cxr } |