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: Wrong Answer)

#include<bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define ull unsigned long long
#define inf 1010000000
#define infll 1010000000000000000ll
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpr make_pair
#define fi first
#define se second
#define pq priority_queue<int>
#define pqll priority_queue<ll>
#define up(x,y) (x<(y)?x=(y):0)
#define dn(x,y) (x>(y)?x=(y):0)
#define ad(x,y) (x=(x+(y))%mod)
#define cbn(x,y) ((ll)fac[x]*inv[y]%mod*inv[(x)-(y)]%mod)
#define mod 1000000007
#define N 100009
using namespace std;

int n,cnt,sz,a[N],vis[N],bo[N],path[N],blg[N],bg[N],dgr[N];
set<int> S; pq Q; vi ans[N],f,g,b[N];
int read(); ll readll();
void dfs(int x,int para){
	if (bo[x]==para) return; vis[x]=1; bo[x]=para; S.insert(x);
	for (int p:b[x]){
		//if (min(u,v)==min(x,p) && max(u,v)==max(x,p)) continue;
		dfs(p,para);	
	}
}
bool work(int k,int len,int last,int rst,int j){
	if (k>len){
		ans[cnt].clear();
		int i; for (i=1; i<=len; i++) ans[cnt].pb(path[i]);
		return 1;
	}
	if (sz==2 && rst==1){
	//	cerr<<"!\n";
		int x=(dgr[j]?j:a[j]);
	//	cerr<<x<<'\n';
		path[k]=x; S.erase(x);
		return work(k+1,len,x,0,j);	
	}
	for (auto it=S.begin(); it!=S.end(); ) if ((*it)!=a[last]){
		path[k]=*it; S.erase(it);
		int tmp=rst;
		if ((*it)==j || (*it)==a[j]) rst=0;
		if (!(--dgr[a[*it]])) rst--;
		if (work(k+1,len,*it,rst,j)) return 1;
		S.insert(path[k]); it=S.upper_bound(path[k]);
		rst=tmp;
	} else it++;
	return 0;
}
int main(){
	scanf("%d",&n);
	int i,j;
	for (i=1; i<=n; i++){
		scanf("%d",&a[i]); b[a[i]].pb(i);
	}
	for (i=1; i<=n; i++) if (!vis[i]){
		cnt++;
		for (j=i; !vis[j]; j=a[j]) vis[j]=1;
		for (sz=0; vis[j]!=2; j=a[j]){ vis[j]=2; sz++; }
		S.clear(); dfs(j,1);
		for (int p:S) dgr[a[p]]++;
		dgr[j]--; dgr[a[j]]--;
		int tmp=0;
		for (int p:S) if (dgr[p]) tmp++;
		if (!work(1,S.size(),0,tmp,j)){ puts("-1"); return 0; }	
		
	}
	for (i=1; i<=cnt; i++) for (int p:ans[i]) blg[p]=i;
	for (i=1; i<=cnt; i++){ bg[i]=1; Q.push(-ans[i][0]); }
	while (!Q.empty()){
		int x=-Q.top();
		printf("%d ",x); Q.pop();
		x=blg[x];
		if (bg[x]<ans[x].size()) Q.push(-ans[x][bg[x]++]);
	}
	return 0;
}


int read(){
	int x=0; char ch=getchar(); bool flag=0;
	while (ch<'0' || ch>'9'){ if (ch=='-') flag=1; ch=getchar(); }
	while (ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
	return flag?-x:x;
}
ll readll(){
	ll x=0; char ch=getchar(); bool flag=0;
	while (ch<'0' || ch>'9'){ if (ch=='-') flag=1; ch=getchar(); }
	while (ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
	return flag?-x:x;
}