Problem p02809 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n;
  cin >> n;
  if (n == 2) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> a(n + 1);
  vector<int> c(n + 1);
  vector<int> pre(n + 1);
  rep(i, n) {
    cin >> a[i + 1];
    c[a[i + 1]]++;
    pre[c[a[i + 1]]] = c[a[i + 1]];
  }
  set<pair<int, int>, greater<pair<int, int>>> s;
  set<int> st;
  rep(i, n) {
    s.insert(MP(c[i + 1], i + 1));
    st.insert(i + 1);
  }
  vector<int> res;
  for (int i = 0; i < n - 3; i++) {
    auto x = *s.begin();
    int k = x.first;
    int z = x.second;
    if (k == n - i - 1) {
      res.push_back(z);
      s.erase(x);
      st.erase(z);
      int y = a[z];
      if (st.count(y)) {
        s.erase(MP(pre[y], y));
        pre[y]--;
        s.insert(MP(pre[y], y));
      }
    } else {
      int p = 0;
      if (i != 0)
        p = a[res[i - 1]];
      auto z = st.begin();
      int nx;
      if (p != (*z)) {
        nx = *z;
      } else {
        z++;
        nx = *z;
      }
      res.push_back(nx);
      st.erase(nx);
      s.erase(MP(pre[nx], nx));
      int y = a[nx];
      if (st.count(y)) {
        s.erase(MP(pre[y], y));
        pre[y]--;
        s.insert(MP(pre[y], y));
      }
    }
  }
  // for(auto x:res){
  //     cerr << x << " ";
  // }
  // cerr << endl;
  vector<int> v;
  for (auto x : st) {
    v.push_back(x);
  }
  int ng;
  if (n > 3)
    ng = a[res[n - 4]];
  else
    ng = 0;
  do {
    if (v[0] == ng)
      continue;
    if (v[1] == a[v[0]])
      continue;
    if (v[2] == a[v[1]])
      continue;
    res.push_back(v[0]);
    res.push_back(v[1]);
    res.push_back(v[2]);
    break;
  } while (next_permutation(v.begin(), v.end()));
  rep(i, n - 1) {
    if (a[res[i]] == res[i + 1])
      assert(0);
  }
  for (auto x : res) {

    cout << x << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n;
  cin >> n;
  if (n == 2) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> a(n + 1);
  vector<int> c(n + 1);
  vector<int> pre(n + 1);
  rep(i, n) {
    cin >> a[i + 1];
    c[a[i + 1]]++;
    pre[a[i + 1]] = c[a[i + 1]];
  }
  set<pair<int, int>, greater<pair<int, int>>> s;
  set<int> st;
  rep(i, n) {
    s.insert(MP(c[i + 1], i + 1));
    st.insert(i + 1);
  }
  vector<int> res;
  for (int i = 0; i < n - 3; i++) {
    auto x = *s.begin();
    int k = x.first;
    int z = x.second;
    if (k == n - i - 1) {
      res.push_back(z);
      s.erase(x);
      st.erase(z);
      int y = a[z];
      if (st.count(y)) {
        s.erase(MP(pre[y], y));
        pre[y]--;
        s.insert(MP(pre[y], y));
      }
    } else {
      int p = 0;
      if (i != 0)
        p = a[res[i - 1]];
      auto z = st.begin();
      int nx;
      if (p != (*z)) {
        nx = *z;
      } else {
        z++;
        nx = *z;
      }
      res.push_back(nx);
      st.erase(nx);
      s.erase(MP(pre[nx], nx));
      int y = a[nx];
      if (st.count(y)) {
        s.erase(MP(pre[y], y));
        pre[y]--;
        s.insert(MP(pre[y], y));
      }
    }
  }
  // for(auto x:res){
  //     cerr << x << " ";
  // }
  // cerr << endl;
  vector<int> v;
  for (auto x : st) {
    v.push_back(x);
  }
  int ng;
  if (n > 3)
    ng = a[res[n - 4]];
  else
    ng = 0;
  do {
    if (v[0] == ng)
      continue;
    if (v[1] == a[v[0]])
      continue;
    if (v[2] == a[v[1]])
      continue;
    res.push_back(v[0]);
    res.push_back(v[1]);
    res.push_back(v[2]);
    break;
  } while (next_permutation(v.begin(), v.end()));
  rep(i, n - 1) {
    if (a[res[i]] == res[i + 1])
      assert(0);
  }
  for (auto x : res) {

    cout << x << " ";
  }
  cout << endl;
  return 0;
}