#include<bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; #define int long long const ll INF = 1e17; typedef pair<int,int> pii; vector<int> calc(vector<vector<int> > vals, int diff) { const int n = vals.size(); vector<int> setup(n); vector<set<pair<int,pii>, greater<pair<int,pii> > > > slacks(5); const vector<int> diffs = {0,-1,0,1}; auto vals2 = vals; for(auto &a : vals2) a[2] = max(a[2], a[0]); auto chan = [&](int i, int x) { for(int j=1;j<=3;j++) slacks[2 + diffs[j] - diffs[setup[i]]].erase({vals2[i][j]-vals2[i][setup[i]],{i,j}}); setup[i] = x; for(int j=1;j<=3;j++) slacks[2 + diffs[j] - diffs[setup[i]]].insert({vals2[i][j]-vals2[i][setup[i]],{i,j}}); }; for(int i=0;i<n;i++) { if(i == 0) { setup[i] = diff+2; } else setup[i] = 2; for(int j=1;j<=3;j++) slacks[2 + diffs[j] - diffs[setup[i]]].insert({vals2[i][j]-vals2[i][setup[i]],{i,j}}); } bool nfound = 1; do { nfound = 1; for(int C=0;nfound && C<5;C++) { auto it = slacks[C].begin(); if(it == slacks[C].end()) continue; int i = it->nd.st; for(int s=0;nfound && s<5;s++) { int t = 6-C-s; if(t < 0 || t >= 5) continue; const auto& sls = slacks[s]; const auto& slt = slacks[t]; auto itj = sls.begin(); while(itj != sls.end() && itj->nd.st == i) itj++; if(itj == sls.end()) continue; int j = itj->nd.st; auto itk = slt.begin(); while(itk != slt.end() && (itk->nd.st == i || itk->nd.st == j)) itk++; if(itk == slt.end()) continue; int k = itk->nd.st; vector<pii> lst = {{i,it->nd.nd}, {j, itj->nd.nd}, {k,itk->nd.nd}}; if(it->st + itj->st + itk->st > 0) { for(auto a:lst) chan(a.st, a.nd); nfound = 0; } } } }while(nfound == 0); for(int i=0;i<n;i++) if(setup[i] == 2) if(vals[i][0] == vals2[i][2]) setup[i] = 0; return setup; } int getScoreAndFixIfWrongParity(int reqParity, const vector<int>& setup, const vector<vector<int> >& vals) { const int n = setup.size(); vector<vector<vector<pii> > > swapsL(4,vector<vector<pii> >(4,vector<pii>(1,{INF,-1}))); int score = 0; for(int i=0;i<n;i++) { score += vals[i][setup[i]]; for(int j=0;j<4;j++) swapsL[setup[i]][j].push_back({vals[i][setup[i]] - vals[i][j], i}); } for(int i=0;i<4;i++) for(int j=0;j<4;j++) sort(all(swapsL[i][j])); auto route = [&](const vector<int>& a) { set<int> used; int res = 0; for(int i=0;i<a.size();i+=2) { const auto& sl = swapsL[a[i]][a[i+1]]; int x = 0; while(used.count(sl[x].nd)) x++; if(sl[x].nd != -1) used.insert(sl[x].nd); if(res >= INF) return res; res += sl[x].st; } return res; }; //<3 if(count(all(setup), 2)%2 != reqParity) { return score -= min({route({0,2}), route({2,0}), route({1,2,3,0}), route({3,2,1,0}), route({2,3,3,0}), route({2,1,1,0}), route({0,3,3,2}), route({0,1,1,2}), route({0,1,2,3}), route({0,3,2,1}), route({0,1,1,3,2,1}), route({0,3,3,1,2,3}), route({2,1,0,3}), route({2,3,0,1}), route({2,1,1,3,0,1}), route({2,3,3,1,0,3}), route({2,1,1,3,3,0}), route({2,3,3,1,1,0}), route({0,1,1,3,3,2}), route({0,3,3,1,1,2}), route({1,2,3,1,1,0}), route({1,0,1,2,3,1}), route({3,2,1,3,3,0}), route({3,0,3,2,1,3}), route({3,0,3,2,1,3}), route({3,2,3,0,1,3}), route({1,0,1,2,3,1}), route({1,2,1,0,3,1}), }); } return score; } vector<int> combine(vector<vector<int> > vals) { vector<vector<int> > res = {calc(vals,0), calc(vals,-1), {}, calc(vals,1)}; res[2] = res[0]; return { getScoreAndFixIfWrongParity(0, res[0], vals), getScoreAndFixIfWrongParity(0, res[1], vals), getScoreAndFixIfWrongParity(1, res[2], vals), getScoreAndFixIfWrongParity(0, res[3], vals), }; } vector<vector<pii> > graf; vector<int> combineValsViaDp(vector<vector<int> > vals) { const int n = vals.size(); function<int(int,int)> dpsolv = [&](int diff, int b)->int { vector<vector<int> > dp(n+1,vector<int>(2*n+1,-INF)); vector<vector<int> > dp2(n+1,vector<int>(2*n+1,-INF)); dp[0][n] = 0; for(int i=0;i<n;i++) { for(int j=0;j<2*n+1;j++) { dp[i+1][j] = dp[i][j] + vals[i][0]; dp[i+1][j] = max(dp[i+1][j], dp2[i][j] + vals[i][2]); if(j+1 < dp[i].size()) dp[i+1][j] = max(dp[i+1][j], dp[i][j+1] + vals[i][1]); if(j-1 >= 0) dp[i+1][j] = max(dp[i+1][j], dp[i][j-1] + vals[i][3]); dp2[i+1][j] = dp2[i][j] + vals[i][0]; dp2[i+1][j] = max(dp2[i+1][j], dp[i][j] + vals[i][2]); if(j+1 < dp2[i].size()) dp2[i+1][j] = max(dp2[i+1][j], dp2[i][j+1] + vals[i][1]); if(j-1 >= 0) dp2[i+1][j] = max(dp2[i+1][j], dp2[i][j-1] + vals[i][3]); } } if(n+diff < 0 || n+diff >= dp[0].size()) return -INF; if(b) return dp2[n][n+diff]; return dp[n][n+diff]; }; vector<int> res = {dpsolv(0,0), dpsolv(-1,0), dpsolv(0,1), dpsolv(1,0)}; return res; } vector<int> dfs(int cur, int p) { vector<vector<int> > vals; for(auto a:graf[cur]) if(a.st != p) { auto dp = dfs(a.st,cur); auto dpUp = {max(dp[3]+a.nd, dp[0]), dp[0]+a.nd, dp[1]+a.nd, dp[2]+a.nd}; vals.push_back(dpUp); } if(vals.size() <= 10) return combineValsViaDp(vals); return combine(vals); } int32_t main(){ ios::sync_with_stdio(false); srand(10494); cin.tie(0); int n; cin >> n; graf.resize(n); for(int i=0;i<n-1;i++){ int a,b,c; cin >> a >> b >> c; graf[a-1].push_back({b-1,c}); graf[b-1].push_back({a-1,c}); } for(auto &a: graf) random_shuffle(a.begin(), a.end()); cout<<dfs(n-1,-1)[0]<<"\n"; }
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | #include<bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; #define int long long const ll INF = 1e17; typedef pair<int,int> pii; vector<int> calc(vector<vector<int> > vals, int diff) { const int n = vals.size(); vector<int> setup(n); vector<set<pair<int,pii>, greater<pair<int,pii> > > > slacks(5); const vector<int> diffs = {0,-1,0,1}; auto vals2 = vals; for(auto &a : vals2) a[2] = max(a[2], a[0]); auto chan = [&](int i, int x) { for(int j=1;j<=3;j++) slacks[2 + diffs[j] - diffs[setup[i]]].erase({vals2[i][j]-vals2[i][setup[i]],{i,j}}); setup[i] = x; for(int j=1;j<=3;j++) slacks[2 + diffs[j] - diffs[setup[i]]].insert({vals2[i][j]-vals2[i][setup[i]],{i,j}}); }; for(int i=0;i<n;i++) { if(i == 0) { setup[i] = diff+2; } else setup[i] = 2; for(int j=1;j<=3;j++) slacks[2 + diffs[j] - diffs[setup[i]]].insert({vals2[i][j]-vals2[i][setup[i]],{i,j}}); } bool nfound = 1; do { nfound = 1; for(int C=0;nfound && C<5;C++) { auto it = slacks[C].begin(); if(it == slacks[C].end()) continue; int i = it->nd.st; for(int s=0;nfound && s<5;s++) { int t = 6-C-s; if(t < 0 || t >= 5) continue; const auto& sls = slacks[s]; const auto& slt = slacks[t]; auto itj = sls.begin(); while(itj != sls.end() && itj->nd.st == i) itj++; if(itj == sls.end()) continue; int j = itj->nd.st; auto itk = slt.begin(); while(itk != slt.end() && (itk->nd.st == i || itk->nd.st == j)) itk++; if(itk == slt.end()) continue; int k = itk->nd.st; vector<pii> lst = {{i,it->nd.nd}, {j, itj->nd.nd}, {k,itk->nd.nd}}; if(it->st + itj->st + itk->st > 0) { for(auto a:lst) chan(a.st, a.nd); nfound = 0; } } } }while(nfound == 0); for(int i=0;i<n;i++) if(setup[i] == 2) if(vals[i][0] == vals2[i][2]) setup[i] = 0; return setup; } int getScoreAndFixIfWrongParity(int reqParity, const vector<int>& setup, const vector<vector<int> >& vals) { const int n = setup.size(); vector<vector<vector<pii> > > swapsL(4,vector<vector<pii> >(4,vector<pii>(1,{INF,-1}))); int score = 0; for(int i=0;i<n;i++) { score += vals[i][setup[i]]; for(int j=0;j<4;j++) swapsL[setup[i]][j].push_back({vals[i][setup[i]] - vals[i][j], i}); } for(int i=0;i<4;i++) for(int j=0;j<4;j++) sort(all(swapsL[i][j])); auto route = [&](const vector<int>& a) { set<int> used; int res = 0; for(int i=0;i<a.size();i+=2) { const auto& sl = swapsL[a[i]][a[i+1]]; int x = 0; while(used.count(sl[x].nd)) x++; if(sl[x].nd != -1) used.insert(sl[x].nd); if(res >= INF) return res; res += sl[x].st; } return res; }; //<3 if(count(all(setup), 2)%2 != reqParity) { return score -= min({route({0,2}), route({2,0}), route({1,2,3,0}), route({3,2,1,0}), route({2,3,3,0}), route({2,1,1,0}), route({0,3,3,2}), route({0,1,1,2}), route({0,1,2,3}), route({0,3,2,1}), route({0,1,1,3,2,1}), route({0,3,3,1,2,3}), route({2,1,0,3}), route({2,3,0,1}), route({2,1,1,3,0,1}), route({2,3,3,1,0,3}), route({2,1,1,3,3,0}), route({2,3,3,1,1,0}), route({0,1,1,3,3,2}), route({0,3,3,1,1,2}), route({1,2,3,1,1,0}), route({1,0,1,2,3,1}), route({3,2,1,3,3,0}), route({3,0,3,2,1,3}), route({3,0,3,2,1,3}), route({3,2,3,0,1,3}), route({1,0,1,2,3,1}), route({1,2,1,0,3,1}), }); } return score; } vector<int> combine(vector<vector<int> > vals) { vector<vector<int> > res = {calc(vals,0), calc(vals,-1), {}, calc(vals,1)}; res[2] = res[0]; return { getScoreAndFixIfWrongParity(0, res[0], vals), getScoreAndFixIfWrongParity(0, res[1], vals), getScoreAndFixIfWrongParity(1, res[2], vals), getScoreAndFixIfWrongParity(0, res[3], vals), }; } vector<vector<pii> > graf; vector<int> combineValsViaDp(vector<vector<int> > vals) { const int n = vals.size(); function<int(int,int)> dpsolv = [&](int diff, int b)->int { vector<vector<int> > dp(n+1,vector<int>(2*n+1,-INF)); vector<vector<int> > dp2(n+1,vector<int>(2*n+1,-INF)); dp[0][n] = 0; for(int i=0;i<n;i++) { for(int j=0;j<2*n+1;j++) { dp[i+1][j] = dp[i][j] + vals[i][0]; dp[i+1][j] = max(dp[i+1][j], dp2[i][j] + vals[i][2]); if(j+1 < dp[i].size()) dp[i+1][j] = max(dp[i+1][j], dp[i][j+1] + vals[i][1]); if(j-1 >= 0) dp[i+1][j] = max(dp[i+1][j], dp[i][j-1] + vals[i][3]); dp2[i+1][j] = dp2[i][j] + vals[i][0]; dp2[i+1][j] = max(dp2[i+1][j], dp[i][j] + vals[i][2]); if(j+1 < dp2[i].size()) dp2[i+1][j] = max(dp2[i+1][j], dp2[i][j+1] + vals[i][1]); if(j-1 >= 0) dp2[i+1][j] = max(dp2[i+1][j], dp2[i][j-1] + vals[i][3]); } } if(n+diff < 0 || n+diff >= dp[0].size()) return -INF; if(b) return dp2[n][n+diff]; return dp[n][n+diff]; }; vector<int> res = {dpsolv(0,0), dpsolv(-1,0), dpsolv(0,1), dpsolv(1,0)}; return res; } vector<int> dfs(int cur, int p) { vector<vector<int> > vals; for(auto a:graf[cur]) if(a.st != p) { auto dp = dfs(a.st,cur); auto dpUp = {max(dp[3]+a.nd, dp[0]), dp[0]+a.nd, dp[1]+a.nd, dp[2]+a.nd}; vals.push_back(dpUp); } if(vals.size() <= 10) return combineValsViaDp(vals); return combine(vals); } int32_t main(){ ios::sync_with_stdio(false); srand(10494); cin.tie(0); int n; cin >> n; graf.resize(n); for(int i=0;i<n-1;i++){ int a,b,c; cin >> a >> b >> c; graf[a-1].push_back({b-1,c}); graf[b-1].push_back({a-1,c}); } for(auto &a: graf) random_shuffle(a.begin(), a.end()); cout<<dfs(n-1,-1)[0]<<"\n"; } |