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 <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;

int n, sn;
vector <int> par[100010];
int ban[100010], num[100010];
int st[100010];

vector <int> c;
vector <int> mp[100010];

vector <int> ans;
bool flag[100010];
vector <int> waste;

vector <int> cand;

void dfs (int x) {
	// cerr << "in : " << x << " " << ans.size() << endl;
	// cerr << "mark : " << x << " : " << n - sn << endl << "ans: ";
	// for (int i = 0; i < ans.size(); i ++)
		// cerr << ans[i] << " ";
	// cerr << endl << "waste: ";
	// for (int i = 0; i < waste.size(); i ++)
		// cerr << waste[i] << " ";
	// cerr << endl;
	if (x == n + 1) {
		// cerr << "done " << endl;
		for (int i = 0; i < n; i ++)
			cout << ans[i] << " ";
		cout << endl;
		exit(0);
	}
	if (x == 1) {
		for (int i = 1; i <= n; i ++) {
			ans.push_back(i);
			if (i != 1)	waste.push_back (1);
			flag[i] = 1;
			dfs (x + 1);
			flag[i] = 0;
			if (i != 1)	waste.pop_back ();
			ans.pop_back();
		}
		return ;
	}
	int last = ans[ans.size() - 1];
	if (x == n - sn) {
		cand.resize(0);
		for (int i = 1; i <= n; i ++)
			if (!flag[i])
				cand.push_back(i);
	}
	if (x >= n - sn) {
		for (int i = 0; i < cand.size(); i ++)
			if (cand[i] != -1)
				if (ban[last] != cand[i]) {
					ans.push_back(cand[i]);
					int tmp = cand[i];
					cand[i] = -1;
					dfs (x + 1);
					cand[i] = tmp;
					ans.pop_back();
				}
		return ;
	}
	int cnt = 0;
	int tg = 0;
	for (int i = 0; i < c.size(); i ++) {
		if (flag[c[i]])
			continue ;
		if (num[c[i]] == n - 2) {
			if (mp[last][i] != 0) {
				return ;
			}
			cnt ++;
			tg = c[i];
		}
	}
	if (cnt > 1)
		return ;
	if (cnt == 1) {
		ans.push_back(tg);
		waste.push_back(x);
		flag[tg] = 1;
		dfs (x + 1);
		ans.pop_back();
		waste.pop_back();
		flag[tg] = 0;
		return ;
	}
	
	sort (waste.begin(), waste.end());
	for (int i = 0; i < waste.size(); i ++) {
		int cur = waste[i];
		if (flag[cur])
			continue ;
		if (ban[last] == cur)
			continue ;
		//select c
		flag[cur] = 1;
		waste[i] = x;
		for (int i = 0; i < c.size(); i ++)
			if (mp[last][i] == 0) {
				mp[last][i] = 1;
				num[c[i]] ++;
			}
		ans.push_back(cur);
		dfs (x + 1);
		ans.pop_back();
		for (int i = 0; i < c.size(); i ++)
			if (mp[last][i] == 1) {
				mp[last][i] = 0;
				num[c[i]] --;
			}
		waste[i] = cur;
		flag[cur] = 0;
	}
	for (int t = x; t <= n; t ++) {
		if (ban[last] == t)
			continue ;
		if (flag[t])
			continue ;
		flag[t] = 1;
		if (t != x)
			waste.push_back (x);
		for (int i = 0; i < c.size(); i ++)
			if (mp[last][i] == 0) {
				mp[last][i] = 1;
				num[c[i]] ++;
			}
		ans.push_back(t);
		dfs (x + 1);
		ans.pop_back();
		for (int i = 0; i < c.size(); i ++)
			if (mp[last][i] == 1) {
				mp[last][i] = 0;
				num[c[i]] --;
			}
		if (t != x)
			waste.pop_back();
		flag[t] = 0;
	}
}


int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	sn = pow(n, 0.6667);
	for (int i = 1; i <= n; i ++) {
		cin >> ban[i];
		num[ban[i]] ++;
		par[ban[i]].push_back(i);
	}

	for (int i = 1; i <= n; i ++) {
		if (num[i] >= sn) {
			c.push_back(i);
			for (int j = 1; j <= n; j ++)
				mp[j].push_back(0);
			for (int j = 0; j < par[i].size(); j ++)
				mp[par[i][j]][mp[1].size() - 1] = -1;
		}
		if (num[i] == n - 1) {
			cout << -1 << endl;
			return 0;
		}
	}

	dfs (1);


	return 0;
}