Submission #900441

#TimeUsernameProblemLanguageResultExecution timeMemory
900441abcvuitunggioIslands (IOI08_islands)C++17
100 / 100
204 ms29776 KiB
#include <bits/stdc++.h> using namespace std; const int mxn=1000001; const long long INF=1e18; queue <int> q; int n,p[mxn],l[mxn],last[mxn],x,u; bitset <mxn> ch; long long dp[mxn],d[mxn],val,res,a,b,sum,s; 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]]++; } for (int i=1;i<=n;i++) if (!last[i]) q.push(i); while (!q.empty()){ u=q.front(); q.pop(); ch[u]=1; d[p[u]]=max(max(d[p[u]],d[u]),dp[p[u]]+dp[u]+l[u]); dp[p[u]]=max(dp[p[u]],dp[u]+l[u]); if (!--last[p[u]]) q.push(p[u]); } memset(last,0,sizeof(last)); for (int i=1;i<=n;i++){ if (ch[i]) continue; sum=0,a=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...