답안 #1055412

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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n), l(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> l[i];
    --a[i];
  }
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    g[i].push_back(a[i]);
    g[a[i]].push_back(i);
  }
  vector<bool> was(n);
  long long res = 0;
  vector<long long> d(n, -1);
  vector<vector<long long>> dp(n, vector<long long>(3));
  for (int i = 0; i < n; i++) {
    if (was[i]) {
      continue;
    }
    vector<int> que(1, i);
    for (int b = 0; b < (int) que.size(); b++) {
      int x = que[b];
      for (int y : g[x]) {
        if (!was[y]) {
          was[y] = true;
          que.push_back(y);
        }
      }
    }
    for (int x : que) {
      was[x] = false;
    }
    int x = i;
    while (!was[x]) {
      was[x] = true;
      x = a[x];
    }
    int y = a[x];
    vector<int> cyc(1, x);
    while (y != x) {
      cyc.push_back(y);
      y = a[y];
    }
    for (int x : que) {
      was[x] = true;
    }
    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];
    }
    function<void(int, int)> Solve = [&](int v, int pv) {
      for (int u : g[v]) {
        if (d[u] != -1 || u == pv) {
          continue;
        }
        Solve(u, v);
        dp[v][2] = max(dp[v][2], dp[u][2]);
        dp[v][2] = max(dp[v][2], dp[v][1] + dp[u][1] + l[u]);
        dp[v][1] = max(dp[v][1], dp[u][1] + l[u]);
      }
    };
    long long c = 0;
    for (int i : cyc) {
      Solve(i, i);
      c = max(c, dp[i][1]);
      c = max(c, dp[i][2]);
    }
    int sz = (int) cyc.size();
    vector<vector<long long>> mx(8 * sz, vector<long long>(2));
    function<void(int, int, int)> Build = [&](int x, int l, int r) {
      if (l == r) {
        mx[x][0] = dp[cyc[l]][1] + d[cyc[l]];
        mx[x][1] = dp[cyc[l]][1] - d[cyc[l]];
        return;
      }
      int mid = (l + r) >> 1;
      Build(x * 2, l, mid);
      Build(x * 2 + 1, mid + 1, r);
      mx[x][0] = max(mx[x * 2][0], mx[x * 2 + 1][0]);
      mx[x][1] = max(mx[x * 2][1], mx[x * 2 + 1][1]);
    };
    function<long long(int, int, int, int, int, int)> Query = [&](int x, int t, int l, int r, int ll, int rr) {
      if (ll <= l && r <= rr) {
        return mx[x][t];
      }
      int mid = (l + r) >> 1;
      if (rr <= mid) {
        return Query(x * 2, t, l, mid, ll, rr);
      } else if (ll > mid) {
        return Query(x * 2 + 1, t, mid + 1, r, ll, rr); 
      } else {
        return max(Query(x * 2, t, l, mid, ll, rr), Query(x * 2 + 1, t, mid + 1, r, ll, rr));
      }
    }; 
    Build(1, 0, sz - 1);
    long long t = d[cyc.back()] + l[cyc.back()];
    int ptr = 0;
    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]][1] + Query(1, 0, 0, sz - 1, pos + 1, sz - 1));
      }
      if (pos > i) {
        c = max(c, t + d[cyc[i]] + dp[cyc[i]][1] + Query(1, 1, 0, sz - 1, i + 1, pos));
      }
    }
    res += c;
  }
  cout << res << '\n';
  return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:106:9: warning: unused variable 'ptr' [-Wunused-variable]
  106 |     int ptr = 0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 604 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 344 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 2 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3232 KB Output is correct
2 Correct 18 ms 11656 KB Output is correct
3 Correct 6 ms 3164 KB Output is correct
4 Correct 5 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 15776 KB Output is correct
2 Correct 48 ms 20980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 35276 KB Output is correct
2 Correct 139 ms 63188 KB Output is correct
3 Correct 147 ms 99120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 57372 KB Output is correct
2 Runtime error 123 ms 131072 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 151 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 183 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -