#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const long long inf = 1e18 + 1; int n; vector<pair<int, int> > kraw[N]; long long dp[4][N]; bitset<N> odw; long long tmp[2 * N][2][2][4]; void clear(int t) { for (int i = -t; i <= t; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { for (int l = 0; l < 4; l++) { tmp[N + i][j][k][l] = -inf; } } } } } void dfs(int x) { odw[x] = true; for (auto v : kraw[x]) { if (!odw[v.first]) { dfs(v.first); } } int ind = 0; clear(kraw[x].size() + 1); tmp[N][0][0][0] = 0; int licz = 0; for (auto v : kraw[x]) { if (!odw[v.first]) { ind ^= 1; licz++; int d = min(licz, (int)kraw[x].size() - licz + 5); for (int i = N - d; i <= N + d; i++) { long long ile = tmp[i][ind ^ 1][0][0] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][0][0] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][0][0] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0 ^ 1][0] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][0][0] + dp[2][v.first] + v.second); tmp[i][ind][0][0] = ile; ile = tmp[i][ind ^ 1][0][1] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][0][1] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][0][1] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0 ^ 1][1] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][0][1] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0][0] + v.second + dp[1 - 1][v.first]); tmp[i][ind][0][1] = ile; ile = tmp[i][ind ^ 1][0][2] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][0][2] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][0][2] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0 ^ 1][2] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][0][2] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0][0] + v.second + dp[2 - 1][v.first]); tmp[i][ind][0][2] = ile; ile = tmp[i][ind ^ 1][0][3] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][0][3] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][0][3] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0 ^ 1][3] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][0][3] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0][0] + v.second + dp[3 - 1][v.first]); tmp[i][ind][0][3] = ile; ile = tmp[i][ind ^ 1][1][0] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][1][0] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][1][0] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1 ^ 1][0] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][1][0] + dp[2][v.first] + v.second); tmp[i][ind][1][0] = ile; ile = tmp[i][ind ^ 1][1][1] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][1][1] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][1][1] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1 ^ 1][1] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][1][1] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1][0] + v.second + dp[1 - 1][v.first]); tmp[i][ind][1][1] = ile; ile = tmp[i][ind ^ 1][1][2] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][1][2] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][1][2] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1 ^ 1][2] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][1][2] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1][0] + v.second + dp[2 - 1][v.first]); tmp[i][ind][1][2] = ile; ile = tmp[i][ind ^ 1][1][3] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][1][3] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][1][3] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1 ^ 1][3] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][1][3] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1][0] + v.second + dp[3 - 1][v.first]); tmp[i][ind][1][3] = ile; } } } for (int i = 0; i < 4; i++) { dp[i][x] = tmp[N][ind][0][i]; } odw[x] = false; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b, w; scanf("%d%d%d", &a, &b, &w); kraw[a].push_back(make_pair(b, w)); kraw[b].push_back(make_pair(a, w)); } // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= 4; j++) { // dp[j][i] = -inf; // } // } int lisc = 0; for (int i = 1; i <= n; i++) { if ((int)kraw[i].size() > 1) { lisc++; // cout << "witam " << i << endl; } } // cout << "siema " << lisc << endl; if (lisc == 1) { printf("0\n"); return 0; } dfs(1); printf("%lld\n", dp[0][1]); // for (int i = 1; i <= n; i++) { // printf("dp[%d] = (%lld, %lld, %lld, %lld)\n", i, dp[0][i], dp[1][i], dp[2][i], dp[3][i]); // } }
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 | #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const long long inf = 1e18 + 1; int n; vector<pair<int, int> > kraw[N]; long long dp[4][N]; bitset<N> odw; long long tmp[2 * N][2][2][4]; void clear(int t) { for (int i = -t; i <= t; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { for (int l = 0; l < 4; l++) { tmp[N + i][j][k][l] = -inf; } } } } } void dfs(int x) { odw[x] = true; for (auto v : kraw[x]) { if (!odw[v.first]) { dfs(v.first); } } int ind = 0; clear(kraw[x].size() + 1); tmp[N][0][0][0] = 0; int licz = 0; for (auto v : kraw[x]) { if (!odw[v.first]) { ind ^= 1; licz++; int d = min(licz, (int)kraw[x].size() - licz + 5); for (int i = N - d; i <= N + d; i++) { long long ile = tmp[i][ind ^ 1][0][0] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][0][0] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][0][0] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0 ^ 1][0] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][0][0] + dp[2][v.first] + v.second); tmp[i][ind][0][0] = ile; ile = tmp[i][ind ^ 1][0][1] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][0][1] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][0][1] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0 ^ 1][1] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][0][1] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0][0] + v.second + dp[1 - 1][v.first]); tmp[i][ind][0][1] = ile; ile = tmp[i][ind ^ 1][0][2] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][0][2] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][0][2] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0 ^ 1][2] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][0][2] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0][0] + v.second + dp[2 - 1][v.first]); tmp[i][ind][0][2] = ile; ile = tmp[i][ind ^ 1][0][3] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][0][3] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][0][3] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0 ^ 1][3] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][0][3] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][0][0] + v.second + dp[3 - 1][v.first]); tmp[i][ind][0][3] = ile; ile = tmp[i][ind ^ 1][1][0] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][1][0] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][1][0] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1 ^ 1][0] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][1][0] + dp[2][v.first] + v.second); tmp[i][ind][1][0] = ile; ile = tmp[i][ind ^ 1][1][1] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][1][1] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][1][1] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1 ^ 1][1] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][1][1] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1][0] + v.second + dp[1 - 1][v.first]); tmp[i][ind][1][1] = ile; ile = tmp[i][ind ^ 1][1][2] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][1][2] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][1][2] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1 ^ 1][2] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][1][2] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1][0] + v.second + dp[2 - 1][v.first]); tmp[i][ind][1][2] = ile; ile = tmp[i][ind ^ 1][1][3] + dp[0][v.first]; ile = max(ile, tmp[i][ind ^ 1][1][3] + dp[3][v.first] + v.second); ile = max(ile, tmp[i - 1][ind ^ 1][1][3] + dp[0][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1 ^ 1][3] + dp[1][v.first] + v.second); ile = max(ile, tmp[i + 1][ind ^ 1][1][3] + dp[2][v.first] + v.second); ile = max(ile, tmp[i][ind ^ 1][1][0] + v.second + dp[3 - 1][v.first]); tmp[i][ind][1][3] = ile; } } } for (int i = 0; i < 4; i++) { dp[i][x] = tmp[N][ind][0][i]; } odw[x] = false; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b, w; scanf("%d%d%d", &a, &b, &w); kraw[a].push_back(make_pair(b, w)); kraw[b].push_back(make_pair(a, w)); } // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= 4; j++) { // dp[j][i] = -inf; // } // } int lisc = 0; for (int i = 1; i <= n; i++) { if ((int)kraw[i].size() > 1) { lisc++; // cout << "witam " << i << endl; } } // cout << "siema " << lisc << endl; if (lisc == 1) { printf("0\n"); return 0; } dfs(1); printf("%lld\n", dp[0][1]); // for (int i = 1; i <= n; i++) { // printf("dp[%d] = (%lld, %lld, %lld, %lld)\n", i, dp[0][i], dp[1][i], dp[2][i], dp[3][i]); // } } |