Language: C++

#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

int n;

int main() {
  cin >> n;
  vector<int> cnt(n);
  vector<int> a(n);
  range(i, 0, n) {
    cin >> a[i];
    a[i]--;
    cnt[a[i]]++;
  }
  vector<int> pos;
  range(i, 0, n) if(cnt[i] == n - 1) {
    pos.push_back(i);
  }
  if(pos.size() > 1) {
    cout << -1;
    return 0;
  }
  if(!pos.empty()) {
    vector<int> res = {pos[0]};
    range(i, 0, n) if(i != pos[0]) res.push_back(i);
    if(a[res[0]] == res[1]) swap(res[1], res[2]);
    for(int i : res) cout << i + 1 << " ";
    return 0;
  }
  set<int> cur;
  vector<int> res;
  range(i, 0, n) cur.insert(i);
  while(cur.size() > 1) {
    auto it = cur.begin();
    if(!res.empty() && a[res.back()] == *it) it = next(it);
    res.push_back(*it);
    cur.erase(it);
  }
  int c = *cur.begin();
  cur.erase(c);
  while(res.size()) {
    if(a[res.back()] == c) {
      cur.insert(res.back());
      res.pop_back();
    }
    else break;
  }
  if(cur.size() == 1 && a[c] == *cur.begin()) {
    res.push_back(*cur.begin());
    vector<int> tt = res;
    int pos = -1;
    husk(i, res.size() - 3, -2) {
      if(i == -1 || a[res[i]] != c) {
        tt.insert(tt.begin() + i + 1, c);
        pos = i;
        break;
      }
    }
    int z = res[res.size() - 2];
    res.erase(res.back() + res.size() - 2);
    husk(i, res.size() - 4, pos - 1) {
      if(i == -1 || a[res[i]] != z) {
        res.insert(res.begin() + i + 1, z);
        res.insert(res.begin() + i + 2, c);
        res = min(res, tt);
        break;
      }
    }
  } else {
    res.push_back(c);
    while(cur.size()) {
      int c = *cur.begin();
      if(a[res.back()] == c) c = *next(cur.begin());
      cur.erase(c);
      res.push_back(c);
    }
  }
  for(int i : res) cout << i + 1 << " ";
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:64:14: error: no matching function for call to 'std::vector<int>::erase(std::vector<int>::size_type)'
   64 |     res.erase(res.back() + res.size() - 2);
      |     ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1536:7: note: candidate: 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(const_iterator) [with _Tp = int; _Alloc = std::allocator<int>; iterator = std::vector<int>::iterator; const_iterator = std::vector<int>::const_iterator]'
 1536 |       erase(const_iterator __position)
      |       ^~~~~
/usr/include/c++/14/bits/stl_vector.h:1536:28: note:   no known conversion for argument 1 from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'std::vector<int>::const_iterator'
 1536 |       erase(const_iterator __position)
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1564:7: note: candidate: 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(const_iterator, const_iterator) [with _Tp = int; _Alloc = std::allocator<int>; iterator = std::vector<int>::iterator; const_iterator = std::vector<int>::const_iterator]'
 1564 |       erase(const_iterator __first, const_iterator __last)
      |       ^~~~~
/usr/include/c++/14/bits/stl_vector.h:1564:7: note:   candidate expects 2 arguments, 1 provided