Language: C++

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

const int MAXN = 200000 + 1000;

int n;
vector<int> edg[MAXN], redg[MAXN];
vector<int> cir;
bool onc[MAXN], vis[MAXN];
int val[MAXN];

int find_circle(int x)
{
	vis[x] = true;
	for(int i = 0; i < redg[x].size(); i++)
	{
		int y = redg[x][i], tmp = vis[y] ? y : find_circle(y);
		if(tmp)
			cir.push_back(x), onc[x] = true;
		return x == tmp ? 0 : tmp;
	}
	return 0;
}

bool hash[MAXN];
void assign_tree(int x)
{
	for(int i = 0; i < edg[x].size(); i++)
		if(!onc[edg[x][i]])
		{
			int y = edg[x][i];
			assign_tree(y);
			hash[val[y]] = true;
		}
	val[x] = 0;
	while(hash[val[x]])
		val[x]++;
	for(int i = 0; i < edg[x].size(); i++)
		if(!onc[edg[x][i]])
			hash[val[edg[x][i]]] = false;
}

void yes()
{
	cout << "POSSIBLE" << endl;
	exit(0);
}

void no()
{
	cout << "IMPOSSIBLE" << endl;
	exit(0);
}

void assign_circle()
{
	for(int i = (int)cir.size() - 1; i >= 0; i--)
	{
		int x = cir[i];
		for(int j = 0; j < edg[x].size(); j++)
			hash[val[edg[x][j]]] = true;
		val[x] = 0;
		while(hash[val[x]])
			val[x]++;
		for(int j = 0; j < edg[x].size(); j++)
			hash[val[edg[x][j]]] = false;
	}
}

void work()
{
	int cir0 = cir[0];
	for(int i = 0; i < edg[cir0].size(); i++)
		if(!onc[edg[cir0][i]])
			hash[val[edg[cir0][i]]] = true;
	int tmp1 = 0, tmp2 = 0;
	while(hash[tmp1])
		tmp1++;
	tmp2 = tmp1 + 1;
	while(hash[tmp2])
		tmp2++;
	for(int i = 0; i < edg[cir0].size(); i++)
		if(!onc[edg[cir0][i]])
			hash[val[edg[cir0][i]]] = false;

	val[cir0] = tmp1;
	assign_circle();
	// for(int i = 1; i <= n; i++)
	// 	cout << val[i] << endl;
	if(val[cir0] == tmp1)
		yes();
	val[cir0] = tmp2;
	assign_circle();
	if(val[cir0] == tmp2)
		yes();
	no();
}

int main()
{
	ios::sync_with_stdio(false);
	// freopen("1.in", "r", stdin);
	// freopen("1.out", "w", stdout);

	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		int x;
		cin >> x;
		edg[x].push_back(i), redg[i].push_back(x);
	}
	find_circle(1);

	// for(int i = 0; i < cir.size(); i++)
	// 	cerr << cir[i] << endl;

	for(int i = 0; i < cir.size(); i++)
		assign_tree(cir[i]);

	work();


	return 0;
}


Compiler output:
a.cc: In function 'void assign_tree(int)':
a.cc:42:25: error: reference to 'hash' is ambiguous
   42 |                         hash[val[y]] = true;
      |                         ^~~~
In file included from /usr/include/c++/14/string_view:50,
                 from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc:45:15: error: reference to 'hash' is ambiguous
   45 |         while(hash[val[x]])
      |               ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc:49:25: error: reference to 'hash' is ambiguous
   49 |                         hash[val[edg[x][i]]] = false;
      |                         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc: In function 'void assign_circle()':
a.cc:70:25: error: reference to 'hash' is ambiguous
   70 |                         hash[val[edg[x][j]]] = true;
      |                         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc:72:23: error: reference to 'hash' is ambiguous
   72 |                 while(hash[val[x]])
      |                       ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc:75:25: error: reference to 'hash' is ambiguous
   75 |                         hash[val[edg[x][j]]] = false;
      |                         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc: In function 'void work()':
a.cc:84:25: error: reference to 'hash' is ambiguous
   84 |                         hash[val[edg[cir0][i]]] = true;
      |                         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc:86:15: error: reference to 'hash' is ambiguous
   86 |         while(hash[tmp1])
      |               ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc:89:15: error: reference to 'hash' is ambiguous
   89 |         while(hash[tmp2])
      |               ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~
a.cc:93:25: error: reference to 'hash' is ambiguous
   93 |                         hash[val[edg[cir0][i]]] = false;
      |                         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:34:6: note:                 'bool hash [201000]'
   34 | bool hash[MAXN];
      |      ^~~~