답안 #1055444

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1055444 2024-08-12T19:36:10 Z MilosMilutinovic Islands (IOI08_islands) C++14
90 / 100
427 ms 131072 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1000000;

int a[N], l[N];

long long d[N], dp[N];

vector<int> g[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> l[i];
    --a[i];
  }
  for (int i = 0; i < n; i++) {
    g[a[i]].push_back(i);
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    d[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    if (d[i] != -1) {
      continue;
    }
    vector<int> que(1, i);
    d[i] = 0;
    for (int b = 0; b < (int) que.size(); b++) {
      int x = que[b];
      if (d[a[x]] == -1) {
        d[a[x]] = 0;
        que.push_back(a[x]);
      }
      for (int y : g[x]) {
        if (d[y] == -1) {
          d[y] = 0;
          que.push_back(y);
        }
      }
    }
    for (int x : que) {
      d[x] = -1;
    }
    int x = i;
    while (d[x] == -1) {
      d[x] = 0;
      x = a[x];
    }
    for (int x : que) {
      d[x] = -1;
    }
    int y = a[x];
    vector<int> cyc(1, x);
    while (y != x) {
      cyc.push_back(y);
      y = a[y];
    }
    d[cyc[0]] = 0;
    for (int i = 1; i < (int) cyc.size(); i++) {
      int x = cyc[i - 1], y = cyc[i];
      d[y] = d[x] + l[x];
    }
    for (int x : que) {
      if (d[x] == -1) {
        d[x] = -2;
      }
    }
    vector<int>().swap(que);
    long long c = 0;
    function<void(int, int)> Solve = [&](int v, int pv) {
      for (int u : g[v]) {
        if (d[u] != -2 || u == pv) {
          continue;
        }
        Solve(u, v);
        c = max(c, dp[v] + dp[u] + l[u]);
        dp[v] = max(dp[v], dp[u] + l[u]);
      }
    };
    for (int i : cyc) {
      Solve(i, i);
      c = max(c, dp[i]);
    }
    int sz = (int) cyc.size();
    vector<long long> mx(sz, -(long long) 1e18);
    for (int i = sz - 1; i >= 0; i--) {
      if (i + 1 < sz) {
        mx[i] = mx[i + 1];
      }
      // mx[i][0] = max(mx[i][0], d[cyc[i]] + dp[cyc[i]]);
      // mx[i][1] = max(mx[i][1], -d[cyc[i]] + dp[cyc[i]]);
      mx[i] = max(mx[i], d[cyc[i]] + dp[cyc[i]]);
    }
    long long t = d[cyc.back()] + l[cyc.back()];
    for (int i = 0; i < sz; i++) {
      int low = i + 1, high = sz - 1, pos = i;
      while (low <= high) {
        int mid = (low + high) >> 1;
        if (t - (d[cyc[mid]] - d[cyc[i]]) >= (d[cyc[mid]] - d[cyc[i]])) {
          pos = mid;
          low = mid + 1;
        } else {
          high = mid - 1;
        }
      }
      if (pos + 1 < sz) {
        c = max(c, -d[cyc[i]] + dp[cyc[i]] + mx[pos + 1]);
      }
      if (pos > i) {
        // c = max(c, t + d[cyc[i]] + dp[cyc[i]] + mx[i + 1][1]);
      }
    }
    for (int i = sz - 1; i >= 0; i--) {
      if (i + 1 < sz) {
        mx[i] = mx[i + 1];
      } else {
        mx[i] = -(long long) 1e18;
      }
      mx[i] = max(mx[i], -d[cyc[i]] + dp[cyc[i]]);
    }
    for (int i = 0; i < sz; i++) {
      int low = i + 1, high = sz - 1, pos = i;
      while (low <= high) {
        int mid = (low + high) >> 1;
        if (t - (d[cyc[mid]] - d[cyc[i]]) >= (d[cyc[mid]] - d[cyc[i]])) {
          pos = mid;
          low = mid + 1;
        } else {
          high = mid - 1;
        }
      }
      if (pos > i) {
        c = max(c, t + d[cyc[i]] + dp[cyc[i]] + mx[i + 1]);
      }
    }
    vector<long long>().swap(mx);
    vector<int>().swap(cyc);
    res += c;
  }
  cout << res << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 23900 KB Output is correct
2 Correct 5 ms 23900 KB Output is correct
3 Correct 6 ms 23900 KB Output is correct
4 Correct 6 ms 23900 KB Output is correct
5 Correct 5 ms 23900 KB Output is correct
6 Correct 6 ms 23896 KB Output is correct
7 Correct 6 ms 23900 KB Output is correct
8 Correct 7 ms 23900 KB Output is correct
9 Correct 6 ms 23988 KB Output is correct
10 Correct 6 ms 23900 KB Output is correct
11 Correct 7 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 23900 KB Output is correct
2 Correct 6 ms 23968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 23900 KB Output is correct
2 Correct 6 ms 24156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 24664 KB Output is correct
2 Correct 13 ms 25436 KB Output is correct
3 Correct 10 ms 24964 KB Output is correct
4 Correct 7 ms 24408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 25932 KB Output is correct
2 Correct 25 ms 28180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 32444 KB Output is correct
2 Correct 57 ms 36148 KB Output is correct
3 Correct 56 ms 36212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 41252 KB Output is correct
2 Correct 88 ms 46884 KB Output is correct
3 Correct 120 ms 56432 KB Output is correct
4 Correct 132 ms 57616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 49612 KB Output is correct
2 Correct 312 ms 67424 KB Output is correct
3 Correct 139 ms 57100 KB Output is correct
4 Correct 174 ms 69332 KB Output is correct
5 Correct 172 ms 68592 KB Output is correct
6 Correct 427 ms 67508 KB Output is correct
7 Correct 191 ms 72820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 164 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -