Submission #734670

#TimeUsernameProblemLanguageResultExecution timeMemory
734670Abrar_Al_SamitBeads and wires (APIO14_beads)C++17
0 / 100
5 ms6564 KiB
#include<bits/stdc++.h> using namespace std; const int nax = 200005; vector<pair<int,int>>g[nax]; int dp[nax][2], n; int solve(int v, int topar, int p = -1) { int &ret = dp[v][(topar>0)]; if(ret!=-1) return ret; ret = 0; int base = 0; for(auto [u, w] : g[v]) if(u!=p) { base += solve(u, w, v); } ret = base; if(topar) { for(auto [u, w] : g[v]) if(u!=p) { ret = max(ret, base - solve(u, w, v) + solve(u, 0, v) + topar + w); } } base = 0; int mx1(-1), mx2(-1), mx3(-1); int t1, t2, t3; for(auto [u, w] : g[v]) if(u!=p) { if(w > mx1) { mx3 = mx2, mx2 = mx1; t3 = t2, t2 = t1; mx1 = w, t1 = u; } else if(w > mx2) { mx3 = mx2, t3 = t2; mx2 = w, t2 = u; } else if(w > mx3) { mx3 = w, t3 = u; } base += solve(u, 0, v); } if(mx2>-1) ret = max(ret, base + mx1 + mx2); if(g[v].size() > 3 - (v==1)) { for(auto [u, w] : g[v]) if(u!=p) { if(u!=t1 && u!=t2) { int cur = base - solve(u, 0, v) + solve(u, w, v) + mx1 + mx2; ret = max(ret, cur); } else if(u==t1) { int cur = base - solve(u, 0, v) + solve(u, w, v) + mx2 + mx3; ret = max(ret, cur); } else { int cur = base - solve(u, 0, v) + solve(u, w, v) + mx1 + mx3; ret = max(ret, cur); } } } return ret; } void PlayGround() { cin>>n; for(int i=1; i<n; ++i) { int u, v, w; cin>>u>>v>>w; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } memset(dp, -1, sizeof dp); cout<<solve(1, 0)<<'\n'; // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); PlayGround(); return 0; }

Compilation message (stderr)

beads.cpp: In function 'int solve(int, int, int)':
beads.cpp:31:14: warning: variable 't3' set but not used [-Wunused-but-set-variable]
   31 |  int t1, t2, t3;
      |              ^~
beads.cpp:52:13: warning: 't2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   52 |    if(u!=t1 && u!=t2) {
      |       ~~~~~~^~~~~~~~
beads.cpp:52:4: warning: 't1' may be used uninitialized in this function [-Wmaybe-uninitialized]
   52 |    if(u!=t1 && u!=t2) {
      |    ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...