#include "bits/stdc++.h" // Tomasz Nowak
using namespace std; // University of Warsaw
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) {
return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) {}
#endif
mt19937 rng(43121234);
vector<vector<pair<int, int>>> graph;
constexpr LL inf = LL(1e18);
array<LL, 4> dfs(int v, int p) {
vector<array<LL, 4>> sons_values;
for(auto [u, w] : graph[v])
if(u != p) {
auto got = dfs(u, v);
got = {
max(got[0], got[3] + w),
got[0] + w,
got[1] + w,
got[2] + w
};
sons_values.emplace_back(got);
}
const int max_diff = min(ssize(sons_values) + 1, 1000);
vector<array<LL, 2>> dp(2 * max_diff + 1, array<LL, 2>{{-inf, -inf}});
auto new_dp = dp;
dp[max_diff][0] = 0;
for(const auto &got : sons_values) {
FOR(d, 0, 2 * max_diff)
for(int parity2 : {0, 1})
new_dp[d][parity2] = dp[d][parity2] + got[0];
FOR(d, 0, 2 * max_diff)
for(int parity2 : {0, 1}) {
if(d != 0)
new_dp[d][parity2] = max(new_dp[d][parity2], dp[d - 1][parity2] + got[1]);
if(d != 2 * max_diff)
new_dp[d][parity2] = max(new_dp[d][parity2], dp[d + 1][parity2] + got[3]);
new_dp[d][parity2] = max(new_dp[d][parity2], dp[d][parity2 ^ 1] + got[2]);
}
swap(dp, new_dp);
}
return {
dp[max_diff][0],
dp[max_diff + 1][0],
dp[max_diff][1],
dp[max_diff - 1][0]
};
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
graph.resize(n);
REP(edge, n - 1) {
int v, u, w;
cin >> v >> u >> w;
--v, --u;
graph[v].emplace_back(u, w);
graph[u].emplace_back(v, w);
}
for(auto &v : graph)
shuffle(v.begin(), v.end(), rng);
cout << dfs(0, -1)[0] << '\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 | #include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif mt19937 rng(43121234); vector<vector<pair<int, int>>> graph; constexpr LL inf = LL(1e18); array<LL, 4> dfs(int v, int p) { vector<array<LL, 4>> sons_values; for(auto [u, w] : graph[v]) if(u != p) { auto got = dfs(u, v); got = { max(got[0], got[3] + w), got[0] + w, got[1] + w, got[2] + w }; sons_values.emplace_back(got); } const int max_diff = min(ssize(sons_values) + 1, 1000); vector<array<LL, 2>> dp(2 * max_diff + 1, array<LL, 2>{{-inf, -inf}}); auto new_dp = dp; dp[max_diff][0] = 0; for(const auto &got : sons_values) { FOR(d, 0, 2 * max_diff) for(int parity2 : {0, 1}) new_dp[d][parity2] = dp[d][parity2] + got[0]; FOR(d, 0, 2 * max_diff) for(int parity2 : {0, 1}) { if(d != 0) new_dp[d][parity2] = max(new_dp[d][parity2], dp[d - 1][parity2] + got[1]); if(d != 2 * max_diff) new_dp[d][parity2] = max(new_dp[d][parity2], dp[d + 1][parity2] + got[3]); new_dp[d][parity2] = max(new_dp[d][parity2], dp[d][parity2 ^ 1] + got[2]); } swap(dp, new_dp); } return { dp[max_diff][0], dp[max_diff + 1][0], dp[max_diff][1], dp[max_diff - 1][0] }; } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; graph.resize(n); REP(edge, n - 1) { int v, u, w; cin >> v >> u >> w; --v, --u; graph[v].emplace_back(u, w); graph[u].emplace_back(v, w); } for(auto &v : graph) shuffle(v.begin(), v.end(), rng); cout << dfs(0, -1)[0] << '\n'; } |
English