Submission #972392

#TimeUsernameProblemLanguageResultExecution timeMemory
972392pccBeads and wires (APIO14_beads)C++17
100 / 100
217 ms34752 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll,ll> #define pii pair<int,int> #define fs first #define sc second #define tlll tuple<ll,ll,ll> const int mxn = 2e5+10; vector<pii> tree[mxn]; int dp[mxn][2]; int N; int ans = 0; vector<pii> v[mxn]; void dfs(int now,int par){ dp[now][0] = 0; dp[now][1] = -1e9; for(auto [nxt,w]:tree[now]){ if(nxt == par)continue; dfs(nxt,now); dp[now][0] += max(dp[nxt][0],dp[nxt][1]+w); v[now].push_back(pii(dp[nxt][0]+w-max(dp[nxt][0],dp[nxt][1]+w),nxt)); } sort(v[now].rbegin(),v[now].rend()); if(!v[now].empty())dp[now][1] = dp[now][0]+v[now][0].fs; return; } void dfs2(int now,int par){ ans = max(ans,dp[now][0]); sort(v[now].rbegin(),v[now].rend()); for(auto [nxt,w]:tree[now]){ if(nxt == par)continue; int dp0 = dp[now][0]-max(dp[nxt][0],dp[nxt][1]+w); int dp1 = -1e9; if(v[now][0].sc != nxt)dp1 = dp0+v[now][0].fs; else if(v[now].size()>1)dp1 = dp0+v[now][1].fs; dp[nxt][0] += max(dp0,dp1+w); v[nxt].push_back(pii(dp0+w-max(dp0,dp1+w),now)); dfs2(nxt,now); } return; } int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>N; for(int i = 1;i<N;i++){ int a,b,c; cin>>a>>b>>c; tree[a].push_back(pii(b,c)); tree[b].push_back(pii(a,c)); } dfs(1,1); dfs2(1,1); 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...