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 Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

const INF: usize = 100000000;

fn main() {
    let n: usize = read();
    let go: Vec<usize> = readnc();
    let go: Vec<usize> = go.into_iter().map(|p| p-1).collect();

    let mut graph: Vec<Vec<usize>> = vec![vec![]; n];
    let mut pa: Vec<usize> = vec![0; n];
    let mut deg: Vec<usize> = vec![0; n];
    for i in 0..n {
        deg[go[i]] += 1;
        pa[i] = go[i];
        graph[go[i]].push(i);
    }

    let mut is_loop: Vec<bool> = vec![false; n];
    let mut head = 0;
    for i in 0..n {
        head = pa[head];
    }
    for i in 0..n {
        is_loop[head] = true;
        head = pa[head];
    }

    let mut value: Vec<usize> = vec![INF; n];

    let mut que: VecDeque<usize> = VecDeque::new();
    for i in 0..n {
        if deg[i] == 0 {
            que.push_back(i);
        }
    }

    while let Some(v) = que.pop_front() {
        value[v] = compute(v, &value, &graph);
        deg[pa[v]] -= 1;
        if deg[pa[v]] == 0 {
            que.push_back(pa[v]);
        }
    }

    let v = head;
    let a = compute(v, &value, &graph);
    let mut is_ok = false;

    for vv in a..a+2 {
        for i in 0..n {
            if is_loop[i] {
                value[i] = INF;
            }
        }
        value[v] = vv;

        let mut head = v;
        loop {
            head = pa[head];
            if head == v {
                break;
            }
            value[head] = compute(head, &value, &graph);
        }
        is_ok |= verify(&value, &graph);
    }
    println!("{}", if is_ok { "POSSIBLE" } else { "IMPOSSIBLE" });
}

fn verify(value: &Vec<usize>, graph: &Vec<Vec<usize>>) -> bool {
    let n = graph.len();
    for i in 0..n {
        for &to in &graph[i] {
            if value[i] == value[to] {
                return false;
            }
        }
        if value[i] != compute(i, value, graph) {
            return false;
        }
    }
    true
}

fn compute(v: usize, value: &Vec<usize>, graph: &Vec<Vec<usize>>) -> usize {
    let n = graph.len();
    let mut set: HashSet<usize> = HashSet::new();
    for &t in &graph[v] {
        set.insert(value[t]);
    }
    let mut i = 0;
    while i < n {
        if !set.contains(&i) {
            return i;
        }
        i += 1;
    }
    panic!("ohno")
}