Submission #197725

#TimeUsernameProblemLanguageResultExecution timeMemory
197725moonrabbit2Beads and wires (APIO14_beads)C++17
13 / 100
11 ms5240 KiB
#include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; typedef double db; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<db,db> pdb; typedef tuple<int,int,int> tii; typedef vector<vector<ll>> mat; const ll mod=1e9+7; const int N=2e5+5; int n,ans,dp1[N],dp2[N],dp3[N],dp4[N]; vector<pii> g[N]; void dfs(int x,int p){ vector<int> v1,v2; dp2[x]=dp3[x]=dp4[x]=-1e9; int s=0; for(auto &it : g[x]) if(it.fi!=p){ int u=it.fi,c=it.se; dfs(u,x); v1.emplace_back(dp1[u]+c-max({dp1[u],dp2[u],dp3[u]+c})); v2.emplace_back(dp2[u]+c-max({dp1[u],dp2[u],dp3[u]+c})); dp1[x]+=max({dp1[u],dp2[u],dp3[u]+c,dp4[u]+c}); s+=max({dp1[u],dp2[u],dp3[u]+c}); } if(p){ for(auto &it : g[x]) if(it.fi!=p){ int u=it.fi,c=it.se; dp3[x]=max(dp3[x],s-max({dp1[u],dp2[u],dp3[u]+c})+dp1[u]+c); dp4[x]=max(dp4[x],s-max({dp1[u],dp2[u],dp3[u]+c})+dp2[u]+c); } } sort(v1.begin(),v1.end()); reverse(v1.begin(),v1.end()); sort(v2.begin(),v2.end()); reverse(v2.begin(),v2.end()); if(v1.size()>=2){ dp2[x]=max(dp2[x],s+v1[0]+v1[1]); for(auto &it : g[x]) if(it.fi!=p){ int u=it.fi,c=it.se; int c1=dp1[u]+c-max({dp1[u],dp2[u],dp3[u]+c}),c2=dp2[u]+c-max({dp1[u],dp2[u],dp3[u]+c}); if(c1==v1[0]) dp2[x]=max(dp2[x],s+v1[1]+c2); else dp2[x]=max(dp2[x],s+v1[0]+c2); if(c2==v2[0]) dp2[x]=max(dp2[x],s+c1+v2[1]); else dp2[x]=max(dp2[x],s+c1+v2[0]); } } //cout<<x<<" : "<<dp1[x]<<" "<<dp2[x]<<" "<<dp3[x]<<" "<<dp4[x]<<"\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n; for(int u,v,c,i=1;i<n;i++){ cin>>u>>v>>c; g[u].emplace_back(v,c); g[v].emplace_back(u,c); } dfs(1,0); cout<<max(dp1[1],dp2[1]); 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...