Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define eb emplace_back
#define deb(x) (cout << #x << ": " << x << endl)

using namespace std;

typedef pair<int, int> ii;
typedef long long ll;
const int N = 1000007, inf = 0x3f3f3f3f;

vector<int> vec[N];
int ans[N], a[N];

void no() {
  cout << "No" << endl;
  exit(0);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    a[i] = x;
    vec[x].eb(i);
  }

  for (int j = 1; j <= n; ++j) {
    int x;
    cin >> x;
    vec[x].eb(0);
  }

  priority_queue<ii> pq;
  for (int i = 1; i < N; ++i) {
    if (vec[i].size())
      pq.emplace((int)vec[i].size(), i);
  }
  // simula 2 em 2
  vector<ii> A, B;
  while (pq.size() >= 2) {
    int tot1, x, tot2, y;

    tie(tot1, x) = pq.top();
    pq.pop();

    // deb(tot1);
    // deb(x);

    if (pq.size() == 0)
      break;

    tie(tot2, y) = pq.top();
    pq.pop();

    // deb(tot2);
    // deb(y);

    assert(x != y);

    int i = vec[x].back();
    vec[x].pop_back();
    int j = vec[y].back();
    vec[y].pop_back();

    // deb(i);
    // deb(j);

    if (i and j)
      A.eb(i, j);
    else if ((i | j) == 0)
      B.eb(x, y);
    else if (i)
      ans[i] = y;
    else
      ans[j] = x;

    if (--tot1)
      pq.emplace(tot1, x);
    if (--tot2)
      pq.emplace(tot2, y);
  }

  // deb(pq.size());
  if (pq.size())
    no();

  assert(A.size() == B.size());

  while (A.size()) {
    if (a[A.back().fi] == B.back().fi or a[A.back().se] == B.back().se)
      swap(B.back().fi, B.back().se);
    ans[A.back().fi] = B.back().fi;
    ans[A.back().se] = B.back().se;

    assert(a[A.back().fi] != B.back().fi);
    assert(a[A.back().se] != B.back().se);
    A.pop_back();
    B.pop_back();
  }

  cout << "Yes" << endl;
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << ' ';
  cout << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define eb emplace_back
#define deb(x) (cout << #x << ": " << x << endl)

using namespace std;

typedef pair<int, int> ii;
typedef long long ll;
const int N = 1000007, inf = 0x3f3f3f3f;

vector<int> vec[N];
int ans[N], a[N];

void no() {
  cout << "No" << endl;
  exit(0);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    a[i] = x;
    vec[x].eb(i);
  }

  for (int j = 1; j <= n; ++j) {
    int x;
    cin >> x;
    vec[x].eb(0);
  }

  priority_queue<ii> pq;
  for (int i = 1; i < N; ++i) {
    if (vec[i].size())
      pq.emplace((int)vec[i].size(), i);
  }
  // simula 2 em 2
  vector<ii> A, B;
  while (pq.size() >= 2) {
    int tot1, x, tot2, y;

    tie(tot1, x) = pq.top();
    pq.pop();

    // deb(tot1);
    // deb(x);

    if (pq.size() == 0)
      break;

    tie(tot2, y) = pq.top();
    pq.pop();

    // deb(tot2);
    // deb(y);

    int i = vec[x].back();
    vec[x].pop_back();
    int j = vec[y].back();
    vec[y].pop_back();

    // deb(i);
    // deb(j);

    if (i and j)
      A.eb(i, j);
    else if ((i | j) == 0)
      B.eb(x, y);
    else if (i)
      ans[i] = y;
    else
      ans[j] = x;

    if (--tot1)
      pq.emplace(tot1, x);
    if (--tot2)
      pq.emplace(tot2, y);
  }

  // deb(pq.size());
  if (pq.size())
    no();

  assert(A.size() == B.size());

  while (A.size()) {
    if (a[A.back().fi] == B.back().fi)
      swap(B.back().fi, B.back().se);
    ans[A.back().fi] = B.back().fi;
    ans[A.back().se] = B.back().se;

    assert(a[A.back().fi] != B.back().fi);
    assert(a[A.back().se] != B.back().se);
    A.pop_back();
    B.pop_back();
  }

  cout << "Yes" << endl;
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << ' ';
  cout << endl;
}