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: Runtime Error)

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int N=100005;
int n,a[N],ans[N],vis[N];
pii t[N*4];
set<int> S;
void build(int k,int l,int r){
	if (l==r){
		t[k]=pii(0,l);
		return;
	}
	int mid=(l+r)/2;
	build(k*2,l,mid);
	build(k*2+1,mid+1,r);
	t[k]=max(t[k*2],t[k*2+1]);
}
void change(int k,int l,int r,int p,int v){
	if (l==r){
		t[k].fi+=v;
		return;
	}
	int mid=(l+r)/2;
	if (p<=mid) change(k*2,l,mid,p,v);
	else change(k*2+1,mid+1,r,p,v);
	t[k]=max(t[k*2],t[k*2+1]);
}
void erase(int x){
	change(1,1,n,a[x],-1);
	change(1,1,n,x,-(1<<30));
}
void insert(int x){
	change(1,1,n,a[x],1);
	change(1,1,n,x,(1<<30));
}
bool ava(int ban,int l){
	if (!l) return 1;
	int id=t[1].se;
	//printf("%d %d\n", ans[1],t[1].se);
	if (t[1].fi<l-1) return 1;
	if (id==ban){
		int p=id,fl=1;
		change(1,1,n,p,-1);
		id=t[1].se;
		if (t[1].fi!=l-1) fl=0;
		change(1,1,n,p,1);
		if (!fl) return 0;
	}
	if (a[a[id]]==id&&l==2) return 0;
	return 1;
}
int main(){
	scanf("%d",&n);
	For(i,1,n) scanf("%d",&a[i]);
	For(i,1,n) S.insert(i);
	build(1,1,n);
	For(i,1,n) insert(i);
	For(i,1,n) vis[i]=1;
	if (!ava(0,n)) return puts("-1"),0;
	For(i,1,n){
		set<int>::iterator it;
		it=S.begin();
		for (;it!=S.end();it++){
			if (*it==a[ans[i-1]]) continue;
			erase(*it);
			ans[i]=*it;
			if (ava(a[*it],n-i)) break;
			insert(*it);
			//cout<<*it<<endl;
		}
		assert(it!=S.end());
		ans[i]=*it;
		S.erase(it);
		vis[ans[i]]=0;
		//printf("%d\n",ans[i]);
	}
	For(i,1,n) printf("%d ",ans[i]);
} 
/*
illegal:
存在一个元素,出现次数==剩余元素个数-1,且第一个位置不能放
否则均合法 
*/