답안 #1055469

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

using namespace std;

const int N = 1000000;

int a[N];

long long l[N], d[N], mx[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> cyc(1, i);
    d[i] = 0;
    for (int b = 0; b < (int) cyc.size(); b++) {
      int x = cyc[b];
      if (d[a[x]] == -1) {
        d[a[x]] = 0;
        cyc.push_back(a[x]);
      }
      for (int y : g[x]) {
        if (d[y] == -1) {
          d[y] = 0;
          cyc.push_back(y);
        }
      }
    }
    for (int x : cyc) {
      d[x] = -1;
    }
    int x = i;
    while (d[x] == -1) {
      d[x] = 0;
      x = a[x];
    }
    for (int x : cyc) {
      d[x] = -2;
    }
    int y = a[x];
    vector<int>(1, x).swap(cyc);
    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];
    }
    long long t = d[cyc.back()] + l[cyc.back()];
    long long c = 0;
    function<long long(int, int)> Solve = [&](int v, int pv) {
      long long dp = 0;
      for (int u : g[v]) {
        if (d[u] != -2 || u == pv) {
          continue;
        }
        long long val = Solve(u, v);
        c = max(c, dp + val);
        dp = max(dp, val);
      }
      dp += l[v];
      l[v] = dp - l[v];
      return dp;
    };
    for (int i : cyc) {
      Solve(i, i);
      c = max(c, l[i]);
    }
    int sz = (int) cyc.size();
    for (int i = sz - 1; i >= 0; i--) {
      if (i + 1 < sz) {
        mx[i] = mx[i + 1];
      } else {
        mx[i] = d[cyc[i]] + l[cyc[i]];
      }
      mx[i] = max(mx[i], d[cyc[i]] + l[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 + 1 < sz) {
        c = max(c, -d[cyc[i]] + l[cyc[i]] + mx[pos + 1]);
      }
    }
    for (int i = sz - 1; i >= 0; i--) {
      if (i + 1 < sz) {
        mx[i] = mx[i + 1];
      } else {
        mx[i] = -d[cyc[i]] + l[cyc[i]];
      }
      mx[i] = max(mx[i], -d[cyc[i]] + l[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]] + l[cyc[i]] + mx[i + 1]);
      }
    }
    vector<int>().swap(cyc);
    res += c;
  }
  cout << res << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 23896 KB Output is correct
2 Correct 8 ms 23764 KB Output is correct
3 Correct 8 ms 23956 KB Output is correct
4 Correct 7 ms 23900 KB Output is correct
5 Correct 8 ms 23896 KB Output is correct
6 Correct 9 ms 23900 KB Output is correct
7 Correct 9 ms 23896 KB Output is correct
8 Correct 9 ms 23900 KB Output is correct
9 Correct 8 ms 23900 KB Output is correct
10 Correct 8 ms 23984 KB Output is correct
11 Correct 8 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 23900 KB Output is correct
2 Correct 8 ms 24004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 23900 KB Output is correct
2 Correct 9 ms 24156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 24668 KB Output is correct
2 Correct 16 ms 25792 KB Output is correct
3 Correct 12 ms 25196 KB Output is correct
4 Correct 9 ms 24412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 26716 KB Output is correct
2 Correct 25 ms 29520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 34816 KB Output is correct
2 Correct 52 ms 38772 KB Output is correct
3 Correct 57 ms 39924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 45932 KB Output is correct
2 Correct 94 ms 50472 KB Output is correct
3 Correct 128 ms 64668 KB Output is correct
4 Correct 136 ms 64528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 131 ms 69060 KB Output is correct
2 Correct 315 ms 82796 KB Output is correct
3 Correct 141 ms 65400 KB Output is correct
4 Correct 184 ms 80496 KB Output is correct
5 Correct 174 ms 76808 KB Output is correct
6 Correct 413 ms 80832 KB Output is correct
7 Correct 222 ms 87928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 173 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -