Language: C++

#include <bits/stdc++.h>
/** #include <ext/pb_ds/assoc_container.hpp>
 * #include <ext/pb_ds/tree_policy.hpp>
 * using namespace __gnu_pbds; */
using namespace std;

#define ll long long
#define ull unsigned long long
#define f(a, b) for (ll i = a; i < b; i++)
#define mod 1000000007
#define pb push_back
#define vll vector<ll>
#define pll vector<pair<ll, ll>>
#define ld long double
#define fr(a, b) for (ll j = a; j >= b; j--)
#define fi(a, b) for (ll j = a; j < b; j++)
#define fii(a, b) for (ll k = a; k < b; k++)
/** typedef tree<ll
 * ,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
 * ordered_set; */

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  os << "]";
  return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}

struct compare {
public:
  bool operator()(const pair<ll, vector<ll>> &a,
                  const pair<ll, vector<ll>> &b) {
    return a.second.size() < b.second.size() || a.first < b.first;
  }
};

const ll N = 200005;
ll a[N], b[N];
vll same[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  f(1, n + 1) cin >> a[i];
  f(1, n + 1) cin >> b[i];
  f(1, n + 1) if (a[i] == b[i]) same[a[i]].push_back(i);
  set<pair<ll, vector<ll>>, compare> s;
  f(1, n + 1) if (!same[i].empty()) s.insert({i, same[i]});
  while (s.size() > 2) {
    vector<ll> v1 = (--s.end())->second;
    ll val1 = (--s.end())->first;
    s.erase(--s.end());
    vector<ll> v2 = (--s.end())->second;
    ll val2 = (--s.end())->first;
    s.erase(--s.end());
    swap(b[v1.back()], b[v2.back()]);
    v1.pop_back();
    v2.pop_back();
    if (!v1.empty())
      s.insert({val1, v1});
    if (!v2.empty())
      s.insert({val2, v2});
  }
  if (s.empty()) {
    cout << "Yes\n";
    f(1, n + 1) cout << b[i] << ' ';
    return 0;
  }
  vll eligible;
  vll v = s.begin()->second;
  ll val = s.begin()->first;
  s.clear();
  f(1, n + 1) {
    if (!(b[i] == val || a[i] == val))
      eligible.push_back(i);
  }
  if (eligible.size() < v.size()) {
    cout << "No\n";
    return 0;
  }
  while (!v.empty()) {
    swap(b[v.back()], b[eligible.back()]);
    v.pop_back();
    eligible.pop_back();
  }
  cout << "Yes\n";
  f(1, n + 1) cout << b[i] << ' ';

  return 0;
}

Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<long long int, std::vector<long long int> >; _Val = std::pair<long long int, std::vector<long long int> >; _KeyOfValue = std::_Identity<std::pair<long long int, std::vector<long long int> > >; _Compare = compare; _Alloc = std::allocator<std::pair<long long int, std::vector<long long int> > >; _Const_Link_type = const std::_Rb_tree_node<std::pair<long long int, std::vector<long long int> > >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::pair<long long int, std::vector<long long int> >; _Val = std::pair<long long int, std::vector<long long int> >; _KeyOfValue = std::_Identity<std::pair<long long int, std::vector<long long int> > >; _Compare = compare; _Alloc = std::allocator<std::pair<long long int, std::vector<long long int> > >; key_type = std::pair<long long int, std::vector<long long int> >]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = std::pair<long long int, std::vector<long long int> >; _Key = std::pair<long long int, std::vector<long long int> >; _Val = std::pair<long long int, std::vector<long long int> >; _KeyOfValue = std::_Identity<std::pair<long long int, std::vector<long long int> > >; _Compare = compare; _Alloc = std::allocator<std::pair<long long int, std::vector<long long int> > >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:523:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = std::pair<long long int, std::vector<long long int> >; _Compare = compare; _Alloc = std::allocator<std::pair<long long int, std::vector<long long int> > >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<long long int, std::vector<long long int> >, std::pair<long long int, std::vector<long long int> >, std::_Identity<std::pair<long long int, std::vector<long long int> > >, compare, std::allocator<std::pair<long long int, std::vector<long long int> > > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<long long int, std::vector<long long int> > >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, std::vector<long long int> > >, std::pair<long long int, std::vector<long long int> > >::rebind<std::pair<long long int, std::vector<long long int> > >; typename _Alloc::value_type = std::pair<long long int, std::vector<long long int> >; value_type = std::pair<long long int, std::vector<long long int> >]'
  523 |           _M_t._M_insert_unique(std::move(__x));
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:67:45:   required from here
   67 |   f(1, n + 1) if (!same[i].empty()) s.insert({i, same[i]});
      |                                     ~~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const compare&, const std::pair<long long int, std::vector<long long int, std::allocator<long long int> > >&, const std::pair<long long int, std::vector<long long int, std::allocator<long long int> > >&>' evaluates to false