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: Accepted)

#include<bits/stdc++.h>
#define F(i,a,b) for(int i=(a);i<=(b);++i)
#define F2(i,a,b) for(int i=(a);i<(b);++i)
#define dF(i,a,b) for(int i=(a);i>=(b);--i)
#define dF2(i,a,b) for(int i=(a);i>(b);--i)
#define dF3(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define eF(i,u) for(int i=h[u];i;i=nxt[i])
using namespace std;typedef long long ll;typedef double ld;int INF=0x3f3f3f3f;int INF2=0x7fffffff;ll LNF=0x3f3f3f3f3f3f3f3f;ll LNF2=0x7fffffffffffffff;

int n;
int p[200001],out[200001];

int h[200001],nxt[200005],to[200005],tot;
void ins(int x,int y){nxt[++tot]=h[x];to[tot]=y;h[x]=tot;}

int que[200001],l=1,r;
int a[200001],k[200001];

int s;

bool check(){
	int now=p[s];
	for(;now!=s;now=p[now]){
		eF(i,now) k[a[to[i]]]=1;
		for(int i=0;;++i) if(!k[i]) {a[now]=i; break;}
		eF(i,now) k[a[to[i]]]=0;
	}
	bool ok=1;
	eF(i,s) k[a[to[i]]]=1;
	for(int i=0;;++i) if(!k[i]) {if(a[s]!=i) ok=0; break;}
	eF(i,s) k[a[to[i]]]=0;
	return ok;
}

int main(){
	scanf("%d",&n);
	F(i,1,n) scanf("%d",p+i), ins(p[i],i), ++out[p[i]];
	F(i,1,n) if(!out[i]) que[++r]=i;
	F(i,1,n) a[i]=-1;
	while(l<=r){
		int u=que[l++];
		eF(i,u) k[a[to[i]]]=1;
		for(int i=0;;++i) if(!k[i]) {a[u]=i; break;}
		eF(i,u) k[a[to[i]]]=0;
		if((--out[p[u]])==0) que[++r]=p[u];
	}
	F(i,1,n) if(a[i]==-1) s=i;
	eF(i,s) k[a[to[i]]]=1;
	for(int i=0;;++i) if(!k[i]) {a[s]=i; break;}
	eF(i,s) k[a[to[i]]]=0;
	if(check()) {puts("POSSIBLE"); return 0;}
	k[a[s]]=1;
	eF(i,s) k[a[to[i]]]=1;
	for(int i=0;;++i) if(!k[i]) {a[s]=i; break;}
	eF(i,s) k[a[to[i]]]=0;
	if(check()) {puts("POSSIBLE"); return 0;}
	puts("IMPOSSIBLE");
	return 0;
}