Submission #198790

#TimeUsernameProblemLanguageResultExecution timeMemory
198790arnold518Islands (IOI08_islands)C++14
64 / 100
483 ms131072 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 1e6; int N; vector<pll> adj[MAXN+10]; pll to[MAXN+10]; ll ans; bool vis[MAXN+10], chk[MAXN+10]; ll dfs(int now, ll dep) { vis[now]=true; ll ret=dep; for(auto nxt : adj[now]) { if(vis[nxt.first]) continue; ret=max(ret, dfs(nxt.first, dep+nxt.second)); } return ret; } int main() { int i, j; scanf("%d", &N); for(i=1; i<=N; i++) { int u=i, v, w; scanf("%d%d", &v, &w); to[u]={v, w}; adj[v].push_back({u, w}); } for(i=1; i<=N; i++) { if(vis[i]) continue; int now=i; vector<int> cyc; while(!chk[now]) { cyc.push_back(now); chk[now]=1; now=to[now].first; } reverse(cyc.begin(), cyc.end()); while(!cyc.empty() && cyc.back()!=now) cyc.pop_back(); reverse(cyc.begin(), cyc.end()); //for(auto it : cyc) printf("%d ", it); printf("\n"); vector<ll> A, D; ll val=0, s=0; D.push_back(0); for(auto it : cyc) vis[it]=1; for(auto it : cyc) A.push_back(dfs(it, 0)); for(auto it : cyc) D.push_back(D.back()+to[it].second), s+=to[it].second; D.pop_back(); for(auto it : A) val=max(val, it); ll t=-1e18; for(j=0; j<cyc.size(); j++) { if(j) val=max(val, D[j]+A[j]+t); t=max(t, A[j]-D[j]); } t=-1e18; for(j=0; j<cyc.size(); j++) { if(j) val=max(val, s-D[j]+A[j]+t); t=max(t, D[j]+A[j]); } ans+=val; } printf("%lld", ans); }

Compilation message (stderr)

islands.cpp: In function 'int main()':
islands.cpp:73:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0; j<cyc.size(); j++)
            ~^~~~~~~~~~~
islands.cpp:80:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0; j<cyc.size(); j++)
            ~^~~~~~~~~~~
islands.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
islands.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~
#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...