Submission #864534

#TimeUsernameProblemLanguageResultExecution timeMemory
864534HuyQuang_re_ZeroIslands (IOI08_islands)C++14
100 / 100
592 ms111024 KiB
#include <bits/stdc++.h> #define ll long long #define db long double #define N 1000005 #define II pair <int,int> #define III pair <ll,II> #define IV pair <vector <int>,vector <int> > #define fst first #define snd second #define BIT(x,i) ((x>>i)&1) #define Log(x) (31-__builtin_clz((int)x)) #define LogLL(x) (63-__builtin_clzll((ll)x)) #define pi acos(-1) #define to_radian(x) (x*pi/180.0) #define to_degree(x) (x*180.0/pi) using namespace std; vector <int> a[N]; int nxt[N],w[N],deg[N]; bool visited[N]; ll n,i,u,v,k,sum,res; ll f[N],ma0[N],ma1[N]; const ll oo=round(1e18); int main() { // freopen("islands.inp","r",stdin); // freopen("islands.out","w",stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(u=1;u<=n;u++) { cin>>v>>k; nxt[u]=v; a[v].push_back(u); deg[v]++; w[u]=k; } for(u=1;u<=n;u++) if(visited[u]==0) { queue <int> q; vector <int> node; ll local_res=0,ncycle=0,ma=-oo; vector <ll> c,dis; c.push_back(0); dis.push_back(0); q.push(u); visited[u]=1; while(q.size()>0) { int u=q.front(); q.pop(); node.push_back(u); for(int v:a[u]) if(visited[v]==0) q.push(v),visited[v]=1; if(visited[nxt[u]]==0) q.push(nxt[u]),visited[nxt[u]]=1; } for(int u:node) if(deg[u]==0) q.push(u); while(q.size()>0) { int u=q.front(),v=nxt[u]; q.pop(); ll k=f[u]+w[u]; f[v]=max(f[v],k); if(ma0[v]<k) ma0[v]=k; if(ma1[v]<ma0[v]) swap(ma0[v],ma1[v]); deg[v]--; if(deg[v]==0) q.push(v); } int x=0,u; for(int u:node) if(deg[u]>0) { x=u; break; } for(int u:node) local_res=max(local_res,ma0[u]+ma1[u]); /////////////////////////////////////////////////////////////////// u=x; ll sum=0; ncycle++; c.push_back(u); dis.push_back(sum); sum+=w[u]; u=nxt[u]; while(u!=x) { ncycle++; c.push_back(u); dis.push_back(sum); sum+=w[u]; u=nxt[u]; } //////////////////////////////////////////////////////////////////////////////// for(i=1;i<=ncycle;i++) { u=c[i]; c[i]=f[u]; } deque <int> dq; int j=1; for(i=1;i<ncycle;i++) { while(j<ncycle && sum-dis[j+1]+dis[i]>=dis[j+1]-dis[i]) { j++; while(dq.size()>0 && c[dq.back()]-dis[dq.back()]<c[j]-dis[j]) dq.pop_back(); dq.push_back(j); } if(dq.size()>0 && dq.front()==i) dq.pop_front(); if(dq.size()>0) local_res=max(local_res,c[i]+c[dq.front()]+sum+dis[i]-dis[dq.front()]); } j=ncycle+1; for(i=ncycle-1;i>=1;i--) { while(j-1>i && sum-dis[j-1]+dis[i]<dis[j-1]-dis[i]) { j--; ma=max(ma,c[j]+dis[j]); } local_res=max(local_res,c[i]-dis[i]+ma); } res+=local_res; } cout<<res; }
#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...