#include <algorithm> #include <iostream> #include <array> #include <cassert> #include <vector> using namespace std; const int MAXN = 200001; const int MAXT = 1600; const long long INF = 1000000000000000000; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) using Edge = pair<int, long long>; using Top = array<long long, 4>; const Top LEAF = {0, -INF, -INF, -INF}; const Top FORBIDDEN = {-INF, -INF, -INF, -INF}; ostream& operator<<(ostream& os, const Top& t) { os << "{" << t[0] << ", " << t[1] << ", " << t[2] << ", " << t[3] << "}"; return os; } template<class T> const T& max(const T& a, const T&b, const T&c, const T&d) { return max(max(a,b), max(c,d)); } Top combine(vector<Top> & Res) { int n = Res.size(); random_shuffle(Res.begin(), Res.end()); // cout << "PRE " << n << endl; // FOR(i, n) { // cout << Res[i] << endl; // } int MID = n+1; vector<long long> T[2] = {vector<long long> (2*MID+3, -INF), vector<long long> (2*MID+3, -INF)}; vector<long long> T1[2] = {vector<long long> (2*MID+3, -INF), vector<long long> (2*MID+3, -INF)}; T[0][MID] = 0; for (int i = 0; i < n; i++) { int lim = min(i+1, MAXT); for (int j = -lim; j <= +lim; j++) { FOR(p, 2) { T1[p][MID+j] = max( T[!p][MID+j] + Res[i][2], T[p][MID+j-1] + Res[i][3], T[p][MID+j+1] + Res[i][1], T[p][MID+j] + Res[i][0] ); // cout << "T(" << i+1 << "," << p << "," << j << ") = " << T1[p][MID+j] << endl; } } swap(T1[0], T[0]); swap(T1[1], T[1]); } // cout << "POST " << n << endl; Top r = { T[0][MID], T[0][MID-1], T[1][MID], T[0][MID+1] }; // cout << " = " << r << endl; return r; } Top extend(Top a, long long val) { return { max(a[0], a[3] + val), a[0] + val, a[1] + val, a[2] + val }; } struct Node { vector<Edge> V; int state; Top value; Node():state(0){} } N[MAXN]; using para = pair<int,int>; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int a, b; long long v; FOR(i, n-1) { cin >> a >> b >> v; N[a-1].V.push_back(Edge(b-1, v)); N[b-1].V.push_back(Edge(a-1, v)); } vector<int> S; S.push_back(0); N[0].state = 1; while(!S.empty()) { int i = S.back(); if(N[i].state == 1) { N[i].state = 2; FOR(j, N[i].V.size()) { if(N[N[i].V[j].first].state == 0) { S.push_back(N[i].V[j].first); N[N[i].V[j].first].state = 1; } } } else if(N[i].state == 2) { vector<Top> Res; FOR(j, N[i].V.size()) { int k = N[i].V[j].first; if(N[k].state == 3) { Top p = extend(N[k].value, N[i].V[j].second); Res.push_back(p); } } N[i].state = 3; N[i].value = combine(Res); S.pop_back(); } } cout << N[0].value[0] << endl;; }
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 | #include <algorithm> #include <iostream> #include <array> #include <cassert> #include <vector> using namespace std; const int MAXN = 200001; const int MAXT = 1600; const long long INF = 1000000000000000000; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) using Edge = pair<int, long long>; using Top = array<long long, 4>; const Top LEAF = {0, -INF, -INF, -INF}; const Top FORBIDDEN = {-INF, -INF, -INF, -INF}; ostream& operator<<(ostream& os, const Top& t) { os << "{" << t[0] << ", " << t[1] << ", " << t[2] << ", " << t[3] << "}"; return os; } template<class T> const T& max(const T& a, const T&b, const T&c, const T&d) { return max(max(a,b), max(c,d)); } Top combine(vector<Top> & Res) { int n = Res.size(); random_shuffle(Res.begin(), Res.end()); // cout << "PRE " << n << endl; // FOR(i, n) { // cout << Res[i] << endl; // } int MID = n+1; vector<long long> T[2] = {vector<long long> (2*MID+3, -INF), vector<long long> (2*MID+3, -INF)}; vector<long long> T1[2] = {vector<long long> (2*MID+3, -INF), vector<long long> (2*MID+3, -INF)}; T[0][MID] = 0; for (int i = 0; i < n; i++) { int lim = min(i+1, MAXT); for (int j = -lim; j <= +lim; j++) { FOR(p, 2) { T1[p][MID+j] = max( T[!p][MID+j] + Res[i][2], T[p][MID+j-1] + Res[i][3], T[p][MID+j+1] + Res[i][1], T[p][MID+j] + Res[i][0] ); // cout << "T(" << i+1 << "," << p << "," << j << ") = " << T1[p][MID+j] << endl; } } swap(T1[0], T[0]); swap(T1[1], T[1]); } // cout << "POST " << n << endl; Top r = { T[0][MID], T[0][MID-1], T[1][MID], T[0][MID+1] }; // cout << " = " << r << endl; return r; } Top extend(Top a, long long val) { return { max(a[0], a[3] + val), a[0] + val, a[1] + val, a[2] + val }; } struct Node { vector<Edge> V; int state; Top value; Node():state(0){} } N[MAXN]; using para = pair<int,int>; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int a, b; long long v; FOR(i, n-1) { cin >> a >> b >> v; N[a-1].V.push_back(Edge(b-1, v)); N[b-1].V.push_back(Edge(a-1, v)); } vector<int> S; S.push_back(0); N[0].state = 1; while(!S.empty()) { int i = S.back(); if(N[i].state == 1) { N[i].state = 2; FOR(j, N[i].V.size()) { if(N[N[i].V[j].first].state == 0) { S.push_back(N[i].V[j].first); N[N[i].V[j].first].state = 1; } } } else if(N[i].state == 2) { vector<Top> Res; FOR(j, N[i].V.size()) { int k = N[i].V[j].first; if(N[k].state == 3) { Top p = extend(N[k].value, N[i].V[j].second); Res.push_back(p); } } N[i].state = 3; N[i].value = combine(Res); S.pop_back(); } } cout << N[0].value[0] << endl;; } |