Problem p03650 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \
  cout << #a << ":";                                                           \
  rep(pachico, n) cout << " " << a[pachico];                                   \
  cout << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(pachico, v.size()) cout << " " << v[pachico];                            \
  cout << endl
#define svecp(v)                                                               \
  cout << #v << ":";                                                           \
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(pachico, s) cout << " " << pachico;                                     \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

class Namori {
public:
  int V;
  vector<vector<int>> G, rG, graph;
  vector<int> deg, loop;
  vector<bool> flag, inloop;
  vector<set<int>> st;
  Namori(int node_size)
      : V(node_size), G(V), rG(V), deg(V, 0), flag(V, false), inloop(V, false),
        st(V) {}
  void add_edge(int u, int v) {
    G[u].push_back(v), rG[v].push_back(u);
    deg[u]++, deg[v]++;
  }
  void dfs(int u) {
    loop.push_back(u);
    inloop[u] = true;
    for (int v : G[u]) {
      if (flag[v])
        continue;
      flag[v] = true;
      dfs(v);
    }
  }
  void build() {
    graph.resize(V);
    queue<int> que;
    for (int i = 0; i < V; i++) {
      if (deg[i] == 1) {
        que.push(i);
        flag[i] = true;
      }
    }
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      for (int v : rG[p]) {
        if (flag[v])
          continue;
        deg[v]--;
        // サイクルから端
        graph[v].push_back(p);
        // 端からサイクル
        graph[p].push_back(v);
        if (deg[v] > 1)
          continue;
        que.push(v);
        flag[v] = true;
      }
    }
    for (int i = 0; i < V; i++) {
      if (flag[i])
        continue;
      flag[i] = true;
      dfs(i);
      break;
    }
  }
  int dfs2(int u) {
    set<int> hoge;
    each(v, G[u]) { hoge.insert(dfs2(v)); }
    int s = 0;
    auto it = hoge.begin();
    while (it != hoge.end()) {
      if (*it != s)
        break;
      it++, s++;
    }
    return s;
  }
  bool alloc(int num) {
    vi al(len(loop));
    al[loop[0]] = num;
    auto nst = st;
    rrep(i, len(loop)) {
      nst[loop[i]].insert(al[loop[(i + 1) % len(loop)]]);
      auto it = nst[loop[i]].begin();
      int s = 0;
      while (it != nst[loop[i]].end()) {
        if (*it != s)
          break;
        it++, s++;
      }
      al[loop[i]] = s;
    }
    return (al[loop[0]] == num);
  }
  bool solve() {
    rep(i, V) {
      each(v, graph[i]) {
        if (!inloop[v]) {
          st[i].insert(dfs2(v));
        }
      }
    }
    int s = loop[0];
    int nw = 0;
    int emp = -1;
    auto it = st[s].begin();
    while (true) {
      if (it == st[s].end()) {
        if (emp >= 0)
          break;
        emp = nw++;
        continue;
      }
      if (*it != nw) {
        if (emp >= 0) {
          break;
        }
        emp = nw++;
        continue;
      }
      it++, nw++;
    }
    if (alloc(emp)) {
      return true;
    } else {
      return alloc(nw);
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  Namori g(n);
  rep(i, n) {
    int a;
    cin >> a;
    g.add_edge(a - 1, i);
  }
  g.build();
  if (g.solve()) {
    cout << "POSSIBLE\n";
  } else {
    cout << "IMPOSSIBLE\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \
  cout << #a << ":";                                                           \
  rep(pachico, n) cout << " " << a[pachico];                                   \
  cout << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(pachico, v.size()) cout << " " << v[pachico];                            \
  cout << endl
#define svecp(v)                                                               \
  cout << #v << ":";                                                           \
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(pachico, s) cout << " " << pachico;                                     \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

class Namori {
public:
  int V;
  vector<vector<int>> G, rG, graph;
  vector<int> deg, loop;
  vector<bool> flag, inloop;
  vector<set<int>> st;
  Namori(int node_size)
      : V(node_size), G(V), rG(V), deg(V, 0), flag(V, false), inloop(V, false),
        st(V) {}
  void add_edge(int u, int v) {
    G[u].push_back(v), rG[v].push_back(u);
    deg[u]++, deg[v]++;
  }
  void dfs(int u) {
    loop.push_back(u);
    inloop[u] = true;
    for (int v : G[u]) {
      if (flag[v])
        continue;
      flag[v] = true;
      dfs(v);
    }
  }
  void build() {
    graph.resize(V);
    queue<int> que;
    for (int i = 0; i < V; i++) {
      if (deg[i] == 1) {
        que.push(i);
        flag[i] = true;
      }
    }
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      for (int v : rG[p]) {
        if (flag[v])
          continue;
        deg[v]--;
        // サイクルから端
        graph[v].push_back(p);
        // 端からサイクル
        graph[p].push_back(v);
        if (deg[v] > 1)
          continue;
        que.push(v);
        flag[v] = true;
      }
    }
    for (int i = 0; i < V; i++) {
      if (flag[i])
        continue;
      flag[i] = true;
      dfs(i);
      break;
    }
  }
  int dfs2(int u) {
    set<int> hoge;
    each(v, G[u]) { hoge.insert(dfs2(v)); }
    int s = 0;
    auto it = hoge.begin();
    while (it != hoge.end()) {
      if (*it != s)
        break;
      it++, s++;
    }
    return s;
  }
  bool alloc(int num) {
    vi al(V);
    al[loop[0]] = num;
    auto nst = st;
    rrep(i, len(loop)) {
      nst[loop[i]].insert(al[loop[(i + 1) % len(loop)]]);
      auto it = nst[loop[i]].begin();
      int s = 0;
      while (it != nst[loop[i]].end()) {
        if (*it != s)
          break;
        it++, s++;
      }
      al[loop[i]] = s;
    }
    return (al[loop[0]] == num);
  }
  bool solve() {
    rep(i, V) {
      each(v, graph[i]) {
        if (!inloop[v]) {
          st[i].insert(dfs2(v));
        }
      }
    }
    int s = loop[0];
    int nw = 0;
    int emp = -1;
    auto it = st[s].begin();
    while (true) {
      if (it == st[s].end()) {
        if (emp >= 0)
          break;
        emp = nw++;
        continue;
      }
      if (*it != nw) {
        if (emp >= 0) {
          break;
        }
        emp = nw++;
        continue;
      }
      it++, nw++;
    }
    if (alloc(emp)) {
      return true;
    } else {
      return alloc(nw);
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  Namori g(n);
  rep(i, n) {
    int a;
    cin >> a;
    g.add_edge(a - 1, i);
  }
  g.build();
  if (g.solve()) {
    cout << "POSSIBLE\n";
  } else {
    cout << "IMPOSSIBLE\n";
  }
  return 0;
}