Problem p02809 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define XX first
#define YY second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define REP rep
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD = 1e9 + 7;

#define rall(X) (X).rbegin(), (X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}

int N;
vector<int> as;
int cnt[114514];
set<int> ls[114514];

signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  cin >> N;
  rep(i, N) {
    int a;
    cin >> a;
    --a;
    as.eb(a);
    cnt[a]++;
  }

  if (N == 2) {
    cout << "-1\n";
    return 0;
  }

  set<int> cand;
  rep(i, N) {
    cand.insert(i);
    ls[cnt[i]].insert(i);
  }

  vector<int> ans;
  auto Del = [&](int idx) {
    ans.eb(idx);
    cand.erase(idx);
    assert(ls[cnt[idx]].count(idx));
    ls[cnt[idx]].erase(idx);
    int a = as[idx];
    if (cand.count(a)) {
      assert(ls[cnt[a]].count(a));
      ls[cnt[a]].erase(a);
      cnt[a]--;
      assert(cnt[a] >= 0);
      ls[cnt[a]].insert(a);
    }
  };

  rep(i, N) {
    assert(cand.size() + i == N);
    int rem = N - 1 - i;
    if (!ls[rem].empty()) {
      int idx = *ls[rem].begin();
      Del(idx);
      continue;
    }

    int idx = *cand.begin();
    if (!ans.empty() && as[ans.back()] == idx) {
      assert(cand.size() > 1);
      auto itr = cand.begin();
      ++itr;
      idx = *itr;
    }
    Del(idx);
  }
  assert(cand.empty() && ans.size() == N);

  if (as[ans[N - 2]] == ans[N - 1]) {
    swap(ans[N - 3], ans[N - 2]);
  }

  rep(i, N) {
    if (i)
      cout << " ";
    cout << ans[i] + 1;
  }
  cout << endl;
  rep(i, N - 1) assert(ans[i + 1] != as[ans[i]]);
}


Fixed submission:
#include <bits/stdc++.h>
#define XX first
#define YY second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define REP rep
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD = 1e9 + 7;

#define rall(X) (X).rbegin(), (X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}

int N;
vector<int> as;
int cnt[114514];
set<int> ls[114514];

signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  cin >> N;
  rep(i, N) {
    int a;
    cin >> a;
    --a;
    as.eb(a);
    cnt[a]++;
  }

  if (N == 2) {
    cout << "-1\n";
    return 0;
  }

  set<int> cand;
  rep(i, N) {
    cand.insert(i);
    ls[cnt[i]].insert(i);
  }

  vector<int> ans;
  auto Del = [&](int idx) {
    ans.eb(idx);
    cand.erase(idx);
    assert(ls[cnt[idx]].count(idx));
    ls[cnt[idx]].erase(idx);
    int a = as[idx];
    if (cand.count(a)) {
      assert(ls[cnt[a]].count(a));
      ls[cnt[a]].erase(a);
      cnt[a]--;
      assert(cnt[a] >= 0);
      ls[cnt[a]].insert(a);
    }
  };

  rep(i, N) {
    assert(cand.size() + i == N);
    int rem = N - 1 - i;
    if (!ls[rem].empty()) {
      int idx = *ls[rem].begin();
      Del(idx);
      continue;
    }

    int idx = *cand.begin();
    if (!ans.empty() && as[ans.back()] == idx) {
      assert(cand.size() > 1);
      auto itr = cand.begin();
      ++itr;
      idx = *itr;
    }
    Del(idx);
  }
  assert(cand.empty() && ans.size() == N);

  if (as[ans[N - 2]] == ans[N - 1]) {
    if (as[ans[N - 4]] == ans[N - 2]) {
      swap(ans[N - 3], ans[N - 1]);
      swap(ans[N - 2], ans[N - 1]);
    } else {
      swap(ans[N - 3], ans[N - 2]);
    }
  }

  rep(i, N) {
    if (i)
      cout << " ";
    cout << ans[i] + 1;
  }
  cout << endl;
  rep(i, N - 1) assert(ans[i + 1] != as[ans[i]]);
}