Problem p02809 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

typedef long long ll;
typedef long double ld;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;

#define LLINF ((long long)1e18) // 1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (100006)
ll n, A[MAXN], cnt[MAXN];
set<int> s[MAXN];
set<int> av;
int main() {
  FAST cin >> n;
  FOR(i, 1, n) cin >> A[i], ++cnt[A[i]];
  if (n == 2) {
    cout << "-1\n";
    return 0;
  }
  FOR(i, 1, n) s[cnt[i]].ins(i), av.ins(i);
  vector<ll> v;
  while (n - siz(v) > 3) {
    ll V = n - siz(v);
    ll tp = -1;
    assert(siz(s[V]) == 0);
    if (siz(s[V - 1])) {
      tp = *s[V - 1].begin();
    } else {
      if (v.empty() || A[v.back()] != *av.begin()) {
        tp = *av.begin();
      } else {
        tp = *++av.begin();
      }
    }
    assert(~tp);
    v.pb(tp);
    av.erase(tp);
    s[cnt[tp]].ins(tp);
    if (av.find(A[tp]) != av.end()) {
      s[cnt[A[tp]]].erase(A[tp]);
      --cnt[A[tp]];
      s[cnt[A[tp]]].ins(A[tp]);
    }
  }
  assert(n - siz(v) == 3);
  /* next permutation here */
  vector<ll> hey = {*av.begin(), *++av.begin(), *++ ++av.begin()};
  vector<ll> lol = {0, 1, 2};
  do {
    v.pb(hey[lol[0]]), v.pb(hey[lol[1]]), v.pb(hey[lol[2]]);
    bool fk = 1;
    FOR(i, max(0ll, n - 4), n - 2) if (A[v[i]] == v[i + 1]) { fk = 0; }
    if (fk)
      break;
    v.pop_back(), v.pop_back(), v.pop_back();
  } while (next_permutation(all(lol)));
  /* */
  assert(v.size() == n);
  for (auto i : v)
    cout << i << ' ';
  cout << '\n';
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

typedef long long ll;
typedef long double ld;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;

#define LLINF ((long long)1e18) // 1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (100006)
ll n, A[MAXN], cnt[MAXN];
set<int> s[MAXN];
set<int> av;
int main() {
  FAST cin >> n;
  FOR(i, 1, n) cin >> A[i], ++cnt[A[i]];
  if (n == 2) {
    cout << "-1\n";
    return 0;
  }
  FOR(i, 1, n) s[cnt[i]].ins(i), av.ins(i);
  vector<ll> v;
  while (n - siz(v) > 3) {
    ll V = n - siz(v);
    ll tp = -1;
    assert(siz(s[V]) == 0);
    if (siz(s[V - 1])) {
      tp = *s[V - 1].begin();
    } else {
      if (v.empty() || A[v.back()] != *av.begin()) {
        tp = *av.begin();
      } else {
        tp = *++av.begin();
      }
    }
    assert(~tp);
    v.pb(tp);
    av.erase(tp);
    s[cnt[tp]].erase(tp);
    if (av.find(A[tp]) != av.end()) {
      s[cnt[A[tp]]].erase(A[tp]);
      --cnt[A[tp]];
      s[cnt[A[tp]]].ins(A[tp]);
    }
  }
  assert(n - siz(v) == 3);
  /* next permutation here */
  vector<ll> hey = {*av.begin(), *++av.begin(), *++ ++av.begin()};
  vector<ll> lol = {0, 1, 2};
  do {
    v.pb(hey[lol[0]]), v.pb(hey[lol[1]]), v.pb(hey[lol[2]]);
    bool fk = 1;
    FOR(i, max(0ll, n - 4), n - 2) if (A[v[i]] == v[i + 1]) { fk = 0; }
    if (fk)
      break;
    v.pop_back(), v.pop_back(), v.pop_back();
  } while (next_permutation(all(lol)));
  /* */
  assert(v.size() == n);
  for (auto i : v)
    cout << i << ' ';
  cout << '\n';
}