#include <cstdio> #include <vector> using namespace std; const int maxN = 2e5 + 10; const long long inf = 1e17; int N; struct E { int a, len; E(int a, int len) : a(a), len(len) {} }; vector<E> nbh[maxN]; long long dp[maxN][4]; long long safeadd(long long a, long long b) { if (a == -inf || b == -inf) return -inf; return a + b; } void join(long long *tmpdp, int v) { long long result[8]; for (int i = 0; i < 8; ++i) result[i] = safeadd(tmpdp[i], dp[v][0]); for (int i = 1; i < 4; ++i) { int _i = 4 - i; for (int mask = 0; mask < 8; ++mask) { if (!(mask & (1 << (i - 1)))) { int newmask = mask | (1 << (i - 1)); result[newmask] = max(result[newmask], safeadd(dp[v][i], tmpdp[mask])); } if (mask & (1 << (_i - 1))) { int newmask = mask ^ (1 << (_i - 1)); result[newmask] = max(result[newmask], safeadd(dp[v][i], tmpdp[mask])); } } } for (int i = 0; i < 8; ++i) tmpdp[i] = result[i]; } void dfs(int v, int p, long long plen) { long long tmpdp[8]; for (int i = 1; i < 8; ++i) tmpdp[i] = -inf; tmpdp[0] = 0; for (E const &e : nbh[v]) if (e.a != p) { dfs(e.a, v, e.len); join(tmpdp, e.a); } dp[v][0] = max(tmpdp[0], safeadd(plen, tmpdp[1 << 2])); dp[v][1] = safeadd(plen, tmpdp[0]); dp[v][2] = safeadd(plen, tmpdp[1 << 0]); dp[v][3] = safeadd(plen, tmpdp[1 << 1]); } int main() { scanf("%d", &N); for (int a, b, x, i = 1; i < N; ++i) { scanf("%d%d%d", &a, &b, &x); nbh[a].emplace_back(b, x); nbh[b].emplace_back(a, x); } dfs(1, -1, -inf); printf("%lld\n", max(0LL, dp[1][0])); 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 | #include <cstdio> #include <vector> using namespace std; const int maxN = 2e5 + 10; const long long inf = 1e17; int N; struct E { int a, len; E(int a, int len) : a(a), len(len) {} }; vector<E> nbh[maxN]; long long dp[maxN][4]; long long safeadd(long long a, long long b) { if (a == -inf || b == -inf) return -inf; return a + b; } void join(long long *tmpdp, int v) { long long result[8]; for (int i = 0; i < 8; ++i) result[i] = safeadd(tmpdp[i], dp[v][0]); for (int i = 1; i < 4; ++i) { int _i = 4 - i; for (int mask = 0; mask < 8; ++mask) { if (!(mask & (1 << (i - 1)))) { int newmask = mask | (1 << (i - 1)); result[newmask] = max(result[newmask], safeadd(dp[v][i], tmpdp[mask])); } if (mask & (1 << (_i - 1))) { int newmask = mask ^ (1 << (_i - 1)); result[newmask] = max(result[newmask], safeadd(dp[v][i], tmpdp[mask])); } } } for (int i = 0; i < 8; ++i) tmpdp[i] = result[i]; } void dfs(int v, int p, long long plen) { long long tmpdp[8]; for (int i = 1; i < 8; ++i) tmpdp[i] = -inf; tmpdp[0] = 0; for (E const &e : nbh[v]) if (e.a != p) { dfs(e.a, v, e.len); join(tmpdp, e.a); } dp[v][0] = max(tmpdp[0], safeadd(plen, tmpdp[1 << 2])); dp[v][1] = safeadd(plen, tmpdp[0]); dp[v][2] = safeadd(plen, tmpdp[1 << 0]); dp[v][3] = safeadd(plen, tmpdp[1 << 1]); } int main() { scanf("%d", &N); for (int a, b, x, i = 1; i < N; ++i) { scanf("%d%d%d", &a, &b, &x); nbh[a].emplace_back(b, x); nbh[b].emplace_back(a, x); } dfs(1, -1, -inf); printf("%lld\n", max(0LL, dp[1][0])); return 0; } |