제출 #967401

#제출 시각아이디문제언어결과실행 시간메모리
967401steveonalex구슬과 끈 (APIO14_beads)C++17
28 / 100
1046 ms6488 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define ALL(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng(1); ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;} ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return mask & (-mask);} ll pop_cnt(ll mask){return __builtin_popcountll(mask);} ll ctz(ll mask){return __builtin_ctzll(mask);} ll clz(ll mask){return __builtin_clzll(mask);} ll logOf(ll mask){return 63 - clz(mask);} template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b){a = b; return true;} return false; } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b){a = b; return true;} return false; } template <class T> void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){ for(auto i: a) out << i << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const int N = 2e5 + 69; const ll INF = 1e18 + 69; int n; vector<pair<int, int>> graph[N]; ll dp[N][2]; void dfs(int u, int p){ for(pair<int, int> v: graph[u]) if (v.first != p){ dfs(v.first, u); } ll cu = 0; vector<ll> reduc; ll ma = -INF; for(pair<int, int> v: graph[u]) if (v.first != p){ dp[u][0] += max(dp[v.first][1] + v.second, dp[v.first][0]); cu += max(dp[v.first][1] + v.second, dp[v.first][0]); maximize(ma, (v.second + dp[v.first][0]) - max(dp[v.first][1] + v.second, dp[v.first][0])); } if (ma > -INF){ cu += ma; maximize(dp[u][1], cu); } if (u != p) return; cu = 0; reduc.clear(); for(pair<int, int> v: graph[u]) if (v.first != p){ cu += max(dp[v.first][1] + v.second, dp[v.first][0]); reduc.push_back((v.second + dp[v.first][0]) - max(dp[v.first][1] + v.second, dp[v.first][0])); } if (reduc.size() < 2) return; nth_element(reduc.begin(), reduc.begin() + 1, reduc.end(), greater<ll>()); for(int i = 0; i<=1; ++i) cu += reduc[i]; maximize(dp[u][0], cu); } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i<n; ++i){ int u, v, w; cin >> u >> v >> w; graph[u].push_back({v, w}); graph[v].push_back({u, w}); } for(int i = 1; i<=n; ++i) sort(ALL(graph[i])); ll ans = 0; for(int i = 1; i<=n; ++i){ for(int i = 1; i<=n; ++i) { dp[i][0] = 0; dp[i][1] = -INF; } dfs(i, i); maximize(ans, dp[i][0]); } cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...