Submission #1020260

# Submission time Handle Problem Language Result Execution time Memory
1020260 2024-07-11T18:28:51 Z Hydrolyzed Islands (IOI08_islands) C++14
100 / 100
190 ms 52304 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MxN = 1000010;

pair<int, ll> nxt[MxN];
bitset<MxN> cycle;
int in[MxN];
queue<int> q;
ll dist[MxN], dp[MxN];

signed main(int argc, char *argv[]) {
  cin.tie(nullptr)->ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for(int i=1; i<=n; ++i) {
    cin >> nxt[i].first >> nxt[i].second;
    in[nxt[i].first]++;
  }
  for(int i=1; i<=n; ++i) {
    if(in[i] != 0) {
      continue;
    }
    q.emplace(i);
  }
  while(!q.empty()) {
    int u = q.front(); q.pop();
    int v = nxt[u].first;
    ll w = nxt[u].second;
    dp[v] = max({dp[u], dp[v], dist[v] + dist[u] + w});
    dist[v] = max(dist[v], dist[u] + w);
    if (--in[v] == 0) {
        q.emplace(v);
    }
  }
#ifdef ICY
  for(int i=1; i<=n; ++i) {
    cerr << "D: " << dp[i] << " " << dist[i] << "\n";
  }
#endif
  auto calc = [&](int pos) {
    int cur_pos = pos;
    cur_pos = nxt[pos].first;
    ll cur_dist = nxt[pos].second;
    ll with = dist[pos];
    ll without = dist[pos];
    ll max_dp_with = dp[pos];
    ll max_dp_without = -1e18 - 100ll;
#ifdef ICY
    cerr << "PRE: " << max_dp_with << " " << max_dp_without << " " << cur_dist << "\n";
#endif
    while(cur_pos != pos) {
      in[cur_pos] = 0;
      max_dp_with = max({max_dp_with, dp[cur_pos], dist[cur_pos] + cur_dist + with});
      max_dp_without = max(max_dp_without, dist[cur_pos] - cur_dist + without);
      with = max(with, dist[cur_pos] - cur_dist);
      without = max(without, dist[cur_pos] + cur_dist);
      cur_dist += nxt[cur_pos].second;
      cur_pos = nxt[cur_pos].first;
#ifdef ICY
      cerr << "A: " << max_dp_with << " " << max_dp_without << "\n";
#endif
   }
    return max(max_dp_without + cur_dist, max_dp_with);
  };
  ll answer = 0ll;
  for(int i=1; i<=n; ++i) {
    if(in[i] == 0) {
      continue;
    }
#ifdef ICY
    cerr << "X: " << i << "\n";
#endif
    answer += calc(i);
  }
  cout << answer << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 528 KB Output is correct
2 Correct 1 ms 584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1216 KB Output is correct
2 Correct 6 ms 2136 KB Output is correct
3 Correct 5 ms 1372 KB Output is correct
4 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2904 KB Output is correct
2 Correct 13 ms 4964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 10320 KB Output is correct
2 Correct 26 ms 8532 KB Output is correct
3 Correct 37 ms 13136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 18972 KB Output is correct
2 Correct 66 ms 24640 KB Output is correct
3 Correct 67 ms 18964 KB Output is correct
4 Correct 95 ms 33104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 35544 KB Output is correct
2 Correct 148 ms 43092 KB Output is correct
3 Correct 104 ms 39504 KB Output is correct
4 Correct 133 ms 49956 KB Output is correct
5 Correct 131 ms 49744 KB Output is correct
6 Correct 190 ms 50516 KB Output is correct
7 Correct 147 ms 52304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 50844 KB Output is correct
2 Correct 145 ms 51028 KB Output is correct
3 Correct 132 ms 35412 KB Output is correct
4 Correct 131 ms 34896 KB Output is correct
5 Correct 134 ms 50256 KB Output is correct
6 Correct 149 ms 49308 KB Output is correct
7 Correct 173 ms 51192 KB Output is correct