Submission #957366

#TimeUsernameProblemLanguageResultExecution timeMemory
957366vjudge1Beads and wires (APIO14_beads)C++14
0 / 100
2 ms4956 KiB
#include<bits/stdc++.h> using namespace std; const int N=2e5+10; int f[N][2]; vector<pair<int, int>> g[N]; void dfs(int u, int p){ for (auto &e:g[u]) if (e.first!=p) dfs(e.first, u); vector<int> vv; int sum=0; for (auto &e:g[u]){ int v=e.first, w=e.second; if (v==p) continue; sum+=max(f[v][0], f[v][1]), vv.push_back(f[v][0]+w-max(f[v][0], f[v][1])); } sort(vv.rbegin(), vv.rend()); vector<int> pf=vv; partial_sum(vv.begin(), vv.end(), pf.begin()); f[u][0]=sum; for (int j=1; j<(int)pf.size(); j+=2) f[u][0]=max(f[u][0], sum+pf[j]); if (p && (int)g[u].size()>1){ int wpar=0; for (auto &e:g[p]) if (e.first==u) wpar=e.second; for (int j=0; j<(int)pf.size(); j+=2) f[u][1]=max(f[u][1], sum+pf[j]+wpar); } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i=1; i<n; ++i){ int u, v, w; cin >> u >> v >> w; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } dfs(1, 0); cout << f[1][0]; 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...