Language: C++

/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>
#include <cstring>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

vector<int> EulerianTrail(const int s, vector<vector<int>> &g, const bool directed) {
    function<void (int, vector<int> &)> dfs = [&](int u, vector<int> &trail) {
        while (!g[u].empty()) {
            int v = g[u].back();
            g[u].pop_back();
            if (!directed) {
                for (int i = 0; i < g[v].size(); i ++) {
                    if (g[v][i] == u) {
                        g[v].erase(g[v].begin() + i);
                        break;
                    }
                }
            }
            dfs(v, trail);
        }
        trail.push_back(u);
    };
    vector<int> trail;
    dfs(s, trail);
    reverse(trail.begin(), trail.end());
    return trail;
}


signed main() {
    int N, A, B;
    cin >> N >> A >> B;
    int NN = 1LL << N;

    vector<vector<int>> table(NN);
    vector<vector<int>> sub_table(NN);
    for (int i = 0; i < NN; i++) {
        for (int k = 1; i + k <= NN - 1; k *= 2) {
            if (__builtin_popcountll(i ^ (i + k)) != 1) continue;
            table[i].push_back(i + k);
            table[i + k].push_back(i);

            if ((i == A && (i + k) == B) || (i == B && (i + k) == A)) continue;
            sub_table[i].push_back(i + k);
            sub_table[i + k].push_back(i);
        }
    }

    // オイラー経路を構築可能かの判定
    for (int i = 0; i < NN; i++) {
        if (i == A || i == B) continue;
        if (table[i].size() % 2 == 1) {
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;

    bool start_is_odd = table[A].size() % 2 == 1;
    bool goal_is_odd = table[B].size() % 2 == 1;

    vector<int> ans;
    if (goal_is_odd) {
        ans = EulerianTrail(A, table, false);
    } else if (start_is_odd) {
        ans = EulerianTrail(B, table, false);
        reverse(ans.begin(), ans.end());
    } else {
        ans = EulerianTrail(A, sub_table, false);
    }

    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i];
        if (i != ans.size() - 1) cout << " ";
    }
    cout << endl;

    return 0;
}

Compiler output:
a.cc:39:21: warning: overflow in conversion from 'long long int' to 'int' changes value from '36028797018963968' to '0' [-Woverflow]
   39 | const int INF = 1LL << 55;
      |                 ~~~~^~~~~
a.cc: In function 'std::vector<int> EulerianTrail(int, std::vector<std::vector<int> >&, bool)':
a.cc:42:5: error: 'function' was not declared in this scope
   42 |     function<void (int, vector<int> &)> dfs = [&](int u, vector<int> &trail) {
      |     ^~~~~~~~
a.cc:28:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
   27 | #include <stack>
  +++ |+#include <functional>
   28 | 
a.cc:42:38: error: expression list treated as compound expression in functional cast [-fpermissive]
   42 |     function<void (int, vector<int> &)> dfs = [&](int u, vector<int> &trail) {
      |                                      ^
a.cc:42:14: error: expected primary-expression before 'void'
   42 |     function<void (int, vector<int> &)> dfs = [&](int u, vector<int> &trail) {
      |              ^~~~
a.cc:59:5: error: 'dfs' was not declared in this scope; did you mean 'ffs'?
   59 |     dfs(s, trail);
      |     ^~~
      |     ffs