Submission #198236

#TimeUsernameProblemLanguageResultExecution timeMemory
198236mehrdad_sohrabiBeads and wires (APIO14_beads)C++14
57 / 100
204 ms1656 KiB
#include <bits/stdc++.h> typedef int ll; typedef long double ld; #define pb push_back #define pii pair < int, int > #define F first #define S second //#define int long long int #define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define endl '\n' using namespace std; const int N=10002; vector <pii> g[N]; ll vis[N]; ll ans=0; ll dp[N][2]; void dfs(ll v,ll p,ll w){ ll ma=-1e9; ll d=0; for (auto u : g[v]){ if (u.F==p) continue; dfs(u.F,v,u.S); dp[v][1]+=dp[u.F][0]; d+=dp[u.F][0]; ma=max(ma,u.S+dp[u.F][1]-dp[u.F][0]); } if (p!=-1 && ma>-1e9){ ll cnt=d+ma+w; dp[v][0]=max(dp[v][1],cnt); } } ll up[N][2]; ll dfs2(ll v,ll p){ ll cnt=0,wp=0; for (auto u : g[v]){ if (u.F==p){ wp=u.S; continue; } cnt+=dp[u.F][0]; } cnt+=up[v][0]; for (auto u : g[v]){ if (u.F==p) continue; up[u.F][1]=cnt-dp[u.F][0]; //up[u.F][0]=max(up[u.F][0],u.S+wp+cnt-dp[u.F][0]); } for (auto u : g[v]){ if (u.F==p || v==1) continue; up[u.F][0]=max(up[u.F][0],u.S+wp+cnt-dp[u.F][0]-up[v][0]+up[v][1]); } // if (v==2) cout << up[3][0] << endl; pii mx; for (auto u : g[v]){ ll z=cnt; if (u.F==p) continue; for (auto q : g[v]){ if (u.F==q.F || q.F==p) continue; ll t=z-dp[u.F][0]-dp[q.F][0]+dp[q.F][1]+u.S+q.S; // if (v==2) cout << t << endl; up[u.F][0]=max(up[u.F][0],t); } // if (v==1) cout << u.F << " " << up[u.F][0] << endl; up[u.F][0]=max(up[u.F][0],up[u.F][1]); } for (auto u : g[v]){ if (u.F!=p) dfs2(u.F,v); } } int32_t main(){ sync; ll n; cin >> n; for (int i=0;i<n-1;i++){ ll u,v,w; cin >> u >> v >> w; g[u].pb({v,w}); g[v].pb({u,w}); } for (int i=1;i<=n;i++){ vector <pii> a; for (auto q : g[i]){ ll u=q.F; if (g[u].size()==1) a.pb({q.S,u}); } sort(a.begin(),a.end()); for (int j=1;j<a.size();j++){ vis[a[j].S]=1; } } ll ma=0; ll cnt=0; dfs(1,-1,0); dfs2(1,1); for (int i=1;i<=n;i++){ ma=max(ma,dp[i][1]+up[i][0]); //cout << i << " " << dp[i][1] << " " << up[i][0] << " " << ma << endl; } cout << ma << endl; } /* 10 4 10 2 1 2 21 1 3 13 6 7 1 7 9 5 2 4 3 2 5 8 1 6 55 6 8 34 */ /* 6 1 2 20 2 3 20 3 4 10 3 5 10 3 6 10 */

Compilation message (stderr)

beads.cpp: In function 'll dfs2(ll, ll)':
beads.cpp:71:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
beads.cpp: In function 'int32_t main()':
beads.cpp:89:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=1;j<a.size();j++){
                      ~^~~~~~~~~
beads.cpp:94:8: warning: unused variable 'cnt' [-Wunused-variable]
     ll cnt=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...