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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

ll Pow(ll n, ll p) {
  ll r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1)
      r = (r * n) % mod;
    n = (n * n) % mod;
  }
  return r;
}

const int M = 100005;
ll F[M];

void Init() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = F[i - 1] * i % mod;
}

ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }

ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }

int n;
vi a, b, c;

int main() {
  cin >> n;
  a = b = c = vi(n);
  for (auto &i : a) {
    cin >> i;
    c[i - 1]++;
  }
  for (auto &i : b) {
    cin >> i;
    c[i - 1]++;
  }
  for (int i = 0; i < n; i++)
    if (c[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  cout << "Yes" << endl;
  reverse(b.begin(), b.end());
  vi d;
  int x = -1;
  for (int i = 0; i < n; i++)
    if (a[i] == b[i])
      x = b[i];
  for (int i = 0; i < n; i++)
    if (a[i] != x && b[i] != x)
      d.push_back(i);
  for (int i = 0; i < n; i++)
    if (a[i] == b[i]) {
      assert(!d.empty());
      int t = d.back();
      d.pop_back();
      swap(b[i], b[t]);
    }
  for (int i = 0; i < n; i++)
    cout << b[i] << " \\n"[i == n - 1];
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

ll Pow(ll n, ll p) {
  ll r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1)
      r = (r * n) % mod;
    n = (n * n) % mod;
  }
  return r;
}

const int M = 100005;
ll F[M];

void Init() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = F[i - 1] * i % mod;
}

ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }

ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }

int n;
vi a, b, c;

int main() {
  cin >> n;
  a = b = c = vi(n);
  for (auto &i : a) {
    cin >> i;
    c[i - 1]++;
  }
  for (auto &i : b) {
    cin >> i;
    c[i - 1]++;
  }
  for (int i = 0; i < n; i++)
    if (c[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  cout << "Yes" << endl;
  reverse(b.begin(), b.end());
  vi d;
  int x = -1;
  for (int i = 0; i < n; i++)
    if (a[i] == b[i])
      x = b[i];
  for (int i = 0; i < n; i++)
    if (a[i] != x)
      d.push_back(i);
  for (int i = 0; i < n; i++)
    if (a[i] == b[i]) {
      int t = d.back();
      d.pop_back();
      swap(b[i], b[t]);
    }
  for (int i = 0; i < n; i++)
    assert(a[i] != b[i]);
  for (int i = 0; i < n; i++)
    cout << b[i] << " \\n"[i == n - 1];
}