fork download
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define PB push_back
  4. #define int long long
  5. #define st first
  6. #define nd second
  7. #define rd third
  8. #define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
  9. #define RE(i, n) FOR(i, 1, n)
  10. #define FORD(i, a, b) for(int i = (a); i >= (b); --i)
  11. #define REP(i, n) for(int i = 0;i <(n); ++i)
  12. #define VAR(v, i) __typeof(i) v=(i)
  13. #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).begin(); ++i)
  14. #define ALL(x) (x).begin(), (x).begin()
  15. #define SZ(x) ((int)(x).size())
  16. #define __builtin_ctz __builtin_ctzll
  17. #define __builtin_clz __builtin_clzll
  18. #define __builtin_popcount __builtin_popcountll
  19. using namespace std;
  20. template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
  21. template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  22. while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
  23. }
  24. #ifdef LOCAL
  25. #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
  26. #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
  27. #else
  28. #define debug(...) (__VA_ARGS__)
  29. #define debugv(x)
  30. #define cerr if(0)cout
  31. #endif
  32. #define left ____left
  33. #define hash ____hash
  34. #define exchange dupa
  35. typedef long long ll;
  36. typedef long double LD;
  37. typedef pair<int, int> PII;
  38. typedef pair<ll, ll> PLL;
  39. typedef vector<int> VI;
  40. typedef vector<VI> VVI;
  41. typedef vector<ll> VLL;
  42. typedef vector<pair<int, int> > VPII;
  43. typedef vector<pair<ll, ll> > VPLL;
  44.  
  45. template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
  46. template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
  47. template<class T1, class T2>
  48. ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
  49. template<class A, class B, class C> struct Triple { A first; B second; C third;
  50. bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
  51. template<class T> void ResizeVec(T&, vector<int>) {}
  52. template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  53. vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  54. for (T& v : vec) { ResizeVec(v, sz); }
  55. }
  56. typedef Triple<int, int, int> TIII;
  57. template<class A, class B, class C>
  58. ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
  59. template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  60. template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  61. template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  62.  
  63. const int N = 2e5 + 5;
  64. VPII slo[N];
  65. int dp[N][4]; // dp[v][c] - najwiekszy zysk jaki mozna uzyskac w poddrzewie v majac niedokonczona sciezke w dol o dlugosci c
  66. int vis[N];
  67. const int kInf = 1e12;
  68. int from_to[4][4];
  69. typedef array<int, 4> A;
  70. set<PII> exchange[4][4];
  71. set<PII> merged02[4][4];
  72. void Dfs(int v) {
  73. vis[v] = 1;
  74. vector<A> sons;
  75. for (auto e : slo[v]) {
  76. int nei = e.st;
  77. int cost = e.nd;
  78. if (vis[nei]) { continue; }
  79. Dfs(nei);
  80. A son;
  81. REP (c, 4) {
  82. son[c] = cost + dp[nei][(c + 3) % 4];
  83. }
  84. maxi(son[0], dp[nei][0]);
  85. sons.PB(son);
  86. }
  87. A B;
  88. REP (tr, 4) {
  89. B[tr] = -kInf / 2;
  90. }
  91. REP (artificial, 4) {
  92. int best = -kInf;
  93. sons.PB(B);
  94. sons.back()[artificial] = 0;
  95. //debug(v, artificial, sons);
  96. REP (x, 4) {
  97. REP (y, 4) {
  98. exchange[x][y].clear();
  99. merged02[x][y].clear();
  100. }
  101. }
  102. int score = 0;
  103. int parity = 0;
  104. VI where(SZ(sons), -1);
  105. function<void(int)> Del = [&](int who) {
  106. int ind = where[who];
  107. where[who] = -1;
  108. auto& vec = sons[who];
  109. score -= vec[ind];
  110. if (ind == 2) { parity ^= 1; }
  111. int i2 = ind;
  112. if (ind == 2) { i2 = 0; }
  113. REP (to, 4) {
  114. if (to == ind) { continue; }
  115. assert(exchange[ind][to].erase({vec[ind] - vec[to], who}));
  116. int t2 = to;
  117. if (to == 2) { t2 = 0; }
  118. merged02[i2][t2].erase({vec[ind] - vec[to], who});
  119. }
  120. };
  121. function<void(int, int)> Add = [&](int who, int ind) {
  122. auto& vec = sons[who];
  123. score += vec[ind];
  124. assert(where[who] == -1);
  125. where[who] = ind;
  126. if (ind == 2) { parity ^= 1; }
  127. int i2 = ind;
  128. if (ind == 2) { i2 = 0; }
  129. REP (to, 4) {
  130. if (to == ind) { continue; }
  131. exchange[ind][to].insert({vec[ind] - vec[to], who});
  132. int t2 = to;
  133. if (to == 2) { t2 = 0; }
  134. merged02[i2][t2].insert({vec[ind] - vec[to], who});
  135. }
  136. };
  137. function<void(int, int)> Move = [&](int who, int to) {
  138. Del(who);
  139. Add(who, to);
  140. };
  141. function<void()> UpdTable = [&]() {
  142. REP (from, 4) {
  143. REP (to, 4) {
  144. if (from == to) { continue; }
  145. if (exchange[from][to].empty()) {
  146. from_to[from][to] = -kInf;
  147. } else {
  148. from_to[from][to] = -exchange[from][to].begin()->st;
  149. }
  150. }
  151. }
  152. };
  153. REP (i, SZ(sons)) {
  154. auto& vec = sons[i];
  155. if (vec[0] > vec[2]) {
  156. Add(i, 0);
  157. } else {
  158. Add(i, 2);
  159. }
  160. }
  161. FOR (k, 0, SZ(sons) / 2) {
  162. UpdTable();
  163. if (parity == 0) {
  164. maxi(best, score);
  165. //debug(k, score);
  166. } else {
  167. int cand = -kInf;
  168. maxi(cand, from_to[0][2]);
  169. maxi(cand, from_to[2][0]);
  170. maxi(cand, from_to[0][3] + from_to[3][1] + from_to[1][2]);
  171. maxi(cand, from_to[0][1] + from_to[1][2]);
  172. maxi(cand, from_to[2][1] + from_to[1][0]);
  173. maxi(cand, from_to[2][1] + from_to[1][3] + from_to[3][0]);
  174. maxi(cand, from_to[0][1] + from_to[1][3] + from_to[3][2]);
  175. maxi(cand, from_to[2][3] + from_to[3][1] + from_to[1][0]);
  176. maxi(cand, from_to[0][3] + from_to[3][2]);
  177. maxi(cand, from_to[2][3] + from_to[3][0]);
  178. maxi(best, cand + score);
  179. assert(cand <= 0);
  180. }
  181. if (k == SZ(sons) / 2) { break; }
  182. assert(SZ(merged02[0][1]) == SZ(sons) - 2 * k);
  183. int c1 = -kInf;
  184. int c11 = -kInf;
  185. int c12 = -kInf;
  186. PII best_to_1 = *merged02[0][1].begin();
  187. PII best_to_3 = *merged02[0][3].begin();
  188. if (best_to_1.nd != best_to_3.nd) {
  189. c1 = -best_to_1.st - best_to_3.st;
  190. } else {
  191. c11 = -best_to_1.st - next(merged02[0][3].begin())->st;
  192. c12 = -next(merged02[0][1].begin())->st - best_to_3.st;
  193. }
  194. int c2 = -kInf, c3 = -kInf;
  195. if (k > 0) {
  196. c2 = -merged02[0][1].begin()->st - next(merged02[0][1].begin())->st - merged02[1][3].begin()->st; // 1->3, 02->1, 02->1
  197. c3 = -merged02[0][3].begin()->st - next(merged02[0][3].begin())->st - merged02[3][1].begin()->st; // 3->1, 02->3, 02->3
  198. }
  199. int M = max({c1, c11, c12, c2, c3});
  200. if (c1 == M) {
  201. Move(best_to_1.nd, 1);
  202. Move(best_to_3.nd, 3);
  203. } else if (c11 == M) {
  204. Move(next(merged02[0][3].begin())->nd, 3);
  205. Move(best_to_1.nd, 1);
  206. } else if (c12 == M) {
  207. Move(next(merged02[0][1].begin())->nd, 1);
  208. Move(best_to_3.nd, 3);
  209. } else if (c2 == M) {
  210. Move(merged02[1][3].begin()->nd, 3);
  211. Move(next(merged02[0][1].begin())->nd, 1);
  212. Move(merged02[0][1].begin()->nd, 1);
  213. } else {
  214. assert(c3 == M);
  215. Move(merged02[3][1].begin()->nd, 1);
  216. Move(next(merged02[0][3].begin())->nd, 3);
  217. Move(merged02[0][3].begin()->nd, 3);
  218. }
  219. }
  220. dp[v][(4 - artificial) % 4] = best;
  221. sons.pop_back();
  222. }
  223. //debug(v);
  224. // REP (i, 4) {
  225. // cerr<<dp[v][i]<<" ";
  226. // }
  227. // cerr<<endl;
  228. }
  229. int32_t main() {
  230.  
  231. ios_base::sync_with_stdio(0);
  232. cout << fixed << setprecision(10);
  233. cerr << fixed << setprecision(10);
  234. cin.tie(0);
  235. //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  236. //#define TEST
  237. int n;
  238. #ifdef TEST
  239. n = 2e5;
  240. int R = 1000000;
  241. #else
  242. cin>>n;
  243. #endif
  244. RE (i, n - 1) {
  245. int a, b, c;
  246. #ifndef TEST
  247. cin>>a>>b>>c;
  248. #else
  249. a = i + 1;
  250. b = 1 + rand() % i;
  251. c = -R + rand() % (2 * R);
  252. #endif
  253. slo[a].PB({b, c});
  254. slo[b].PB({a, c});
  255. }
  256. Dfs(1);
  257. cout<<dp[1][0]<<endl;
  258.  
  259.  
  260.  
  261.  
  262.  
  263. return 0;
  264. }
  265.  
Runtime error #stdin #stdout 0.01s 7968KB
stdin
Standard input is empty
stdout
Standard output is empty