#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 2e5 + 7; const int P = 250; const LL INF = 1e18; mt19937 rng(1998); int n; LL dp[N][4]; LL dp_aux[2][2 * P][2]; vector<pair<int, int>> G[N]; void dfs(int v, int f = -1) { vector<pair<int, int>> children; for (auto [u, c]: G[v]) { if (u != f) { dfs(u, v); children.push_back({u, c}); } } for (int j = 0; j < 4; j++) dp[v][j] = -INF; for (int t = 0; t < 6; t++) { shuffle(all(children), rng); for (int j = 0; j < 2 * P; j++) for (int k = 0; k < 2; k++) dp_aux[0][j][k] = dp_aux[1][j][k] = -INF; dp_aux[0][P][0] = 0; for (int i = 1; i <= sz(children); i++) { auto [u, c] = children[i - 1]; for (int j = max(0, P - i - 1); j < min(2 * P, P + i + 2); j++) { for (int k = 0; k < 2; k++) { dp_aux[i & 1][j][k] = max(-INF, dp_aux[(i - 1) & 1][j][k] + max(dp[u][0], dp[u][3] + c)); dp_aux[i & 1][j][k] = max(dp_aux[i & 1][j][k], dp_aux[(i - 1) & 1][j][k ^ 1] + dp[u][1] + c); if (j > 0) { dp_aux[i & 1][j][k] = max(dp_aux[i & 1][j][k], dp_aux[(i - 1) & 1][j - 1][k] + dp[u][2] + c); } if (j + 1 < 2 * P) { dp_aux[i & 1][j][k] = max(dp_aux[i & 1][j][k], dp_aux[(i - 1) & 1][j + 1][k] + dp[u][0] + c); } } } } dp[v][0] = max(dp[v][0], dp_aux[sz(children) & 1][P][0]); dp[v][2] = max(dp[v][2], dp_aux[sz(children) & 1][P][1]); dp[v][1] = max(dp[v][1], dp_aux[sz(children) & 1][P - 1][0]); dp[v][3] = max(dp[v][3], dp_aux[sz(children) & 1][P + 1][0]); } } int main() { _upgrade cin >> n; for (int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); } dfs(1); cout << dp[1][0] << "\n"; 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 2e5 + 7; const int P = 250; const LL INF = 1e18; mt19937 rng(1998); int n; LL dp[N][4]; LL dp_aux[2][2 * P][2]; vector<pair<int, int>> G[N]; void dfs(int v, int f = -1) { vector<pair<int, int>> children; for (auto [u, c]: G[v]) { if (u != f) { dfs(u, v); children.push_back({u, c}); } } for (int j = 0; j < 4; j++) dp[v][j] = -INF; for (int t = 0; t < 6; t++) { shuffle(all(children), rng); for (int j = 0; j < 2 * P; j++) for (int k = 0; k < 2; k++) dp_aux[0][j][k] = dp_aux[1][j][k] = -INF; dp_aux[0][P][0] = 0; for (int i = 1; i <= sz(children); i++) { auto [u, c] = children[i - 1]; for (int j = max(0, P - i - 1); j < min(2 * P, P + i + 2); j++) { for (int k = 0; k < 2; k++) { dp_aux[i & 1][j][k] = max(-INF, dp_aux[(i - 1) & 1][j][k] + max(dp[u][0], dp[u][3] + c)); dp_aux[i & 1][j][k] = max(dp_aux[i & 1][j][k], dp_aux[(i - 1) & 1][j][k ^ 1] + dp[u][1] + c); if (j > 0) { dp_aux[i & 1][j][k] = max(dp_aux[i & 1][j][k], dp_aux[(i - 1) & 1][j - 1][k] + dp[u][2] + c); } if (j + 1 < 2 * P) { dp_aux[i & 1][j][k] = max(dp_aux[i & 1][j][k], dp_aux[(i - 1) & 1][j + 1][k] + dp[u][0] + c); } } } } dp[v][0] = max(dp[v][0], dp_aux[sz(children) & 1][P][0]); dp[v][2] = max(dp[v][2], dp_aux[sz(children) & 1][P][1]); dp[v][1] = max(dp[v][1], dp_aux[sz(children) & 1][P - 1][0]); dp[v][3] = max(dp[v][3], dp_aux[sz(children) & 1][P + 1][0]); } } int main() { _upgrade cin >> n; for (int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); } dfs(1); cout << dp[1][0] << "\n"; return 0; } |