Submission #197722

#TimeUsernameProblemLanguageResultExecution timeMemory
197722moonrabbit2Beads and wires (APIO14_beads)C++17
0 / 100
7 ms5112 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]; vector<pii> g[N]; void dfs(int x,int p){ vector<int> v; for(auto &it : g[x]) if(it.fi!=p){ int u=it.fi,c=it.se; dfs(u,x); v.emplace_back(max(dp1[u],dp2[u])+c-max({dp1[u],dp2[u],dp3[u]+c})); dp1[x]+=max({dp1[u],dp2[u],dp3[u]+c}); } sort(v.begin(),v.end()); reverse(v.begin(),v.end()); if(v.size()>=2){ dp2[x]=dp1[x]+v[0]+v[1]; } else dp2[x]=-1e9; if(v.size()>=1&&p){ dp3[x]=dp1[x]+v[0]; } else dp3[x]=-1e9; } 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],dp3[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...