Submission #900430

#TimeUsernameProblemLanguageResultExecution timeMemory
900430abcvuitunggioIslands (IOI08_islands)C++17
67 / 100
657 ms131072 KiB
#include <iostream> #include <bitset> #include <vector> #include <queue> using namespace std; const int mxn=1000001; const long long INF=1e18; vector <int> ke[mxn]; queue <int> q; int n,p[mxn],l[mxn],last[mxn],r[mxn],x,u; bitset <mxn> ch; long long dp[mxn],d[mxn],val,res,a,b,sum,s; int dfs(int u){ int mx=u; for (int v:ke[u]){ dfs(v); if (dp[v]+l[v]>dp[u]) mx=v; dp[u]=max(dp[u],dp[v]+l[v]); } return mx; } int dfs2(int u, int par){ int mx=0; for (int v:ke[u]) if (v!=par) mx=max(mx,dfs2(v,u)+l[v]); if (ch[u]&&p[u]!=par) mx=max(mx,dfs2(p[u],u)+l[u]); return mx; } int f(int i){ return (r[i]==i?i:r[i]=f(r[i])); } int main(){ ios_base::sync_with_stdio(NULL);cin.tie(nullptr); cin >> n; for (int i=1;i<=n;i++){ cin >> p[i] >> l[i]; last[p[i]]++; r[i]=i; } for (int i=1;i<=n;i++) if (!last[i]) q.push(i); while (!q.empty()){ u=q.front(); q.pop(); ch[u]=1; r[f(u)]=f(p[u]); if (!--last[p[u]]) q.push(p[u]); } for (int i=1;i<=n;i++) if (ch[i]) ke[p[i]].push_back(i); for (int i=1;i<=n;i++) if (!ch[i]) d[i]=dfs2(dfs(i),0); for (int i=1;i<=n;i++){ last[i]=0; vector <int>().swap(ke[i]); } for (int i=1;i<=n;i++){ if (ch[i]) continue; sum=0,a=-INF,b=-INF; x=i; while (true){ sum+=l[x]; ch[x]=1; last[p[x]]=x; x=p[x]; if (x==i) break; } val=0,s=sum; for (x=last[i];;x=last[x]){ val=max(max(val,d[x]),dp[x]+max(a-s,b+sum+s)); a=max(a,dp[x]+s); b=max(b,dp[x]-s); s-=l[last[x]]; if (x==i) break; } res+=val; } 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...