Problem:
Score : 800 points

Problem Statement

There is a directed graph with N vertices and N edges. The vertices are numbered 1, 2, ..., N.

The graph has the following N edges: (p_1, 1), (p_2, 2), ..., (p_N, N), and the graph is weakly connected. Here, an edge from Vertex u to Vertex v is denoted by (u, v), and a weakly connected graph is a graph which would be connected if each edge was bidirectional.

We would like to assign a value to each of the vertices in this graph so that the following conditions are satisfied. Here, a_i is the value assigned to Vertex i.

Each a_i is a non-negative integer.

For each edge (i, j), a_i \neq a_j holds.

For each i and each integer x(0 ≤ x < a_i), there exists a vertex j such that the edge (i, j) exists and x = a_j holds.

Determine whether there exists such an assignment.

Constraints

2 ≤ N ≤ 200 000

1 ≤ p_i ≤ N

p_i \neq i

The graph is weakly connected.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

If the assignment is possible, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

4
2 3 4 1

Sample Output 1

POSSIBLE

The assignment is possible: {a_i} = {0, 1, 0, 1} or {a_i} = {1, 0, 1, 0}.

Sample Input 2

3
2 3 1

Sample Output 2

IMPOSSIBLE

Sample Input 3

4
2 3 1 1

Sample Output 3

POSSIBLE

The assignment is possible: {a_i} = {2, 0, 1, 0}.

Sample Input 4

6
4 5 6 5 6 4

Sample Output 4

IMPOSSIBLE

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define scanf(...) scanf(__VA_ARGS__)?:0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair <PLL, int> PLLI;
typedef pair <PLL, PLL> PP;
typedef pair <PII, int> PPI;
typedef pair <ll, int> PLI;
typedef unsigned int ui;
const int inf = 1e9+9;
const int mod = 924844033;
#define maxn 200100
int tab[maxn], val[maxn];
int ile[30][maxn];
vector <int> cykl;
bool odw[maxn];
vector <int> graf[maxn];

int findNim(int x, int par) {
	int st = 0;
	while (ile[st][x]) ++st;
	if (par) ++st;
	while (ile[st][x]) ++st;
	return st;
}

void dfs(int x, int start)
{
	for (auto u : graf[x])
	{
		dfs(u, start);
		ile[val[u]][x]++;
	}
	if (x != start) val[x] = findNim(x, 0);
}
int n;
bool check()
{
	FOR(i, 1, n)
		if (val[i] == val[tab[i]]) return 0;
	return 1;
}

int main()
{
	boost;
	cin >> n;
	FOR(i, 1, n) cin >> tab[i];
	int gdzie = 1;
	FOR(i, 1, n) gdzie = tab[gdzie];
	while (!odw[gdzie])
	{
		cykl.pb(gdzie);
		odw[gdzie] = 1;
		gdzie = tab[gdzie];
	}
	
	FOR(i, 1, n)
		if (!(odw[i] && odw[tab[i]])) graf[tab[i]].pb(i);
	
	for (auto u : cykl) dfs(u, u);
	//determined everything that is not on the cycle
	int sz = (int)cykl.size(), akt = cykl[sz - 1];
	val[akt] = findNim(akt, 0);
	ile[val[akt]][cykl[0]]++;
	for (int i=0; i<sz-1; ++i)
	{
		val[cykl[i]] = findNim(cykl[i], 0);
		ile[val[cykl[i]]][cykl[i + 1]]++;
	}

	if (check()) OUT("POSSIBLE");
	
	FOR(j, 0, 29)
		FOR(i, 1, n) ile[j][i] = 0;
	FOR(i, 1, n) val[i] = 0;
	
	for (auto u : cykl) dfs(u, u);
	sz = (int)cykl.size(), akt = cykl[sz - 1];
	val[akt] = findNim(akt, 1);
	ile[val[akt]][cykl[0]]++;
	for (int i=0; i<sz-1; ++i)
	{
		val[cykl[i]] = findNim(cykl[i], 0);
		ile[val[cykl[i]]][cykl[i + 1]]++;
	}
	if (check()) OUT("POSSIBLE");
	cout << "IMPOSSIBLE";
}