Submission #331591

# Submission time Handle Problem Language Result Execution time Memory
331591 2020-11-29T03:25:39 Z quindecim Islands (IOI08_islands) C++14
90 / 100
726 ms 131076 KB
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
const int MOD = 1e9+7;
const int MX = 1e6+10;
const ll INF = 0x3f3f3f3f3f3f3f3f;

int n, m, a[MX], w[MX];
vector<int> adj[MX];
ll ans, ret;
vector<ll> V, S;
bool vv[MX];

inline int cycle(int u) {
  if(vv[u]) return u;
  vv[u] = 1;
  int r = cycle(a[u]);
  if(r) {
    V.push_back(u);
    S.push_back(w[u]);
    if(r==u) return 0;
    else return r;
  }
  vv[u] = 0;
  return 0;
}

inline ll dfs(int u) {
  ll mx = 0;
  vv[u] = 1;
  for(int v : adj[u]) {
    if(!vv[v]) {
      ll r = dfs(v) + w[v];
      ret = max(ret, mx+r);
      mx = max(mx, r);
    }
  }
  return mx;
}

void solve(int u) {
  V.clear(); S.clear();
  cycle(u);
  reverse(V.begin(), V.end());
  reverse(S.begin(), S.end());
  for(auto &v : V) v = dfs(v);
  m = V.size();
  for(int i = m-1; i > 0; --i) swap(S[i], S[i-1]);
  for(int i = 1; i < m; ++i) S[i] += S[i-1];
  ll m1 = -INF, m2 = -INF;
  for(int i = 0; i < m; ++i) {
    ret = max(ret, max(m1+S[i]+V[i], m2-S[i]+V[i]+S[m-1]));
    m1 = max(m1, V[i]-S[i]);
    m2 = max(m2, V[i]+S[i]);
  }
}


signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for(ll i = 1; i <= n; ++i) {
    cin >> a[i] >> w[i];
    adj[a[i]].push_back(i);
  }
  for(ll i = 1; i <= n; ++i)
    if(!vv[i]) solve(i), ans+=ret, ret = 0;
  cout << ans << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23936 KB Output is correct
2 Correct 18 ms 23804 KB Output is correct
3 Correct 22 ms 23916 KB Output is correct
4 Correct 18 ms 23788 KB Output is correct
5 Correct 18 ms 23788 KB Output is correct
6 Correct 17 ms 23916 KB Output is correct
7 Correct 17 ms 23916 KB Output is correct
8 Correct 21 ms 23788 KB Output is correct
9 Correct 18 ms 23916 KB Output is correct
10 Correct 18 ms 23788 KB Output is correct
11 Correct 17 ms 23788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23916 KB Output is correct
2 Correct 18 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24044 KB Output is correct
2 Correct 19 ms 24172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 24684 KB Output is correct
2 Correct 40 ms 26220 KB Output is correct
3 Correct 27 ms 24940 KB Output is correct
4 Correct 23 ms 24428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 27416 KB Output is correct
2 Correct 48 ms 29976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 34792 KB Output is correct
2 Correct 87 ms 40544 KB Output is correct
3 Correct 108 ms 43396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 42860 KB Output is correct
2 Correct 179 ms 53184 KB Output is correct
3 Correct 193 ms 67328 KB Output is correct
4 Correct 232 ms 69760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 259 ms 52560 KB Output is correct
2 Correct 562 ms 89160 KB Output is correct
3 Correct 244 ms 58220 KB Output is correct
4 Correct 316 ms 79184 KB Output is correct
5 Correct 309 ms 77312 KB Output is correct
6 Correct 726 ms 64716 KB Output is correct
7 Correct 352 ms 94152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 360 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -