#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A,typename B> ostream& operator<<(ostream& out,const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out,const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } const int N = 200001; vector<vector<pair<int,int>>> g; vector<vector<vector<long long>>> dp[N]; vector<int> u; void dfs(int v) { u[v] = 1; vector<pair<int,int>> ch; for(auto [to,c] : g[v]) { if(!u[to]) { dfs(to); ch.push_back({to,c}); } } int m = ch.size(); vector<vector<long long>> ndp[m + 1]; for(int i = 0; i <= m; i++) { ndp[i].resize(m - i + 1); for(int j = 0; i + j <= m; j++) { ndp[i][j].resize(m - i - j + 1); } } for(int r = 0; r < ch.size(); r++) { int to = ch[r].first; int c = ch[r].second; for(int i = 0; i <= r + 1; i++) { for(int j = 0; i + j <= r + 1; j++) { for(int k = 0; i + j + k <= r + 1; k++) { ndp[i][j][k] = dp[v][i][j][k] + dp[to][0][0][0]; if(i + j + k + 1 <= r) { ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][1][0] + dp[v][i + 1][j][k]); ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][1][0][0] + dp[v][i][j + 1][k]); ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][0][0] + dp[v][i][j][k + 1]); } if(i) ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][0][0] + dp[v][i - 1][j][k]); if(j) ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][1][0][0] + dp[v][i][j - 1][k]); if(k) ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][1][0] + dp[v][i][j][k - 1]); ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][0][1] + dp[v][i][j][k]); } } } for(int i = 0; i <= r + 1; i++) { for(int j = 0; i + j <= r + 1; j++) { for(int k = 0; i + j + k <= r + 1; k++) { dp[v][i][j][k] = ndp[i][j][k]; } } } } } void solve() { int n; cin >> n; g.resize(n); u.resize(n); for(int i = 0; i < n - 1; i++) { int a,b,c; cin >> a >> b >> c; a--; b--; g[a].push_back({b,c}); g[b].push_back({a,c}); } for(int v = 0; v < n; v++) { int x = g[v].size(); dp[v].resize(x + 1); for(int i = 0; i <= x; i++) { dp[v][i].resize(x - i + 1); for(int j = 0; i + j <= x; j++) { dp[v][i][j].resize(x - i - j + 1); for(int k = 0; i + j + k <= x; k++) { if(i || j || k) { dp[v][i][j][k] = -1e18; } } } } } dfs(0); cout << dp[0][0][0][0] << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testNum = 1; //cin >> testNum; for(int i = 1; i <= testNum; i++) { solve(); } 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 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A,typename B> ostream& operator<<(ostream& out,const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out,const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } const int N = 200001; vector<vector<pair<int,int>>> g; vector<vector<vector<long long>>> dp[N]; vector<int> u; void dfs(int v) { u[v] = 1; vector<pair<int,int>> ch; for(auto [to,c] : g[v]) { if(!u[to]) { dfs(to); ch.push_back({to,c}); } } int m = ch.size(); vector<vector<long long>> ndp[m + 1]; for(int i = 0; i <= m; i++) { ndp[i].resize(m - i + 1); for(int j = 0; i + j <= m; j++) { ndp[i][j].resize(m - i - j + 1); } } for(int r = 0; r < ch.size(); r++) { int to = ch[r].first; int c = ch[r].second; for(int i = 0; i <= r + 1; i++) { for(int j = 0; i + j <= r + 1; j++) { for(int k = 0; i + j + k <= r + 1; k++) { ndp[i][j][k] = dp[v][i][j][k] + dp[to][0][0][0]; if(i + j + k + 1 <= r) { ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][1][0] + dp[v][i + 1][j][k]); ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][1][0][0] + dp[v][i][j + 1][k]); ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][0][0] + dp[v][i][j][k + 1]); } if(i) ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][0][0] + dp[v][i - 1][j][k]); if(j) ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][1][0][0] + dp[v][i][j - 1][k]); if(k) ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][1][0] + dp[v][i][j][k - 1]); ndp[i][j][k] = max(ndp[i][j][k],c + dp[to][0][0][1] + dp[v][i][j][k]); } } } for(int i = 0; i <= r + 1; i++) { for(int j = 0; i + j <= r + 1; j++) { for(int k = 0; i + j + k <= r + 1; k++) { dp[v][i][j][k] = ndp[i][j][k]; } } } } } void solve() { int n; cin >> n; g.resize(n); u.resize(n); for(int i = 0; i < n - 1; i++) { int a,b,c; cin >> a >> b >> c; a--; b--; g[a].push_back({b,c}); g[b].push_back({a,c}); } for(int v = 0; v < n; v++) { int x = g[v].size(); dp[v].resize(x + 1); for(int i = 0; i <= x; i++) { dp[v][i].resize(x - i + 1); for(int j = 0; i + j <= x; j++) { dp[v][i][j].resize(x - i - j + 1); for(int k = 0; i + j + k <= x; k++) { if(i || j || k) { dp[v][i][j][k] = -1e18; } } } } } dfs(0); cout << dp[0][0][0][0] << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testNum = 1; //cin >> testNum; for(int i = 1; i <= testNum; i++) { solve(); } return 0; } |