#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin()) #define sz(v) ((int)(v).size()) #define vec1d(x) vector<x> #define vec2d(x) vector<vec1d(x)> #define vec3d(x) vector<vec2d(x)> #define vec4d(x) vector<vec3d(x)> #define ivec1d(x, n, v) vec1d(x)(n, v) #define ivec2d(x, n, m, v) vec2d(x)(n, ivec1d(x, m, v)) #define ivec3d(x, n, m, k, v) vec3d(x)(n, ivec2d(x, m, k, v)) #define ivec4d(x, n, m, k, l, v) vec4d(x)(n, ivec3d(x, m, k, l, v)) #ifdef LOCAL #include "pretty_print.h" #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__) #else #define dbg(...) 42 #endif #define nl "\n" typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x < 0? -x : x; } template <typename T> T gcd(T a, T b) { return b? gcd(b, a % b) : a; } template <typename T> bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; } template <typename T> bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; } auto random_address = [] { char *p = new char; delete p; return (uint64_t) p; }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); int main(int /* argc */, const char** /* argv */) { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef LOCAL assert(freopen("i.txt", "r", stdin)); assert(freopen("o.txt", "w", stdout)); #endif int n; cin >> n; vec2d(pii) e(n); for (int i = 1; i < n; ++i) { int u, v, w; cin >> u >> v >> w; --u; --v; e[u].push_back({v, w}); e[v].push_back({u, w}); } const ll UNDEF = -1e+18; auto f = ivec2d(ll, n, 4, UNDEF); auto g = ivec3d(ll, 2, n + n + 42, 2, UNDEF); function<void(int, int)> dfs = [&](int x, int p) { for (auto& [y, w] : e[x]) { if (y == p) { continue; } dfs(y, x); } int idx = 0; int offset = n + 21; int left = offset; int right = offset; g[0][offset][0] = 0; ll limit = -1e+16; for (auto& [y, w] : e[x]) { if (y == p) { continue; } auto v = ivec1d(ll, 4, UNDEF); auto& inp = g[idx]; auto& out = g[1 - idx]; v[0] = f[y][0]; for (int i = 0; i < 4; ++i) { if (f[y][i] == UNDEF) { continue; } chmax(v[(i + 1) % 4], f[y][i] + w); } for (int b = left; b <= right; ++b) { for (int p = 0; p < 2; ++p) { if (inp[b][p] == UNDEF) { continue; } chmax(out[b + 1][p], inp[b][p] + v[3]); chmax(out[b - 1][p], inp[b][p] + v[1]); chmax(out[b][1 - p], inp[b][p] + v[2]); chmax(out[b][p], inp[b][p] + v[0]); inp[b][p] = UNDEF; } } for (;;) { bool ok = false; for (int p = 0; p < 2 && !ok; ++p) { if (out[left - 1][p] > limit) { ok = true; break; } } if (!ok) { break; } --left; } for (;;) { bool ok = false; for (int p = 0; p < 2 && !ok; ++p) { if (out[right + 1][p] > limit) { ok = true; break; } } if (!ok) { break; } ++right; } // dbg(left, right, right - left, sz(e[x])); idx ^= 1; } auto& out = g[idx]; f[x][0] = out[offset][0]; f[x][1] = out[offset - 1][0]; f[x][2] = out[offset][1]; f[x][3] = out[offset + 1][0]; for (int i = 0; i < 4; ++i) { if (f[x][i] < limit) { f[x][i] = UNDEF; } } for (int b = left; b <= right; ++b) { for (int p = 0; p < 2; ++p) { for (int x = 0; x < 4; ++x) { out[b][p] = UNDEF; } } } }; dfs(0, -1); cout << f[0][0] << nl; #ifdef LOCAL cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl; #endif return 0; }
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 171 172 173 174 | #include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin()) #define sz(v) ((int)(v).size()) #define vec1d(x) vector<x> #define vec2d(x) vector<vec1d(x)> #define vec3d(x) vector<vec2d(x)> #define vec4d(x) vector<vec3d(x)> #define ivec1d(x, n, v) vec1d(x)(n, v) #define ivec2d(x, n, m, v) vec2d(x)(n, ivec1d(x, m, v)) #define ivec3d(x, n, m, k, v) vec3d(x)(n, ivec2d(x, m, k, v)) #define ivec4d(x, n, m, k, l, v) vec4d(x)(n, ivec3d(x, m, k, l, v)) #ifdef LOCAL #include "pretty_print.h" #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__) #else #define dbg(...) 42 #endif #define nl "\n" typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x < 0? -x : x; } template <typename T> T gcd(T a, T b) { return b? gcd(b, a % b) : a; } template <typename T> bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; } template <typename T> bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; } auto random_address = [] { char *p = new char; delete p; return (uint64_t) p; }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); int main(int /* argc */, const char** /* argv */) { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef LOCAL assert(freopen("i.txt", "r", stdin)); assert(freopen("o.txt", "w", stdout)); #endif int n; cin >> n; vec2d(pii) e(n); for (int i = 1; i < n; ++i) { int u, v, w; cin >> u >> v >> w; --u; --v; e[u].push_back({v, w}); e[v].push_back({u, w}); } const ll UNDEF = -1e+18; auto f = ivec2d(ll, n, 4, UNDEF); auto g = ivec3d(ll, 2, n + n + 42, 2, UNDEF); function<void(int, int)> dfs = [&](int x, int p) { for (auto& [y, w] : e[x]) { if (y == p) { continue; } dfs(y, x); } int idx = 0; int offset = n + 21; int left = offset; int right = offset; g[0][offset][0] = 0; ll limit = -1e+16; for (auto& [y, w] : e[x]) { if (y == p) { continue; } auto v = ivec1d(ll, 4, UNDEF); auto& inp = g[idx]; auto& out = g[1 - idx]; v[0] = f[y][0]; for (int i = 0; i < 4; ++i) { if (f[y][i] == UNDEF) { continue; } chmax(v[(i + 1) % 4], f[y][i] + w); } for (int b = left; b <= right; ++b) { for (int p = 0; p < 2; ++p) { if (inp[b][p] == UNDEF) { continue; } chmax(out[b + 1][p], inp[b][p] + v[3]); chmax(out[b - 1][p], inp[b][p] + v[1]); chmax(out[b][1 - p], inp[b][p] + v[2]); chmax(out[b][p], inp[b][p] + v[0]); inp[b][p] = UNDEF; } } for (;;) { bool ok = false; for (int p = 0; p < 2 && !ok; ++p) { if (out[left - 1][p] > limit) { ok = true; break; } } if (!ok) { break; } --left; } for (;;) { bool ok = false; for (int p = 0; p < 2 && !ok; ++p) { if (out[right + 1][p] > limit) { ok = true; break; } } if (!ok) { break; } ++right; } // dbg(left, right, right - left, sz(e[x])); idx ^= 1; } auto& out = g[idx]; f[x][0] = out[offset][0]; f[x][1] = out[offset - 1][0]; f[x][2] = out[offset][1]; f[x][3] = out[offset + 1][0]; for (int i = 0; i < 4; ++i) { if (f[x][i] < limit) { f[x][i] = UNDEF; } } for (int b = left; b <= right; ++b) { for (int p = 0; p < 2; ++p) { for (int x = 0; x < 4; ++x) { out[b][p] = UNDEF; } } } }; dfs(0, -1); cout << f[0][0] << nl; #ifdef LOCAL cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl; #endif return 0; } |