Submission #900351

#TimeUsernameProblemLanguageResultExecution timeMemory
900351abcvuitunggioIslands (IOI08_islands)C++17
70 / 100
173 ms131072 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int mxn=1000001,INF=1e18; vector <int> ke[mxn],ve[mxn],V[mxn],s,a,b,tmp; int n,p[mxn],l[mxn],vis[mxn],vis2[mxn],root[mxn],pos[mxn],cycle[mxn],dp[mxn],dp2[mxn],d[mxn],mx,idx,res; void dfs(int u){ for (int v:ke[u]){ d[v]=d[u]+l[v]; dfs(v); dp[u]=max(dp[u],dp[v]+l[v]); } } void dfs2(int u){ pair <int, int> mx={-1,0},mx2={-1,0}; for (int v:ke[u]){ auto tmp=make_pair(dp[v]+l[v],v); if (tmp>mx){ mx2=mx; mx=tmp; } else mx2=max(mx2,tmp); } for (int v:ke[u]){ dp2[v]=max(dp2[u],(mx.second==v?mx2.first:mx.first))+l[v]; dfs2(v); } } int32_t main(){ ios_base::sync_with_stdio(NULL);cin.tie(nullptr); cin >> n; for (int i=1;i<=n;i++) cin >> p[i] >> l[i]; for (int i=1;i<=n;i++) if (!vis[i]){ int j=i; tmp.clear(); while (true){ tmp.push_back(i); vis2[i]=tmp.size(); i=p[i]; if (vis[i]){ for (int j:tmp){ root[j]=root[i]; cycle[j]=cycle[i]; ke[p[j]].push_back(j); vis[j]=1; } break; } if (vis2[i]){ for (int j=vis2[i]-1;j<tmp.size();j++){ root[tmp[j]]=tmp[j]; cycle[tmp[j]]=idx; pos[tmp[j]]=j-vis2[i]+1; ve[idx].push_back(tmp[j]); } for (int j=0;j<vis2[i]-1;j++){ root[tmp[j]]=i; ke[tmp[j+1]].push_back(tmp[j]); } for (int j:tmp){ cycle[j]=cycle[i]; vis[j]=1; } idx++; break; } } i=j; } for (int i=1;i<=n;i++) if (root[i]==i){ dfs(i); dfs2(i); } for (int i=1;i<=n;i++) V[cycle[i]].push_back(i); for (int i=0;i<idx;i++){ int sz=ve[i].size(); a.assign(sz,0); b.assign(sz,0); a.push_back(-INF); b.push_back(-INF); for (int j=0;j<sz;j++) s.push_back((s.empty()?0:s.back())+l[ve[i][(j?j:sz)-1]]); for (int j=sz-1;j>=0;j--){ a[j]=max(a[j+1],dp[ve[i][j]]+s[j]); b[j]=max(b[j+1],dp[ve[i][j]]-s[j]); } mx=0; for (int j:V[i]){ int k=pos[root[j]],val=max(max(dp[j],dp2[j]),d[j]+max(a[k+1]-s[k],b[k+1]+s.back()+s[k])); mx=max(mx,val); } a.clear(); b.clear(); s.clear(); res+=mx; } cout << res; }

Compilation message (stderr)

islands.cpp: In function 'int32_t main()':
islands.cpp:53:43: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |                     for (int j=vis2[i]-1;j<tmp.size();j++){
      |                                          ~^~~~~~~~~~~
#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...