#include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; using std::int64_t; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } namespace chacha_private { template <int shift> void rotate_left(std::uint32_t &x) { x = (x << shift) | (x >> (32 - shift)); } inline void quarter_round(std::uint32_t &a, std::uint32_t &b, std::uint32_t &c, std::uint32_t &d) { a += b; d ^= a; rotate_left<16>(d); c += d; b ^= c; rotate_left<12>(b); a += b; d ^= a; rotate_left<8>(d); c += d; b ^= c; rotate_left<7>(b); } } template<int rounds> void chacha(const std::uint32_t (&key)[8], const std::uint64_t nonce, const std::uint64_t counter, std::uint32_t (&output)[16]) { static_assert(rounds == 8 || rounds == 12 || rounds == 20); using namespace chacha_private; std::uint32_t input[16]; std::memcpy(input + 0, "expand 32-byte k", 4 * sizeof(std::uint32_t)); std::memcpy(input + 4, key, 8 * sizeof(std::uint32_t)); std::memcpy(input + 12, &counter, 2 * sizeof(std::uint32_t)); std::memcpy(input + 14, &nonce, 2 * sizeof(std::uint32_t)); std::uint32_t x[16]; std::memcpy(x, input, 16 * sizeof(std::uint32_t)); for (int double_round = 0; double_round < rounds / 2; ++double_round) { quarter_round(x[0], x[4], x[8], x[12]); quarter_round(x[1], x[5], x[9], x[13]); quarter_round(x[2], x[6], x[10], x[14]); quarter_round(x[3], x[7], x[11], x[15]); quarter_round(x[0], x[5], x[10], x[15]); quarter_round(x[1], x[6], x[11], x[12]); quarter_round(x[2], x[7], x[8], x[13]); quarter_round(x[3], x[4], x[9], x[14]); } for (int i = 0; i < 16; ++i) { x[i] += input[i]; } std::memcpy(output, x, 16 * sizeof(std::uint32_t)); } class ChachaRandom { public: using result_type = std::uint32_t; constexpr result_type min() { return 0; } constexpr result_type max() { return std::numeric_limits<result_type>::max(); } explicit ChachaRandom(const std::uint32_t (&key)[8]); std::uint32_t operator()() { if (m_buffer_next == 16) { refill_buffer(); } return m_buffer[m_buffer_next++]; } private: void refill_buffer(); std::uint32_t m_chacha_key[8]; static constexpr std::uint64_t m_chacha_nonce = 0; std::uint64_t m_chacha_counter = 0; std::uint32_t m_buffer[16]; int m_buffer_next = 16; }; ChachaRandom::ChachaRandom(const std::uint32_t (&key)[8]) { std::memcpy(m_chacha_key, key, 8 * sizeof(std::uint32_t)); } void ChachaRandom::refill_buffer() { chacha<8>(m_chacha_key, m_chacha_nonce, m_chacha_counter++, m_buffer); m_buffer_next = 0; } ChachaRandom rng {{ 0x6ef8bdb2, 0x29173e28, 0x3045720a, 0x1554270c, 0xf4b3032, 0xbbe13b97, 0xf88df4ec, 0x3a83bd73, }}; constexpr int root = 0; constexpr int vertex_none = -1; constexpr int64_t infinity = 1LL << 60; struct Vertex { int parent = vertex_none; int edge_begin = 0; int edge_end = 0; int64_t subtree_value[4] = {}; // index = extra path from root }; struct Edge { int from = 0; int to = 0; int weight = 0; }; int num_vertices; std::vector<Vertex> vertices; std::vector<Edge> edges; void read_tree() { std::cin >> num_vertices; vertices.resize(num_vertices); edges.reserve(2 * (num_vertices - 1)); REP(i, num_vertices - 1) { Edge edge; std::cin >> edge.from >> edge.to >> edge.weight; --edge.from; --edge.to; edges.push_back(edge); std::swap(edge.from, edge.to); edges.push_back(edge); } std::sort(edges.begin(), edges.end(), [](const Edge &a, const Edge &b) { return a.from < b.from; }); int edge_next = 0; REP(i, num_vertices) { Vertex &v = vertices[i]; v.edge_begin = edge_next; while (edge_next != int(edges.size()) && edges[edge_next].from == i) { ++edge_next; } v.edge_end = edge_next; } assert(edge_next == int(edges.size())); } void initialize(int64_t *new_score, int64_t *score, int len, int64_t add) { int i = 0; while (i + 4 <= len) { auto score0 = score[i]; auto score1 = score[i+1]; auto score2 = score[i+2]; auto score3 = score[i+3]; new_score[i] = score0 + add; new_score[i+1] = score1 + add; new_score[i+2] = score2 + add; new_score[i+3] = score3 + add; i += 4; } while (i < len) { new_score[i] = score[i] + add; i += 1; } } void improve(int64_t *new_score, int64_t *score, int len, int64_t add) { int i = 0; while (i + 4 <= len) { auto score0 = score[i]; auto score1 = score[i+1]; auto score2 = score[i+2]; auto score3 = score[i+3]; new_score[i] = std::max(new_score[i], score0 + add); new_score[i+1] = std::max(new_score[i+1], score1 + add); new_score[i+2] = std::max(new_score[i+2], score2 + add); new_score[i+3] = std::max(new_score[i+3], score3 + add); i += 4; } while (i < len) { new_score[i] = std::max(new_score[i], score[i] + add); i += 1; } } void solve_vertex(Vertex &v) { // Worst case: 50000 * 3 + 50000 * 1. // Random walk +-1 of len 50000 has stddev 223. // 1000 is 4.47 sigma, good enough. static constexpr int max_bias = 1000; // score[p][max_bias + b] = best score for: // p = number of "2" children mod 2 // b = number of "3" children - number of "1" children static int64_t score[2][2 * max_bias + 1]; static int64_t new_score[2][2 * max_bias + 1]; REP(p, 2) REP(b, 2 * max_bias + 1) score[p][b] = -infinity; score[0][max_bias] = 0; std::shuffle(edges.begin() + v.edge_begin, edges.begin() + v.edge_end, rng); for (int edge_idx = v.edge_begin; edge_idx != v.edge_end; ++edge_idx) { const Edge &edge = edges[edge_idx]; if (edge.to == v.parent) continue; const Vertex &child = vertices[edge.to]; // Don't use edge. REP(p, 2) { initialize(new_score[p], score[p], 2 * max_bias + 1, child.subtree_value[0]); } // 0 = edge + 3 in child // parity = 0 // bias = 0 if (child.subtree_value[3] > -infinity/2) { REP(p, 2) { improve(new_score[p], score[p], 2 * max_bias + 1, edge.weight + child.subtree_value[3]); } } // 1 = edge + 0 in child // parity = 0 // bias = -1 if (child.subtree_value[0] > -infinity/2) { REP(p, 2) { improve(new_score[p], score[p] + 1, 2 * max_bias, edge.weight + child.subtree_value[0]); } } // 2 = edge + 1 in child // parity = 1 // bias = 0 if (child.subtree_value[1] > -infinity/2) { REP(p, 2) { improve(new_score[p], score[p ^ 1], 2 * max_bias + 1, edge.weight + child.subtree_value[1]); } } // 3 = edge + 2 in child // parity = 0 // bias = 1 if (child.subtree_value[2] > -infinity/2) { REP(p, 2) { improve(new_score[p] + 1, score[p], 2 * max_bias, edge.weight + child.subtree_value[2]); } } std::memcpy(&score, &new_score, sizeof(score)); } v.subtree_value[0] = score[0][max_bias]; v.subtree_value[1] = score[0][max_bias - 1]; v.subtree_value[2] = score[1][max_bias]; v.subtree_value[3] = score[0][max_bias + 1]; } enum class Stage { enter, finish, }; struct StackElem { int vertex; Stage stage; }; int64_t solve() { std::vector<StackElem> stack; stack.reserve(num_vertices); vertices[root].parent = vertex_none; stack.push_back(StackElem { root, Stage::enter }); while (!stack.empty()) { StackElem elem = stack.back(); stack.pop_back(); Vertex &v = vertices[elem.vertex]; if (elem.stage == Stage::enter) { stack.push_back(StackElem { elem.vertex, Stage::finish }); for (int edge_idx = v.edge_begin; edge_idx != v.edge_end; ++edge_idx) { const Edge &edge = edges[edge_idx]; if (edge.to == v.parent) continue; vertices[edge.to].parent = elem.vertex; stack.push_back(StackElem { edge.to, Stage::enter }); } } else { solve_vertex(v); } } return vertices[root].subtree_value[0]; } int main() { init_io(); read_tree(); const auto res = solve(); std::cout << res << '\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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | #include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; using std::int64_t; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } namespace chacha_private { template <int shift> void rotate_left(std::uint32_t &x) { x = (x << shift) | (x >> (32 - shift)); } inline void quarter_round(std::uint32_t &a, std::uint32_t &b, std::uint32_t &c, std::uint32_t &d) { a += b; d ^= a; rotate_left<16>(d); c += d; b ^= c; rotate_left<12>(b); a += b; d ^= a; rotate_left<8>(d); c += d; b ^= c; rotate_left<7>(b); } } template<int rounds> void chacha(const std::uint32_t (&key)[8], const std::uint64_t nonce, const std::uint64_t counter, std::uint32_t (&output)[16]) { static_assert(rounds == 8 || rounds == 12 || rounds == 20); using namespace chacha_private; std::uint32_t input[16]; std::memcpy(input + 0, "expand 32-byte k", 4 * sizeof(std::uint32_t)); std::memcpy(input + 4, key, 8 * sizeof(std::uint32_t)); std::memcpy(input + 12, &counter, 2 * sizeof(std::uint32_t)); std::memcpy(input + 14, &nonce, 2 * sizeof(std::uint32_t)); std::uint32_t x[16]; std::memcpy(x, input, 16 * sizeof(std::uint32_t)); for (int double_round = 0; double_round < rounds / 2; ++double_round) { quarter_round(x[0], x[4], x[8], x[12]); quarter_round(x[1], x[5], x[9], x[13]); quarter_round(x[2], x[6], x[10], x[14]); quarter_round(x[3], x[7], x[11], x[15]); quarter_round(x[0], x[5], x[10], x[15]); quarter_round(x[1], x[6], x[11], x[12]); quarter_round(x[2], x[7], x[8], x[13]); quarter_round(x[3], x[4], x[9], x[14]); } for (int i = 0; i < 16; ++i) { x[i] += input[i]; } std::memcpy(output, x, 16 * sizeof(std::uint32_t)); } class ChachaRandom { public: using result_type = std::uint32_t; constexpr result_type min() { return 0; } constexpr result_type max() { return std::numeric_limits<result_type>::max(); } explicit ChachaRandom(const std::uint32_t (&key)[8]); std::uint32_t operator()() { if (m_buffer_next == 16) { refill_buffer(); } return m_buffer[m_buffer_next++]; } private: void refill_buffer(); std::uint32_t m_chacha_key[8]; static constexpr std::uint64_t m_chacha_nonce = 0; std::uint64_t m_chacha_counter = 0; std::uint32_t m_buffer[16]; int m_buffer_next = 16; }; ChachaRandom::ChachaRandom(const std::uint32_t (&key)[8]) { std::memcpy(m_chacha_key, key, 8 * sizeof(std::uint32_t)); } void ChachaRandom::refill_buffer() { chacha<8>(m_chacha_key, m_chacha_nonce, m_chacha_counter++, m_buffer); m_buffer_next = 0; } ChachaRandom rng {{ 0x6ef8bdb2, 0x29173e28, 0x3045720a, 0x1554270c, 0xf4b3032, 0xbbe13b97, 0xf88df4ec, 0x3a83bd73, }}; constexpr int root = 0; constexpr int vertex_none = -1; constexpr int64_t infinity = 1LL << 60; struct Vertex { int parent = vertex_none; int edge_begin = 0; int edge_end = 0; int64_t subtree_value[4] = {}; // index = extra path from root }; struct Edge { int from = 0; int to = 0; int weight = 0; }; int num_vertices; std::vector<Vertex> vertices; std::vector<Edge> edges; void read_tree() { std::cin >> num_vertices; vertices.resize(num_vertices); edges.reserve(2 * (num_vertices - 1)); REP(i, num_vertices - 1) { Edge edge; std::cin >> edge.from >> edge.to >> edge.weight; --edge.from; --edge.to; edges.push_back(edge); std::swap(edge.from, edge.to); edges.push_back(edge); } std::sort(edges.begin(), edges.end(), [](const Edge &a, const Edge &b) { return a.from < b.from; }); int edge_next = 0; REP(i, num_vertices) { Vertex &v = vertices[i]; v.edge_begin = edge_next; while (edge_next != int(edges.size()) && edges[edge_next].from == i) { ++edge_next; } v.edge_end = edge_next; } assert(edge_next == int(edges.size())); } void initialize(int64_t *new_score, int64_t *score, int len, int64_t add) { int i = 0; while (i + 4 <= len) { auto score0 = score[i]; auto score1 = score[i+1]; auto score2 = score[i+2]; auto score3 = score[i+3]; new_score[i] = score0 + add; new_score[i+1] = score1 + add; new_score[i+2] = score2 + add; new_score[i+3] = score3 + add; i += 4; } while (i < len) { new_score[i] = score[i] + add; i += 1; } } void improve(int64_t *new_score, int64_t *score, int len, int64_t add) { int i = 0; while (i + 4 <= len) { auto score0 = score[i]; auto score1 = score[i+1]; auto score2 = score[i+2]; auto score3 = score[i+3]; new_score[i] = std::max(new_score[i], score0 + add); new_score[i+1] = std::max(new_score[i+1], score1 + add); new_score[i+2] = std::max(new_score[i+2], score2 + add); new_score[i+3] = std::max(new_score[i+3], score3 + add); i += 4; } while (i < len) { new_score[i] = std::max(new_score[i], score[i] + add); i += 1; } } void solve_vertex(Vertex &v) { // Worst case: 50000 * 3 + 50000 * 1. // Random walk +-1 of len 50000 has stddev 223. // 1000 is 4.47 sigma, good enough. static constexpr int max_bias = 1000; // score[p][max_bias + b] = best score for: // p = number of "2" children mod 2 // b = number of "3" children - number of "1" children static int64_t score[2][2 * max_bias + 1]; static int64_t new_score[2][2 * max_bias + 1]; REP(p, 2) REP(b, 2 * max_bias + 1) score[p][b] = -infinity; score[0][max_bias] = 0; std::shuffle(edges.begin() + v.edge_begin, edges.begin() + v.edge_end, rng); for (int edge_idx = v.edge_begin; edge_idx != v.edge_end; ++edge_idx) { const Edge &edge = edges[edge_idx]; if (edge.to == v.parent) continue; const Vertex &child = vertices[edge.to]; // Don't use edge. REP(p, 2) { initialize(new_score[p], score[p], 2 * max_bias + 1, child.subtree_value[0]); } // 0 = edge + 3 in child // parity = 0 // bias = 0 if (child.subtree_value[3] > -infinity/2) { REP(p, 2) { improve(new_score[p], score[p], 2 * max_bias + 1, edge.weight + child.subtree_value[3]); } } // 1 = edge + 0 in child // parity = 0 // bias = -1 if (child.subtree_value[0] > -infinity/2) { REP(p, 2) { improve(new_score[p], score[p] + 1, 2 * max_bias, edge.weight + child.subtree_value[0]); } } // 2 = edge + 1 in child // parity = 1 // bias = 0 if (child.subtree_value[1] > -infinity/2) { REP(p, 2) { improve(new_score[p], score[p ^ 1], 2 * max_bias + 1, edge.weight + child.subtree_value[1]); } } // 3 = edge + 2 in child // parity = 0 // bias = 1 if (child.subtree_value[2] > -infinity/2) { REP(p, 2) { improve(new_score[p] + 1, score[p], 2 * max_bias, edge.weight + child.subtree_value[2]); } } std::memcpy(&score, &new_score, sizeof(score)); } v.subtree_value[0] = score[0][max_bias]; v.subtree_value[1] = score[0][max_bias - 1]; v.subtree_value[2] = score[1][max_bias]; v.subtree_value[3] = score[0][max_bias + 1]; } enum class Stage { enter, finish, }; struct StackElem { int vertex; Stage stage; }; int64_t solve() { std::vector<StackElem> stack; stack.reserve(num_vertices); vertices[root].parent = vertex_none; stack.push_back(StackElem { root, Stage::enter }); while (!stack.empty()) { StackElem elem = stack.back(); stack.pop_back(); Vertex &v = vertices[elem.vertex]; if (elem.stage == Stage::enter) { stack.push_back(StackElem { elem.vertex, Stage::finish }); for (int edge_idx = v.edge_begin; edge_idx != v.edge_end; ++edge_idx) { const Edge &edge = edges[edge_idx]; if (edge.to == v.parent) continue; vertices[edge.to].parent = elem.vertex; stack.push_back(StackElem { edge.to, Stage::enter }); } } else { solve_vertex(v); } } return vertices[root].subtree_value[0]; } int main() { init_io(); read_tree(); const auto res = solve(); std::cout << res << '\n'; } |