Language: C++

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";

int N;
int p[202020];

vector<int> g[202020];
vector<int> rg[202020];
int a[202020];

bool check(int v, int n) {
    a[v] = n;
    int w = rg[v][0];
    if (a[w] != -1) {
        if (a[w] == 2 && a[v] != 1) return false; 
        if (a[w] == 1 && a[v] == 1) return false;
        if (a[w] == 0 && a[v] == 0) return false;
        return true;
    }
    if (n == 2) return check(w, (g[w].size() > 1) ? 1 : 0);
    if (n == 1) return check(w, (g[w].size() <= 1) ? 0 : 2);
    /* n == 0 */ return g[w].size() <= 1 && check(w, 1);
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    for (int j = 0; j < N; ++j) {
        cin >> p[j]; --p[j];
        g[p[j]].push_back(j);
        rg[j].push_back(p[j]);
    }

    int start = -1;
    for (int j = 0; j < N; ++j) {
        if (g[j].size() > 0) {
            start = j;
            break;
        }
    }

    bool r = false;
    fill(a, a+N, -1);
    r = check(start, 1);
    if (!r) {
        fill(a, a+N, -1);
        r = check(start, (g[start].size() <= 1) ? 0 : 2);
    }
    cout << (r ? POSSIBLE : IMPOSSIBLE) << endl;
    return 0;
}
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";

int N;
int p[202020];

vector<int> g[202020];
vector<int> rg[202020];
int a[202020];

bool check(int v, int n) {
    a[v] = n;
    int w = rg[v][0];
    if (a[w] != -1) {
        if (a[w] == 2 && a[v] != 1) return false; 
        if (a[w] == 1 && a[v] == 1) return false;
        if (a[w] == 0 && a[v] == 0) return false;
        return true;
    }
    if (n == 2) return check(w, (g[w].size() > 1) ? 1 : 0);
    if (n == 1) return check(w, (g[w].size() <= 1) ? 0 : 2);
    /* n == 0 */ return g[w].size() <= 1 && check(w, 1);
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    for (int j = 0; j < N; ++j) {
        cin >> p[j]; --p[j];
        g[p[j]].push_back(j);
        rg[j].push_back(p[j]);
    }

    int start = -1;
    for (int j = 0; j < N; ++j) {
        if (g[j].size() > 0) {
            start = j;
            break;
        }
    }

    bool r = false;
    fill(a, a+N, -1);
    r = check(start, 1);
    if (!r) {
        fill(a, a+N, -1);
        r = check(start, (g[start].size() <= 1) ? 0 : 2);
    }
    cout << (r ? POSSIBLE : IMPOSSIBLE) << endl;
    return 0;
}
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";

int N;
int p[202020];

vector<int> g[202020];
vector<int> rg[202020];
int a[202020];

bool check(int v, int n) {
    a[v] = n;
    int w = rg[v][0];
    if (a[w] != -1) {
        if (a[w] == 2 && a[v] != 1) return false; 
        if (a[w] == 1 && a[v] == 1) return false;
        if (a[w] == 0 && a[v] == 0) return false;
        return true;
    }
    if (n == 2) return check(w, (g[w].size() > 1) ? 1 : 0);
    if (n == 1) return check(w, (g[w].size() <= 1) ? 0 : 2);
    /* n == 0 */ return g[w].size() <= 1 && check(w, 1);
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    for (int j = 0; j < N; ++j) {
        cin >> p[j]; --p[j];
        g[p[j]].push_back(j);
        rg[j].push_back(p[j]);
    }

    int start = -1;
    for (int j = 0; j < N; ++j) {
        if (g[j].size() > 0) {
            start = j;
            break;
        }
    }

    bool r = false;
    fill(a, a+N, -1);
    r = check(start, 1);
    if (!r) {
        fill(a, a+N, -1);
        r = check(start, (g[start].size() <= 1) ? 0 : 2);
    }
    cout << (r ? POSSIBLE : IMPOSSIBLE) << endl;
    return 0;
}


Compiler output:
a.cc:101:14: error: redefinition of 'const std::string POSSIBLE'
  101 | const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";
      |              ^~~~~~~~
a.cc:26:14: note: 'const std::string POSSIBLE' previously declared here
   26 | const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";
      |              ^~~~~~~~
