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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)
#define TRACE(x) cerr << #x << " = " << x << endl

typedef long long ll;
typedef pair<int, int> point;

const int mod = 1e9 + 7;

int add(int x, int y) {x += y; if(x >= mod) return x - mod; return x;}
int sub(int x, int y) {x -= y; if(x < 0) return x + mod; return x;}
int mul(int x, int y) {return (ll) x * y % mod;}

const int MAXN = 1e5 + 5;

int n, a[MAXN], deg[MAXN];
set<point> degrees;
set<int> S;

bool check(vector <int> &v){
  REP(i, n - 1)
    if(a[ v[i] ] == v[i + 1])
      return false;
  return true;
}

void permutiraj(vector <int> &v){
  vector <int> tmp;
  FOR(i, n - 3, n)
    tmp.pb(v[i]);

  REP(i, 3)
    v.pop_back();

  vector <int> ret;
  do{
    REP(i, 3)
      v.pb(tmp[i]);

    if(check(v)){
      if(ret.empty())
        ret = v;
      else
        ret = min(ret, v);
    }

    REP(i, 3)
      v.pop_back();

  } while(next_permutation(tmp.begin(), tmp.end()));
  v = ret;
}

void ubaci(int id){
  S.erase(id);
  if(degrees.find( point(deg[a[id]], a[id]) ) != degrees.end()){
    degrees.erase(point(deg[a[id]], a[id]));
    deg[ a[id] ] --;
    degrees.insert(point(deg[a[id]], a[id]));
  }
  if(degrees.find( point(deg[id], id) ) != degrees.end())
    degrees.erase( point(deg[id], id) );
}

int main(){
  ios_base::sync_with_stdio(false); cin.tie(0);

  cin >> n;
  FOR(i, 1, n + 1){
    S.insert(i);
    cin >> a[i];
    deg[a[i]] ++;
  }

  if(n == 2){
    if(a[1] == 2 && a[2] == 1)
      cout << -1;
    else{
      if(a[1] == 1 && a[2] == 1)
        cout << 1 _ 2;
      else
        cout << 2 _ 1;
    }
    return 0;
  }

  FOR(i, 1, n + 1){
    degrees.insert(point(deg[i], i));
  }
  vector <int> sol;
  REP(i, n){
    set<int> :: iterator it = S.begin();

    if(degrees.rbegin() -> first == sz(S) - 1 && sz(S) > 2){
      int id = degrees.rbegin() -> second;
      sol.pb(id);
      ubaci(id);
    }
    else{
      int num1 = *it, num2 = *it;
      if(sz(S) > 1){
        it ++;
        num2 = *it;
      }
      int id = MAXN - 1;
      if(i != 0)
        id = sol.back();

      if(a[id] == num1){
        sol.pb(num2);
        ubaci(num2);
      }
      else{
        sol.pb(num1);
        ubaci(num1);
      }
    }
  }

  if(!check(sol))
    permutiraj(sol);

  for(auto it : sol)
    cout << it << " ";
}