Submission #261381

#TimeUsernameProblemLanguageResultExecution timeMemory
261381MercenaryBeads and wires (APIO14_beads)C++14
100 / 100
371 ms30724 KiB
#include<bits/stdc++.h> #define taskname "A" #define pb push_back #define mp make_pair #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int,int> ii; const int maxn = 2e5 + 5; int n; ll dp[maxn][2][2]; vector<ii> adj[maxn]; void dfs(int u , int par , int parcost){ ll sum = 0; for(auto & c : adj[u]){ if(c.first == par)continue; dfs(c.first , u , c.second); sum += dp[c.first][0][0]; } dp[u][0][0] = dp[u][0][1] = dp[u][1][0] = dp[u][1][1] = sum; { vector<ll> val; ll tmp1 = 0; for(auto & c : adj[u]){ if(c.first == par)continue; tmp1 = max(tmp1 , dp[c.first][0][1] - dp[c.first][0][0]); val.pb(dp[c.first][1][0] + c.second - dp[c.first][0][0]); } if(val.size() >= 2){ sort(val.begin(),val.end(),greater<ll>()); for(auto & c : adj[u]){ if(c.first == par)continue; if(val[0] == dp[c.first][1][0] + c.second - dp[c.first][0][0]){ dp[u][1][1] = max(dp[u][1][1] , sum + val[1] + max(dp[c.first][1][0],dp[c.first][1][1]) + c.second - dp[c.first][0][0]); }else{ dp[u][1][1] = max(dp[u][1][1] , sum + val[0] + max(dp[c.first][1][0],dp[c.first][1][1]) + c.second - dp[c.first][0][0]); } } } dp[u][1][1] = max(dp[u][1][1] , sum + tmp1); } { vector<ll> val; for(auto & c : adj[u]){ if(c.first == par)continue; val.pb(dp[c.first][1][0] + c.second - dp[c.first][0][0]); } sort(val.begin(),val.end(),greater<ll>()); if(val.size() >= 1)dp[u][0][0] = max(dp[u][0][0] , dp[u][0][0] + parcost + val[0]); else dp[u][0][0] = -1e9; dp[u][0][0] = max(dp[u][0][0] , dp[u][1][0]); } { vector<ll> val; for(auto & c : adj[u]){ if(c.first == par)continue; val.pb(max(dp[c.first][1][1],dp[c.first][1][0]) + c.second - dp[c.first][0][0]); } sort(val.begin(),val.end(),greater<ll>()); if(val.size() >= 1)dp[u][0][1] = max(dp[u][0][1] , dp[u][0][1] + parcost + val[0]); else dp[u][0][1] = -1e9; dp[u][0][1] = max(dp[u][0][1] , dp[u][1][1]); dp[u][0][1] = max(dp[u][0][1] , dp[u][0][0]); } // cout << u << " " << dp[u][0][0] << " " << dp[u][0][1] << " " << dp[u][1][0] << " " << dp[u][1][1] << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); if(fopen(taskname".inp", "r")) { freopen(taskname".inp", "r", stdin); freopen(taskname".out", "w", stdout); } cin >> n; for(int i = 1 ; i < n ; ++i){ int u , v , c;cin >> u >> v >> c; adj[u].pb(mp(v,c));adj[v].pb(mp(u , c)); } dfs(1 , 0 , 0); cout << max(dp[1][1][0] , dp[1][1][1]); }

Compilation message (stderr)

beads.cpp: In function 'int main()':
beads.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(taskname".inp", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(taskname".out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...