Problem:
Score : 800 points

Problem Statement

Niwango has N cards, numbered 1,2,\ldots,N.
He will now arrange these cards in a row.

Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below.
To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.

To the immediate right of Card 1 (if any) is NOT Card a_1.

To the immediate right of Card 2 (if any) is NOT Card a_2.

\vdots

To the immediate right of Card N (if any) is NOT Card a_N.

Constraints

2 \leq N \leq 10^{5}

1 \leq a_i \leq N

a_i \neq i

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

If no arrangements satisfy the conditions, print -1. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:

b_1 b_2 \ldots b_N

Here, b_i represents the i-th card from the left.

Sample Input 1

4
2 3 4 1

Sample Output 1

1 3 2 4

The arrangement (1,2,3,4) is lexicographically smaller than (1,3,2,4), but is invalid, since it violates the condition "to the immediate right of Card 1 is not Card 2."

Sample Input 2

2
2 1

Sample Output 2

-1

If no arrangements satisfy the conditions, print -1.

Sample Input 3

13
2 3 4 5 6 7 8 9 10 11 12 13 12

Sample Output 3

1 3 2 4 6 5 7 9 8 10 12 11 13

Solution in Rust (1.42.0) (judge verdict: Wrong Answer)

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();
    let forbid = (0..n).map(|_| sc.read::<usize>() - 1).collect::<Vec<_>>();

    if n == 2 {
        println!("-1");
        return;
    }

    let mut q = (0..n).rev().collect::<Vec<_>>();

    let mut ans = vec![];
    while q.len() > 3 {
        let first = q.pop().unwrap();
        if ans.len() > 0 {
            let last = ans[ans.len() - 1];
            if forbid[last] == first {
                let second = q.pop().unwrap();
                ans.push(second);
                q.push(first);
            } else {
                ans.push(first);
            }
        } else {
            ans.push(first);
        }
    }

    assert_eq!(q.len(), 3);
    let mut candidate = vec![
        (q[0], q[1], q[2]),
        (q[0], q[2], q[1]),
        (q[1], q[0], q[2]),
        (q[1], q[2], q[0]),
        (q[2], q[1], q[0]),
        (q[2], q[0], q[1]),
    ];
    candidate.sort();
    for (a1, a2, a3) in candidate.into_iter() {
        if ans.len() > 0 {
            let last = ans[ans.len() - 1];
            if forbid[last] == a1 {
                continue;
            }
        }

        if forbid[a1] == a2 || forbid[a2] == a3 {
            continue;
        }
        ans.push(a1);
        ans.push(a2);
        ans.push(a3);
        break;
    }

    // assert_eq!(ans.len(), n);
    // for i in 0..n {
    //     if i > 0 {
    //         sc.write(" ");
    //     }
    //     sc.write(ans[i] + 1);
    // }
    // sc.write("\n");
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}