Submission #89905

#TimeUsernameProblemLanguageResultExecution timeMemory
89905psmaoIslands (IOI08_islands)C++14
100 / 100
587 ms90168 KiB
#include <bits/stdc++.h> using namespace std; #define fo(i,s,t) for(int i = s; i <= t; ++ i) #define fd(i,s,t) for(int i = s; i >= t; -- i) #define bf(i,s) for(int i = head[s]; i; i = e[i].next) #define mp make_pair #define fi first #define se second #define pii pair<int,int> #define pb push_back #define VI vector<int> #define sf scanf #define pf printf #define fp freopen #define SZ(x) ((int)(x).size()) #ifdef MPS #define D(x...) printf(x) #else #define D(x...) #endif typedef long long ll; typedef double db; typedef unsigned long long ull; const int inf = 1<<30; const ll INF = 1ll<<60; const db Inf = 1e20; const db eps = 1e-9; void gmax(int &a,int b){a = (a > b ? a : b);} void gmin(int &a,int b){a = (a < b ? a : b);} const int maxn = 1000001; int n, deg[maxn]; ll Ans, dm[maxn]; pii adj[maxn]; pair<ll,ll> dp[maxn]; ll solve(int x) { deque<pair<ll,int> > Q; vector<pair<int,ll> > V; V.emplace_back(mp(0,0ll)); int sz = 0; ll ret = 0; while(deg[x]) { -- deg[x]; V.pb(mp(x, adj[x].se)); dm[x] = max(dm[x], dp[x].fi + dp[x].se); ret = max(ret, dm[x]); x = adj[x].fi; ++ sz; } fo(i,1,sz) V[i].se += V[i-1].se; fo(i,2,sz) { while(SZ(Q) && Q.back().fi < V[i-1].se + dp[V[i].fi].fi) Q.pop_back(); Q.emplace_back(mp(V[i-1].se + dp[V[i].fi].fi, V[i].fi)); } ret = max(ret, dp[V[1].fi].fi + Q.front().fi); fo(i,1,sz-1) { if(Q.front().se == V[i+1].fi) Q.pop_front(); while(SZ(Q) && Q.back().fi < V[sz].se + V[i-1].se + dp[V[i].fi].fi) Q.pop_back(); Q.emplace_back(mp(V[sz].se+V[i-1].se+dp[V[i].fi].fi, V[i].fi)); ret = max(ret, Q.front().fi + dp[V[i+1].fi].fi - V[i].se); } return ret; } int main() { sf("%d",&n); fo(i,1,n) {int u, v; sf("%d%d",&u,&v); deg[u] ++; adj[i] = (mp(u, v));} queue<int> Q; fo(i,1,n) if(!deg[i]) Q.push(i); while(!Q.empty()) { int h = Q.front(); Q.pop(); pii p = adj[h]; dm[h] = max(dm[h], dp[h].fi + dp[h].se); dm[p.fi] = max(dm[p.fi], dm[h]); ll x = dp[h].fi + p.se; if(x > dp[p.fi].fi) swap(x, dp[p.fi].fi); if(x > dp[p.fi].se) swap(x, dp[p.fi].se); if(!(--deg[p.fi])) Q.push(p.fi); } fo(i,1,n) if(deg[i]) Ans += solve(i); cout << Ans << endl; return 0; }

Compilation message (stderr)

islands.cpp: In function 'int main()':
islands.cpp:73:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d",&n);
    ^
islands.cpp:74:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  fo(i,1,n) {int u, v; sf("%d%d",&u,&v); deg[u] ++; adj[i] = (mp(u, v));}
                         ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...