Language: C++

using namespace std;

int main() {
    int n; scanf("%d", &n);
    vector<int> p(n), dg(n);
    vector<vector<int>> g(n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &p[i]);
        p[i]--;
        dg[p[i]]++;
        g[p[i]].push_back(i);
    }
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        if (dg[i] == 0) q.push(i); 
    }
    vector<int> used(n + n), sg(n);
    while (!q.empty()) {
        int x = q.front(); q.pop();
        for (int u : g[x]) used[sg[u]] = 1;
        while (used[sg[x]]) sg[x]++;
        for (int u : g[x]) used[sg[u]] = 0;
        if (--dg[p[x]] == 0) q.push(p[x]);
    }
    vector<int> nxt(n, -1), prv(n, -1), upd(n, -1);
    vector<int> cycle;
    for (int i = 0; i < n; ++i) {
        if (dg[i] != 0) {
            for (int u : g[i]) if (dg[u] == 0) used[sg[u]] = 1;
            while (used[sg[i]]) sg[i]++;
            upd[i] = sg[i] + 1;
            while (used[upd[i]]) ++upd[i];
            for (int u : g[i]) if (dg[u] == 0) used[sg[u]] = 0;
            for (int u : g[i]) if (dg[u] > 0) { 
                nxt[i] = u;
                prv[u] = i;
            }
            cycle.push_back(i);
        }
    }

    auto Check = [&]() {
        for (int i = 0; i < n; ++i) {
            for (int u : g[i]) {
                if (sg[u] == sg[i]) return false;
                used[sg[u]] = 1;
            }
            for (int j = 0; j < sg[i]; ++j) {
                if (!used[j]) return false;
            }
            for (int u : g[i]) used[sg[u]] = 0;
        }
        return true;
    };

    if (Check()) {
        puts("POSSIBLE");
        return 0;
    }
    auto saved = sg;
    for (int j = 0; j < cycle.size(); ++j) {
        sg = saved;
        sg[cycle[j]] = upd[cycle[j]];
        int x = cycle[j];
        for (int i = 0; i < cycle.size(); ++i) {
            int r = prv[x];
            for (int k : g[r]) used[sg[k]] = 1;
            sg[r] = 0;
            while (used[sg[r]]) sg[r]++;
            // printf("r = %d to = %d\n", r, sg[r]);
            for (int k : g[r]) used[sg[k]] = 0;
            x = r;
        }
        if (Check()) {
            // printf("upd = %d\n", cycle[j]);
            puts("POSSIBLE");
            return 0;
        }
    }
    puts("IMPOSSIBLE");
}


Compiler output:
a.cc: In function 'int main()':
a.cc:4:12: error: 'scanf' was not declared in this scope
    4 |     int n; scanf("%d", &n);
      |            ^~~~~
a.cc:5:5: error: 'vector' was not declared in this scope
    5 |     vector<int> p(n), dg(n);
      |     ^~~~~~
a.cc:1:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
  +++ |+#include <vector>
    1 | using namespace std;
a.cc:5:12: error: expected primary-expression before 'int'
    5 |     vector<int> p(n), dg(n);
      |            ^~~
a.cc:6:19: error: expected primary-expression before 'int'
    6 |     vector<vector<int>> g(n);
      |                   ^~~
a.cc:8:22: error: 'p' was not declared in this scope
    8 |         scanf("%d", &p[i]);
      |                      ^
a.cc:10:9: error: 'dg' was not declared in this scope
   10 |         dg[p[i]]++;
      |         ^~
a.cc:11:9: error: 'g' was not declared in this scope
   11 |         g[p[i]].push_back(i);
      |         ^
a.cc:13:5: error: 'queue' was not declared in this scope
   13 |     queue<int> q;
      |     ^~~~~
a.cc:1:1: note: 'std::queue' is defined in header '<queue>'; this is probably fixable by adding '#include <queue>'
  +++ |+#include <queue>
    1 | using namespace std;
