Problem p03650 (C++)

Buggy submission (status: Runtime Error):
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int n;
const int maxN = 2e5 + 10;
vector<int> g[maxN];
int p[maxN];
bool used[maxN];
int mex[maxN];
bool on_cyc[maxN];
void dfs(int v) {
  used[v] = true;
  set<int> s;
  for (int to : g[v]) {
    if (on_cyc[to])
      continue;
    dfs(to);
    s.insert(mex[to]);
  }
  while (s.count(mex[v]))
    mex[v]++;
}
int val[maxN][2];
const int LG = 20;
int up[maxN][LG];
int gg[2 * maxN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    g[p[i]].emplace_back(i);
  }
  int c = 1;
  while (!used[c]) {
    used[c] = true;
    c = p[c];
  }
  vector<int> cyc;
  memset(used, 0, sizeof used);
  while (!used[c]) {
    cyc.emplace_back(c);
    on_cyc[c] = true;
    used[c] = true;
    c = p[c];
  }
  for (int v : cyc) {
    dfs(v);
  }
  for (int i = 0; i < cyc.size(); i++) {
    set<int> s;
    for (int to : g[cyc[i]]) {
      if (on_cyc[to])
        continue;
      s.insert(mex[to]);
    }
    val[i][0] = mex[cyc[i]];
    s.insert(mex[cyc[i]]);
    while (s.count(val[i][1]))
      val[i][1]++;
    assert(val[i][1] != val[i][0]);
  }
  for (int i = 0; i < cyc.size(); i++) {
    gg[i] = val[i][0];
    gg[i + cyc.size()] = val[i][1];
    int prv = (i + cyc.size() - 1) % cyc.size();
    for (int j = 0; j < 2; j++) {
      if (val[prv][0] == val[i][j])
        up[cyc.size() * j + i][0] = prv + cyc.size();
      else
        up[cyc.size() * j + i][0] = prv;
    }
  }
  for (int i = 0; i + 1 < LG; i++) {
    for (int j = 0; j < 2 * cyc.size(); j++) {
      up[j][i + 1] = up[up[j][i]][i];
    }
  }
  for (int i = 0; i < cyc.size(); i++) {
    int steps = cyc.size() - 1;
    int where = i;
    for (int j = 0; j < LG; j++) {
      if (steps & (1 << j))
        where = up[where][j];
    }
    if (gg[where] > gg[i]) {
      cout << "POSSIBLE";
      return 0;
    }
  }
  cout << "IMPOSSIBLE";
  return 0;
}

Fixed submission:
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int n;
const int maxN = 2e5 + 10;
vector<int> g[maxN];
int p[maxN];
bool used[maxN];
int mex[maxN];
bool on_cyc[maxN];
void dfs(int v) {
  used[v] = true;
  set<int> s;
  for (int to : g[v]) {
    if (on_cyc[to])
      continue;
    dfs(to);
    s.insert(mex[to]);
  }
  while (s.count(mex[v]))
    mex[v]++;
}
int val[maxN][2];
const int LG = 20;
int up[2 * maxN][LG];
int gg[2 * maxN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    g[p[i]].emplace_back(i);
  }
  int c = 1;
  while (!used[c]) {
    used[c] = true;
    c = p[c];
  }
  vector<int> cyc;
  memset(used, 0, sizeof used);
  while (!used[c]) {
    cyc.emplace_back(c);
    on_cyc[c] = true;
    used[c] = true;
    c = p[c];
  }
  for (int v : cyc) {
    dfs(v);
  }
  for (int i = 0; i < cyc.size(); i++) {
    set<int> s;
    for (int to : g[cyc[i]]) {
      if (on_cyc[to])
        continue;
      s.insert(mex[to]);
    }
    val[i][0] = mex[cyc[i]];
    s.insert(mex[cyc[i]]);
    while (s.count(val[i][1]))
      val[i][1]++;
    assert(val[i][1] != val[i][0]);
  }
  for (int i = 0; i < cyc.size(); i++) {
    gg[i] = val[i][0];
    gg[i + cyc.size()] = val[i][1];
    int prv = (i + cyc.size() - 1) % cyc.size();
    for (int j = 0; j < 2; j++) {
      if (val[prv][0] == val[i][j])
        up[cyc.size() * j + i][0] = prv + cyc.size();
      else
        up[cyc.size() * j + i][0] = prv;
    }
  }
  for (int i = 0; i + 1 < LG; i++) {
    for (int j = 0; j < 2 * cyc.size(); j++) {
      up[j][i + 1] = up[up[j][i]][i];
    }
  }
  for (int i = 0; i < cyc.size(); i++) {
    int steps = cyc.size() - 1;
    int where = i;
    for (int j = 0; j < LG; j++) {
      if (steps & (1 << j))
        where = up[where][j];
    }
    if (gg[where] > gg[i]) {
      cout << "POSSIBLE";
      return 0;
    }
  }
  cout << "IMPOSSIBLE";
  return 0;
}