Submission #434860

#TimeUsernameProblemLanguageResultExecution timeMemory
434860cpp219Beads and wires (APIO14_beads)C++14
0 / 100
3 ms4940 KiB
#pragma GCC optimization O2 #pragma GCC optimization "unroll-loop" #pragma target ("avx2") #include <bits/stdc++.h> #define ll int #define ld long double #define fs first #define sc second using namespace std; typedef pair<ll,ll> LL; const ll N = 2e5 + 9; const ll inf = 1e9 + 7; vector<LL> g[N]; ll n,x,y,w,dp[N][2],ans,val[N]; void DFS(ll u,ll p){ ll child = 0; for (auto i : g[u]) if (i.fs != p) DFS(i.fs,u),child++; dp[u][0] = dp[u][1] = 0; if (!child) dp[u][1] = -inf; for (auto i : g[u]){ ll v = i.fs,cost = i.sc; if (v == p) continue; dp[u][0] += max(dp[v][1] + cost,dp[v][0]); val[v] = dp[v][0] + cost - max(dp[v][1] + cost,dp[v][0]); } for (auto i : g[u]){ ll v = i.fs,cost = i.sc; if (v == p) continue; ll now = dp[u][0] - max(dp[v][1] + cost,dp[v][0]) + dp[v][0] + cost; dp[u][1] = max(dp[u][1],now); } if (child < 2) return; //cout<<val[5]; exit(0); multiset<ll> s; for (auto i : g[u]){ ll v = i.fs,cost = i.sc; if (v == p) continue; s.insert(val[v]); } while(s.size() > 2) s.erase(s.begin()); dp[u][0] += max(0,*s.begin() + *s.rbegin()); } int main(){ ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); #define task "tst" if (fopen(task".INP","r")){ freopen(task".INP","r",stdin); //freopen(task".OUT","w",stdout); } cin>>n; for (ll i = 1;i < n;i++){ cin>>x>>y>>w; g[x].push_back({y,w}); g[y].push_back({x,w}); } DFS(1,0); cout<<dp[1][0]; return 0; for (ll i = 1;i <= min(n,2000);i++){ DFS(i,0); ans = max(ans,dp[i][0]); //cout<<dp[i][0]; return 0; } cout<<ans; }

Compilation message (stderr)

beads.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization O2
      | 
beads.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
beads.cpp:3: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
    3 | #pragma target ("avx2")
      | 
beads.cpp: In function 'void DFS(int, int)':
beads.cpp:38:21: warning: unused variable 'cost' [-Wunused-variable]
   38 |         ll v = i.fs,cost = i.sc;
      |                     ^~~~
beads.cpp: In function 'int main()':
beads.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...