a.cc:13:11: error: expected primary-expression before 'int'
   13 |     queue<int> q;
      |           ^~~
a.cc:15:13: error: 'dg' was not declared in this scope
   15 |         if (dg[i] == 0) q.push(i);
      |             ^~
a.cc:15:25: error: 'q' was not declared in this scope
   15 |         if (dg[i] == 0) q.push(i);
      |                         ^
a.cc:17:12: error: expected primary-expression before 'int'
   17 |     vector<int> used(n + n), sg(n);
      |            ^~~
a.cc:18:13: error: 'q' was not declared in this scope
   18 |     while (!q.empty()) {
      |             ^
a.cc:20:22: error: 'g' was not declared in this scope
   20 |         for (int u : g[x]) used[sg[u]] = 1;
      |                      ^
a.cc:20:28: error: 'used' was not declared in this scope
   20 |         for (int u : g[x]) used[sg[u]] = 1;
      |                            ^~~~
a.cc:20:33: error: 'sg' was not declared in this scope
   20 |         for (int u : g[x]) used[sg[u]] = 1;
      |                                 ^~
a.cc:21:16: error: 'used' was not declared in this scope
   21 |         while (used[sg[x]]) sg[x]++;
      |                ^~~~
a.cc:21:21: error: 'sg' was not declared in this scope
   21 |         while (used[sg[x]]) sg[x]++;
      |                     ^~
a.cc:22:22: error: 'g' was not declared in this scope
   22 |         for (int u : g[x]) used[sg[u]] = 0;
      |                      ^
a.cc:22:28: error: 'used' was not declared in this scope
   22 |         for (int u : g[x]) used[sg[u]] = 0;
      |                            ^~~~
a.cc:22:33: error: 'sg' was not declared in this scope
   22 |         for (int u : g[x]) used[sg[u]] = 0;
      |                                 ^~
a.cc:23:15: error: 'dg' was not declared in this scope
   23 |         if (--dg[p[x]] == 0) q.push(p[x]);
      |               ^~
a.cc:23:18: error: 'p' was not declared in this scope
   23 |         if (--dg[p[x]] == 0) q.push(p[x]);
      |                  ^
a.cc:25:12: error: expected primary-expression before 'int'
   25 |     vector<int> nxt(n, -1), prv(n, -1), upd(n, -1);
      |            ^~~
a.cc:26:12: error: expected primary-expression before 'int'
   26 |     vector<int> cycle;
      |            ^~~
a.cc:28:13: error: 'dg' was not declared in this scope
   28 |         if (dg[i] != 0) {
      |             ^~
a.cc:29:26: error: 'g' was not declared in this scope
   29 |             for (int u : g[i]) if (dg[u] == 0) used[sg[u]] = 1;
      |                          ^
a.cc:29:48: error: 'used' was not declared in this scope
   29 |             for (int u : g[i]) if (dg[u] == 0) used[sg[u]] = 1;
      |                                                ^~~~
a.cc:29:53: error: 'sg' was not declared in this scope
   29 |             for (int u : g[i]) if (dg[u] == 0) used[sg[u]] = 1;
      |                                                     ^~
a.cc:30:20: error: 'used' was not declared in this scope
   30 |             while (used[sg[i]]) sg[i]++;
      |                    ^~~~
a.cc:30:25: error: 'sg' was not declared in this scope
   30 |             while (used[sg[i]]) sg[i]++;
      |                         ^~
a.cc:31:13: error: 'upd' was not declared in this scope
   31 |             upd[i] = sg[i] + 1;
      |             ^~~
a.cc:31:22: error: 'sg' was not declared in this scope
   31 |             upd[i] = sg[i] + 1;
      |                      ^~
a.cc:32:20: error: 'used' was not declared in this scope
   32 |             while (used[upd[i]]) ++upd[i];
      |                    ^~~~
a.cc:33:26: error: 'g' was not declared in this scope
   33 |             for (int u : g[i]) if (dg[u] == 0) used[sg[u]] = 0;
      |                          ^
a.cc:33:48: error: 'used' was not declared in this scope
   33 |             for (int u : g[i]) if (dg[u] == 0) used[sg[u]] = 0;
      |                                                ^~~~
a.cc:34:26: error: 'g' was not declared in this scope
   34 |             for (int u : g[i]) if (dg[u] > 0) {
      |                          ^
a.cc:35:17: error: 'nxt' was not declared in this scope
   35 |                 nxt[i] = u;
      |                 ^~~
a.cc:36:17: error: 'prv' was not declared in this scope
   36 |                 prv[u] = i;
      |                 ^~~
a.cc:38:13: error: 'cycle' was not declared in this scope
   38 |             cycle.push_back(i);
      |             ^~~~~
a.cc: In lambda function:
a.cc:44:26: error: 'g' was not declared in this scope
   44 |             for (int u : g[i]) {
      |                          ^
a.cc:45:21: error: 'sg' was not declared in this scope
   45 |                 if (sg[u] == sg[i]) return false;
      |                     ^~
a.cc:46:17: error: 'used' was not declared in this scope
   46 |                 used[sg[u]] = 1;
      |                 ^~~~
a.cc:46:22: error: 'sg' was not declared in this scope
   46 |                 used[sg[u]] = 1;
      |                      ^~
a.cc:48:33: error: 'sg' was not declared in this scope
   48 |             for (int j = 0; j < sg[i]; ++j) {
      |                                 ^~
a.cc:49:22: error: 'used' was not declared in this scope
   49 |                 if (!used[j]) return false;
      |                      ^~~~
a.cc:51:26: error: 'g' was not declared in this scope
   51 |             for (int u : g[i]) used[sg[u]] = 0;
      |                          ^
a.cc:51:32: error: 'used' was not declared in this scope
   51 |             for (int u : g[i]) used[sg[u]] = 0;
      |                                ^~~~
a.cc:51:37: error: 'sg' was not declared in this scope
   51 |             for (int u : g[i]) used[sg[u]] = 0;
      |                                     ^~
a.cc: In function 'int main()':
a.cc:57:9: error: 'puts' was not declared in this scope
   57 |         puts("POSSIBLE");
      |         ^~~~
a.cc:60:18: error: 'sg' was not declared in this scope
   60 |     auto saved = sg;
      |                  ^~
a.cc:61:25: error: 'cycle' was not declared in this scope
   61 |     for (int j = 0; j < cycle.size(); ++j) {
      |                         ^~~~~
a.cc:63:24: error: 'upd' was not declared in this scope
   63 |         sg[cycle[j]] = upd[cycle[j]];
      |                        ^~~
a.cc:66:21: error: 'prv' was not declared in this scope
   66 |             int r = prv[x];
      |                     ^~~
a.cc:67:26: error: 'g' was not declared in this scope
   67 |             for (int k : g[r]) used[sg[k]] = 1;
      |                          ^
a.cc:67:32: error: 'used' was not declared in this scope
   67 |             for (int k : g[r]) used[sg[k]] = 1;
      |                                ^~~~
a.cc:69:20: error: 'used' was not declared in this scope
   69 |             while (used[sg[r]]) sg[r]++;
      |                    ^~~~
a.cc:71:26: error: 'g' was not declared in this scope
   71 |             for (int k : g[r]) used[sg[k]] = 0;
      |                          ^
a.cc:71:32: error: 'used' was not declared in this scope
   71 |             for (int k : g[r]) used[sg[k]] = 0;
      |                                ^~~~
a.cc:76:13: error: 'puts' was not declared in this scope
   76 |             puts("POSSIBLE");
      |             ^~~~
a.cc:80:5: error: 'puts' was not declared in this scope
   80 |     puts("IMPOSSIBLE");
      |     ^~~~