Problem p02809 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  if (n == 2) {
    cout << "-1\n";
    return 0;
  }
  vector<int> hate(n);
  for (int i = 0; i < n; ++i)
    ++hate[a[i]];
  set<int> have;
  for (int i = 0; i < n; ++i)
    have.insert(i);
  vector<int> ans;
  vector<bool> used(n);
  int who = -1;
  for (int i = 0; i < n; ++i) {
    for (int j : have) {
      if (j == who)
        continue;
      int x = a[j];
      if (!used[x] && hate[x] == n - i - 1) {
        continue;
      }
      ans.push_back(j);
      used[j] = 1;
      --hate[x];
      who = x;
      break;
    }
    have.erase(ans.back());
    if (n - i - 1 == 3)
      break;
  }
  vector<int> cur;
  for (int j : have)
    cur.push_back(j);
  vector<int> best;
  do {
    if (cur.front() == who)
      continue;
    bool bad = false;
    for (int i = 0; i + 1 < (int)cur.size(); ++i) {
      if (cur[i + 1] == a[cur[i]]) {
        bad = true;
        break;
      }
    }
    if (!bad) {
      if (best.empty() || cur < best)
        best = cur;
    }
  } while (next_permutation(cur.begin(), cur.end()));

  for (int x : ans) {
    cout << x + 1 << ' ';
  }
  for (int x : best) {
    cout << x + 1 << ' ';
  }
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  if (n == 2) {
    cout << "-1\n";
    return 0;
  }
  vector<int> hate(n);
  for (int i = 0; i < n; ++i)
    ++hate[a[i]];
  set<int> have;
  for (int i = 0; i < n; ++i)
    have.insert(i);
  vector<int> ans;
  vector<bool> used(n);
  int who = -1;
  for (int i = 0; i < n; ++i) {
    for (int j : have) {
      if (j == who)
        continue;
      int x = a[j];
      if (!used[x] && hate[x] == n - i - 1) {
        continue;
      }
      ans.push_back(j);
      used[j] = 1;
      --hate[x];
      who = x;
      break;
    }
    have.erase(ans.back());
    if (n - i - 1 <= 6)
      break;
  }
  vector<int> cur;
  for (int j : have)
    cur.push_back(j);
  vector<int> best;
  do {
    if (cur.front() == who)
      continue;
    bool bad = false;
    for (int i = 0; i + 1 < (int)cur.size(); ++i) {
      if (cur[i + 1] == a[cur[i]]) {
        bad = true;
        break;
      }
    }
    if (!bad) {
      if (best.empty() || cur < best)
        best = cur;
    }
  } while (next_permutation(cur.begin(), cur.end()));

  for (int x : ans) {
    cout << x + 1 << ' ';
  }
  for (int x : best) {
    cout << x + 1 << ' ';
  }
}