a.cc:101:37: error: redefinition of 'const std::string IMPOSSIBLE'
  101 | const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";
      |                                     ^~~~~~~~~~
a.cc:26:37: note: 'const std::string IMPOSSIBLE' previously declared here
   26 | const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";
      |                                     ^~~~~~~~~~
a.cc:103:5: error: redefinition of 'int N'
  103 | int N;
      |     ^
a.cc:28:5: note: 'int N' previously declared here
   28 | int N;
      |     ^
a.cc:104:5: error: redefinition of 'int p [202020]'
  104 | int p[202020];
      |     ^
a.cc:29:5: note: 'int p [202020]' previously declared here
   29 | int p[202020];
      |     ^
a.cc:106:13: error: redefinition of 'std::vector<int> g [202020]'
  106 | vector<int> g[202020];
      |             ^
a.cc:31:13: note: 'std::vector<int> g [202020]' previously declared here
   31 | vector<int> g[202020];
      |             ^
a.cc:107:13: error: redefinition of 'std::vector<int> rg [202020]'
  107 | vector<int> rg[202020];
      |             ^~
a.cc:32:13: note: 'std::vector<int> rg [202020]' previously declared here
   32 | vector<int> rg[202020];
      |             ^~
a.cc:108:5: error: redefinition of 'int a [202020]'
  108 | int a[202020];
      |     ^
a.cc:33:5: note: 'int a [202020]' previously declared here
   33 | int a[202020];
      |     ^
a.cc:110:6: error: redefinition of 'bool check(int, int)'
  110 | bool check(int v, int n) {
      |      ^~~~~
a.cc:35:6: note: 'bool check(int, int)' previously defined here
   35 | bool check(int v, int n) {
      |      ^~~~~
a.cc:124:5: error: redefinition of 'int main()'
  124 | int main() {
      |     ^~~~
a.cc:49:5: note: 'int main()' previously defined here
   49 | int main() {
      |     ^~~~
a.cc:176:14: error: redefinition of 'const std::string POSSIBLE'
  176 | const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";
      |              ^~~~~~~~
a.cc:26:14: note: 'const std::string POSSIBLE' previously declared here
   26 | const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";
      |              ^~~~~~~~
a.cc:176:37: error: redefinition of 'const std::string IMPOSSIBLE'
  176 | const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";
      |                                     ^~~~~~~~~~
a.cc:26:37: note: 'const std::string IMPOSSIBLE' previously declared here
   26 | const string POSSIBLE = "POSSIBLE", IMPOSSIBLE = "IMPOSSIBLE";
      |                                     ^~~~~~~~~~
a.cc:178:5: error: redefinition of 'int N'
  178 | int N;
      |     ^
a.cc:28:5: note: 'int N' previously declared here
   28 | int N;
      |     ^
a.cc:179:5: error: redefinition of 'int p [202020]'
  179 | int p[202020];
      |     ^
a.cc:29:5: note: 'int p [202020]' previously declared here
   29 | int p[202020];
      |     ^
a.cc:181:13: error: redefinition of 'std::vector<int> g [202020]'
  181 | vector<int> g[202020];
      |             ^
a.cc:31:13: note: 'std::vector<int> g [202020]' previously declared here
   31 | vector<int> g[202020];
      |             ^
a.cc:182:13: error: redefinition of 'std::vector<int> rg [202020]'
  182 | vector<int> rg[202020];
      |             ^~
a.cc:32:13: note: 'std::vector<int> rg [202020]' previously declared here
   32 | vector<int> rg[202020];
      |             ^~
a.cc:183:5: error: redefinition of 'int a [202020]'
  183 | int a[202020];
      |     ^
a.cc:33:5: note: 'int a [202020]' previously declared here
   33 | int a[202020];
      |     ^
a.cc:185:6: error: redefinition of 'bool check(int, int)'
  185 | bool check(int v, int n) {
      |      ^~~~~
a.cc:35:6: note: 'bool check(int, int)' previously defined here
   35 | bool check(int v, int n) {
      |      ^~~~~
a.cc:199:5: error: redefinition of 'int main()'
  199 | int main() {
      |     ^~~~
a.cc:49:5: note: 'int main()' previously defined here
   49 | int main() {
      |     ^~~~