#include <bits/stdc++.h> using namespace std; long long n, t, a, b, c; const int N = 200001; const int B_MAX = 500000; const long long inf = 1e15; vector<pair<int, int> >v[N]; long long mx[N][4]; pair<long long, int> tmp_mx[4][2]; bool w[N]; priority_queue<pair<long long, long long>> q[4]; set<int>s; int bnum[4]; int b_op; void back(int i) { for (int j = 0; j < 2; j++) { if (tmp_mx[i][0].second != -1) { q[i].push(tmp_mx[i][0]); } } } long long h(int p, int pp, int z) { for (int i = 0; i < 4; i++) { while(!q[i].empty()) {q[i].pop();} } s.clear(); for (int i = 0; i < v[p].size(); i++) { int a = v[p][i].first; if (a != pp && a != z) { long long c = v[p][i].second; q[0].push(make_pair(c, a)); q[1].push(make_pair(mx[a][1] - mx[a][0] + c, a)); q[2].push(make_pair(mx[a][2] - mx[a][0] + c, a)); q[3].push(make_pair(mx[a][3] - mx[a][0] + c, a)); } } long long res = 0; while (true) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { while(!q[i].empty() && (s.find(q[i].top().second) != s.end() || q[i].top().second == -1)) { q[i].pop(); } if (!q[i].empty()) { tmp_mx[i][j] = q[i].top(); q[i].pop(); } else { tmp_mx[i][j] = make_pair(-inf, -1); } } } long long mx11 = 0; if (tmp_mx[1][0].second != tmp_mx[1][1].second && tmp_mx[1][1].second != -1) { mx11 = tmp_mx[1][0].first + tmp_mx[1][1].first; } long long mx02 = 0; int a0 = -1, a2 = -1; if (tmp_mx[0][0].second != -1 && tmp_mx[2][0].second != -1) { if (tmp_mx[0][0].second != tmp_mx[2][0].second) { mx02 = tmp_mx[0][0].first + tmp_mx[2][0].first; a0 = 0; a2 = 0; } if (tmp_mx[0][1].second != -1 && tmp_mx[0][1].first + tmp_mx[2][0].first > mx02) { mx02 = tmp_mx[0][1].first + tmp_mx[2][0].first; a0 = 1; a2 = 0; } if (tmp_mx[2][1].second != -1 && tmp_mx[0][0].first + tmp_mx[2][1].first > mx02) { mx02 = tmp_mx[0][0].first + tmp_mx[2][1].first; a0 = 0; a2 = 1; } } long long mx33 = max(0LL, tmp_mx[3][0].first) + max(0LL, tmp_mx[3][1].first); if (mx02 <= 0 && mx11 <= 0 && mx33 <= 0) { return res; } if (mx02 > mx11 && mx02 > mx33) { res += mx02; s.insert(tmp_mx[0][a0].second); s.insert(tmp_mx[2][a2].second); q[0].push(tmp_mx[0][(a0 + 1) % 2]); q[2].push(tmp_mx[2][(a2 + 1) % 2]); back(1); back(3); } else if (mx11 > mx33) { res += mx11; s.insert(tmp_mx[1][0].second); s.insert(tmp_mx[1][1].second); back(0); back(2); back(3); } else { res += mx33; s.insert(tmp_mx[3][0].second); s.insert(tmp_mx[3][1].second); back(0); back(1); back(2); } } return res; } long long br(int p, int pp, int i, int z) { if (i == v[p].size()) { if (bnum[0] == bnum[2] && (bnum[1] % 2) == 0) { return 0; } return -inf; } long long a = v[p][i].first; long long res = br(p, pp, i + 1, z); if (a != pp && a != z) { long long c = v[p][i].second; bnum[0]++; res = max(res, c + br(p, pp, i + 1, z)); bnum[0]--; for (int j = 1; j < 4; j++) { bnum[j]++; res = max(res, c + mx[a][j] - mx[a][0] + br(p, pp, i + 1, z)); bnum[j]--; } } return res; } void r(int p, int pp) { for (int i = 0; i < v[p].size(); i++) { int a = v[p][i].first; if (a != pp) { r(a, p); mx[p][0] += mx[a][0]; } } long long child_sum = mx[p][0]; int kr = v[p].size() - 1; long long kr5 = (kr > 3 && kr < 9) ? pow(5, kr) : B_MAX; if (kr5 + b_op < B_MAX) { mx[p][0] += max(0LL, br(p, pp, 0, -1)); b_op += kr5; } else { mx[p][0] += max(0LL, h(p, pp, -1)); } for (int i = 0; i < v[p].size(); i++) { int a = v[p][i].first; if (a != pp) { long long c = v[p][i].second; long long bez = 0; if (kr5 + b_op < B_MAX) { bez = br(p, pp, 0, a); b_op += kr5; } else { bez = h(p, pp, a); } for (int j = 1; j < 4; j++) { mx[p][j] = max(mx[p][j], child_sum - mx[a][0] + bez + mx[a][j - 1] + c); } } } } void init(int a) { mx[a][0] = 0; mx[a][1] = -inf; mx[a][2] = -inf; mx[a][3] = -inf; } int main() { ios_base::sync_with_stdio(0); cin >> n; init(0); for(int i = 1; i < n; i++) { init(i); cin >> a >> b >> c; a--; b--; v[a].push_back(make_pair(b, c)); v[b].push_back(make_pair(a, c)); } r(0, -1); cout << mx[0][0] << endl; 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 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 | #include <bits/stdc++.h> using namespace std; long long n, t, a, b, c; const int N = 200001; const int B_MAX = 500000; const long long inf = 1e15; vector<pair<int, int> >v[N]; long long mx[N][4]; pair<long long, int> tmp_mx[4][2]; bool w[N]; priority_queue<pair<long long, long long>> q[4]; set<int>s; int bnum[4]; int b_op; void back(int i) { for (int j = 0; j < 2; j++) { if (tmp_mx[i][0].second != -1) { q[i].push(tmp_mx[i][0]); } } } long long h(int p, int pp, int z) { for (int i = 0; i < 4; i++) { while(!q[i].empty()) {q[i].pop();} } s.clear(); for (int i = 0; i < v[p].size(); i++) { int a = v[p][i].first; if (a != pp && a != z) { long long c = v[p][i].second; q[0].push(make_pair(c, a)); q[1].push(make_pair(mx[a][1] - mx[a][0] + c, a)); q[2].push(make_pair(mx[a][2] - mx[a][0] + c, a)); q[3].push(make_pair(mx[a][3] - mx[a][0] + c, a)); } } long long res = 0; while (true) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { while(!q[i].empty() && (s.find(q[i].top().second) != s.end() || q[i].top().second == -1)) { q[i].pop(); } if (!q[i].empty()) { tmp_mx[i][j] = q[i].top(); q[i].pop(); } else { tmp_mx[i][j] = make_pair(-inf, -1); } } } long long mx11 = 0; if (tmp_mx[1][0].second != tmp_mx[1][1].second && tmp_mx[1][1].second != -1) { mx11 = tmp_mx[1][0].first + tmp_mx[1][1].first; } long long mx02 = 0; int a0 = -1, a2 = -1; if (tmp_mx[0][0].second != -1 && tmp_mx[2][0].second != -1) { if (tmp_mx[0][0].second != tmp_mx[2][0].second) { mx02 = tmp_mx[0][0].first + tmp_mx[2][0].first; a0 = 0; a2 = 0; } if (tmp_mx[0][1].second != -1 && tmp_mx[0][1].first + tmp_mx[2][0].first > mx02) { mx02 = tmp_mx[0][1].first + tmp_mx[2][0].first; a0 = 1; a2 = 0; } if (tmp_mx[2][1].second != -1 && tmp_mx[0][0].first + tmp_mx[2][1].first > mx02) { mx02 = tmp_mx[0][0].first + tmp_mx[2][1].first; a0 = 0; a2 = 1; } } long long mx33 = max(0LL, tmp_mx[3][0].first) + max(0LL, tmp_mx[3][1].first); if (mx02 <= 0 && mx11 <= 0 && mx33 <= 0) { return res; } if (mx02 > mx11 && mx02 > mx33) { res += mx02; s.insert(tmp_mx[0][a0].second); s.insert(tmp_mx[2][a2].second); q[0].push(tmp_mx[0][(a0 + 1) % 2]); q[2].push(tmp_mx[2][(a2 + 1) % 2]); back(1); back(3); } else if (mx11 > mx33) { res += mx11; s.insert(tmp_mx[1][0].second); s.insert(tmp_mx[1][1].second); back(0); back(2); back(3); } else { res += mx33; s.insert(tmp_mx[3][0].second); s.insert(tmp_mx[3][1].second); back(0); back(1); back(2); } } return res; } long long br(int p, int pp, int i, int z) { if (i == v[p].size()) { if (bnum[0] == bnum[2] && (bnum[1] % 2) == 0) { return 0; } return -inf; } long long a = v[p][i].first; long long res = br(p, pp, i + 1, z); if (a != pp && a != z) { long long c = v[p][i].second; bnum[0]++; res = max(res, c + br(p, pp, i + 1, z)); bnum[0]--; for (int j = 1; j < 4; j++) { bnum[j]++; res = max(res, c + mx[a][j] - mx[a][0] + br(p, pp, i + 1, z)); bnum[j]--; } } return res; } void r(int p, int pp) { for (int i = 0; i < v[p].size(); i++) { int a = v[p][i].first; if (a != pp) { r(a, p); mx[p][0] += mx[a][0]; } } long long child_sum = mx[p][0]; int kr = v[p].size() - 1; long long kr5 = (kr > 3 && kr < 9) ? pow(5, kr) : B_MAX; if (kr5 + b_op < B_MAX) { mx[p][0] += max(0LL, br(p, pp, 0, -1)); b_op += kr5; } else { mx[p][0] += max(0LL, h(p, pp, -1)); } for (int i = 0; i < v[p].size(); i++) { int a = v[p][i].first; if (a != pp) { long long c = v[p][i].second; long long bez = 0; if (kr5 + b_op < B_MAX) { bez = br(p, pp, 0, a); b_op += kr5; } else { bez = h(p, pp, a); } for (int j = 1; j < 4; j++) { mx[p][j] = max(mx[p][j], child_sum - mx[a][0] + bez + mx[a][j - 1] + c); } } } } void init(int a) { mx[a][0] = 0; mx[a][1] = -inf; mx[a][2] = -inf; mx[a][3] = -inf; } int main() { ios_base::sync_with_stdio(0); cin >> n; init(0); for(int i = 1; i < n; i++) { init(i); cin >> a >> b >> c; a--; b--; v[a].push_back(make_pair(b, c)); v[b].push_back(make_pair(a, c)); } r(0, -1); cout << mx[0][0] << endl; return 0; } |