結果

問題 No.1776 Love Triangle 2 (Hard)
ユーザー hitonanodehitonanode
提出日時 2021-11-20 19:56:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 16,905 bytes
コンパイル時間 2,991 ms
コンパイル使用メモリ 184,496 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-02 12:13:38
合計ジャッジ時間 101,967 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,812 KB
testcase_01 AC 5 ms
6,944 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 934 ms
6,944 KB
testcase_05 AC 875 ms
6,940 KB
testcase_06 AC 936 ms
6,940 KB
testcase_07 AC 358 ms
6,944 KB
testcase_08 AC 1,180 ms
6,940 KB
testcase_09 AC 22 ms
6,944 KB
testcase_10 AC 29 ms
6,944 KB
testcase_11 AC 31 ms
6,940 KB
testcase_12 AC 32 ms
6,940 KB
testcase_13 AC 46 ms
6,944 KB
testcase_14 AC 81 ms
6,940 KB
testcase_15 AC 163 ms
6,940 KB
testcase_16 AC 198 ms
6,940 KB
testcase_17 AC 353 ms
6,944 KB
testcase_18 AC 1,059 ms
6,940 KB
testcase_19 AC 27 ms
6,944 KB
testcase_20 AC 31 ms
6,940 KB
testcase_21 AC 32 ms
6,944 KB
testcase_22 AC 43 ms
6,940 KB
testcase_23 AC 43 ms
6,940 KB
testcase_24 AC 28 ms
6,940 KB
testcase_25 AC 45 ms
6,940 KB
testcase_26 AC 51 ms
6,944 KB
testcase_27 AC 39 ms
6,944 KB
testcase_28 AC 74 ms
6,940 KB
testcase_29 AC 1,321 ms
6,940 KB
testcase_30 AC 1,369 ms
6,944 KB
testcase_31 AC 691 ms
6,940 KB
testcase_32 AC 764 ms
6,940 KB
testcase_33 AC 415 ms
6,944 KB
testcase_34 AC 415 ms
6,940 KB
testcase_35 AC 318 ms
6,940 KB
testcase_36 AC 126 ms
6,944 KB
testcase_37 AC 60 ms
6,940 KB
testcase_38 AC 29 ms
6,944 KB
testcase_39 AC 1,847 ms
6,940 KB
testcase_40 AC 1,130 ms
6,940 KB
testcase_41 AC 607 ms
6,944 KB
testcase_42 WA -
testcase_43 AC 306 ms
6,944 KB
testcase_44 AC 298 ms
6,944 KB
testcase_45 WA -
testcase_46 AC 170 ms
6,944 KB
testcase_47 AC 40 ms
6,940 KB
testcase_48 AC 33 ms
6,940 KB
testcase_49 AC 1,070 ms
6,940 KB
testcase_50 AC 1,101 ms
6,944 KB
testcase_51 AC 589 ms
6,940 KB
testcase_52 AC 555 ms
6,940 KB
testcase_53 WA -
testcase_54 WA -
testcase_55 AC 255 ms
6,940 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1,721 ms
6,940 KB
testcase_60 AC 995 ms
6,940 KB
testcase_61 AC 499 ms
6,940 KB
testcase_62 AC 499 ms
6,944 KB
testcase_63 AC 286 ms
6,944 KB
testcase_64 AC 237 ms
6,944 KB
testcase_65 AC 106 ms
6,940 KB
testcase_66 AC 47 ms
6,940 KB
testcase_67 AC 34 ms
6,944 KB
testcase_68 AC 26 ms
6,944 KB
testcase_69 AC 2,883 ms
6,940 KB
testcase_70 AC 2,882 ms
6,940 KB
testcase_71 AC 2,769 ms
6,940 KB
testcase_72 AC 715 ms
6,940 KB
testcase_73 AC 2,857 ms
6,944 KB
testcase_74 AC 39 ms
6,944 KB
testcase_75 AC 40 ms
6,944 KB
testcase_76 AC 50 ms
6,940 KB
testcase_77 AC 57 ms
6,940 KB
testcase_78 AC 105 ms
6,944 KB
testcase_79 AC 145 ms
6,940 KB
testcase_80 AC 221 ms
6,940 KB
testcase_81 AC 490 ms
6,944 KB
testcase_82 AC 1,158 ms
6,940 KB
testcase_83 AC 2,633 ms
6,940 KB
testcase_84 AC 45 ms
6,940 KB
testcase_85 AC 52 ms
6,940 KB
testcase_86 AC 46 ms
6,940 KB
testcase_87 AC 61 ms
6,944 KB
testcase_88 AC 140 ms
6,944 KB
testcase_89 AC 61 ms
6,940 KB
testcase_90 AC 88 ms
6,944 KB
testcase_91 AC 106 ms
6,944 KB
testcase_92 AC 63 ms
6,940 KB
testcase_93 AC 152 ms
6,940 KB
testcase_94 AC 3,023 ms
6,940 KB
testcase_95 AC 3,724 ms
6,944 KB
testcase_96 AC 1,679 ms
6,944 KB
testcase_97 AC 1,803 ms
6,940 KB
testcase_98 AC 762 ms
6,940 KB
testcase_99 AC 907 ms
6,940 KB
testcase_100 AC 469 ms
6,940 KB
testcase_101 AC 220 ms
6,940 KB
testcase_102 AC 77 ms
6,940 KB
testcase_103 AC 45 ms
6,944 KB
testcase_104 AC 7,085 ms
6,940 KB
testcase_105 AC 2,651 ms
6,940 KB
testcase_106 AC 1,609 ms
6,944 KB
testcase_107 AC 1,495 ms
6,944 KB
testcase_108 AC 943 ms
6,944 KB
testcase_109 AC 732 ms
6,944 KB
testcase_110 AC 584 ms
6,940 KB
testcase_111 AC 421 ms
6,940 KB
testcase_112 AC 104 ms
6,940 KB
testcase_113 AC 50 ms
6,940 KB
testcase_114 AC 3,025 ms
6,940 KB
testcase_115 WA -
testcase_116 AC 1,452 ms
6,940 KB
testcase_117 AC 1,270 ms
6,940 KB
testcase_118 WA -
testcase_119 WA -
testcase_120 WA -
testcase_121 WA -
testcase_122 WA -
testcase_123 WA -
testcase_124 AC 3,007 ms
6,940 KB
testcase_125 AC 2,184 ms
6,940 KB
testcase_126 AC 1,454 ms
6,944 KB
testcase_127 AC 1,573 ms
6,940 KB
testcase_128 AC 642 ms
6,944 KB
testcase_129 AC 677 ms
6,948 KB
testcase_130 AC 516 ms
6,940 KB
testcase_131 AC 256 ms
6,940 KB
testcase_132 AC 130 ms
6,944 KB
testcase_133 AC 67 ms
6,940 KB
testcase_134 AC 24 ms
6,940 KB
testcase_135 WA -
testcase_136 WA -
testcase_137 WA -
testcase_138 WA -
testcase_139 WA -
testcase_140 WA -
testcase_141 WA -
testcase_142 WA -
testcase_143 WA -
testcase_144 AC 39 ms
6,944 KB
testcase_145 AC 35 ms
6,940 KB
testcase_146 AC 38 ms
6,944 KB
testcase_147 AC 35 ms
6,944 KB
testcase_148 AC 39 ms
6,940 KB
testcase_149 WA -
testcase_150 WA -
testcase_151 WA -
testcase_152 WA -
testcase_153 WA -
testcase_154 AC 28 ms
6,940 KB
testcase_155 WA -
testcase_156 WA -
testcase_157 WA -
testcase_158 WA -
testcase_159 WA -
testcase_160 WA -
testcase_161 WA -
testcase_162 WA -
testcase_163 WA -
testcase_164 WA -
testcase_165 WA -
testcase_166 WA -
testcase_167 AC 68 ms
6,940 KB
testcase_168 AC 66 ms
6,940 KB
testcase_169 WA -
testcase_170 WA -
testcase_171 WA -
testcase_172 AC 64 ms
6,940 KB
testcase_173 WA -
testcase_174 AC 62 ms
6,940 KB
testcase_175 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// 嘘ヒューリスティック
// x-y, y-z, z-x のうち一つを固定して残り二つの長さの和を MCF で最小化するのを反復する山登り
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <typename T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl
#define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr)
#else
#define dbg(x) (x)
#define dbgif(cond, x) 0
#endif

template <typename T, T INF = std::numeric_limits<T>::max() / 2, int INVALID = -1> struct ShortestPath {
    int V, E;
    bool single_positive_weight;
    T wmin, wmax;
    std::vector<std::vector<std::pair<int, T>>> to;

    ShortestPath(int V = 0) : V(V), E(0), single_positive_weight(true), wmin(0), wmax(0), to(V) {}
    void add_edge(int s, int t, T w) {
        assert(0 <= s and s < V);
        assert(0 <= t and t < V);
        to[s].emplace_back(t, w);
        E++;
        if (w > 0 and wmax > 0 and wmax != w) single_positive_weight = false;
        wmin = std::min(wmin, w);
        wmax = std::max(wmax, w);
    }

    std::vector<T> dist;
    std::vector<int> prev;

    void ZeroOneBFS(int s) {
        assert(0 <= s and s < V);
        dist.assign(V, INF), prev.assign(V, INVALID);
        dist[s] = 0;
        std::deque<int> que;
        que.push_back(s);
        while (!que.empty()) {
            int v = que.front();
            que.pop_front();
            for (auto nx : to[v]) {
                T dnx = dist[v] + nx.second;
                if (dist[nx.first] > dnx) {
                    dist[nx.first] = dnx, prev[nx.first] = v;
                    if (nx.second) {
                        que.push_back(nx.first);
                    } else {
                        que.push_front(nx.first);
                    }
                }
            }
        }
    }
};


#include <atcoder/mincostflow>

uint32_t rand_int() // XorShift random integer generator
{
    static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}


struct State {
    // x -> y -> z -> x のウォーク
    int x, y, z;
    vector<int> xy;  // [x, ..., y] or (empty)
    vector<int> yz;  // [y, ..., z] or (empty)
    vector<int> zx;  // [z, ..., x] or (empty)

    int nb_good_e() const { return (!xy.empty()) + (!yz.empty()) + (!zx.empty()); }
    bool feasible() const { return xy.size() and yz.size() and zx.size(); }

    int length() const {
        if (!feasible()) return -1;
        return xy.size() + yz.size() + zx.size() - 3;
    }

    vector<int> render() const {
        // 以下のいずれかを出力
        // - 現状態が表す解 [x, ..., y, ..., z, ..., x] (feasible)
        // - [] (infeasible)
        if (!feasible()) return {};
        vector<int> ret = xy;
        ret.pop_back();
        ret.insert(ret.end(), yz.begin(), yz.end());
        ret.pop_back();
        ret.insert(ret.end(), zx.begin(), zx.end());
        return ret;
    }

    // 状態に順序を導入
    // - (feasible soluton) < (infeasible solution)
    // - (shorter feasible solution) < (longer feasible solution)
    // - (lexicographically smaller feasible solution) < (lexicographically greater feasible solution)
    bool operator<(const State &x) const {
        if (!this->feasible()) return false;
        if (!x.feasible()) return true;
        int len = this->length(), xlen = x.length();
        if (len != xlen) return len < xlen;
        return this->render() < x.render();
    }

    void clear() {
        xy.clear();
        yz.clear();
        zx.clear();
    }

    vector<int> enum_inner_vs() const {
        // 現在の状態でパス上にある,端点 (x, y, z) 以外の頂点を列挙
        vector<int> ret;
        for (int i = 1; i + 1 < int(xy.size()); ++i) ret.push_back(xy[i]);
        for (int i = 1; i + 1 < int(yz.size()); ++i) ret.push_back(yz[i]);
        for (int i = 1; i + 1 < int(zx.size()); ++i) ret.push_back(zx[i]);
        return ret;
    }

    void init(int x_, int y_, int z_) {
        clear();
        x = x_, y = y_, z = z_;
    }

    void rev() {
        // パスの順序を逆転させる
        swap(y, z);
        swap(xy, zx);
        reverse(xy.begin(), xy.end());
        reverse(yz.begin(), yz.end());
        reverse(zx.begin(), zx.end());
    }
};

int N;
vector<vector<int>> to;

// used_vs に含まれる頂点は使わずに,from -> to1 と from->to2 の点素なパスを構成する.
// 両方のパスが構築できなければ empty vector の組を返す.
// already_feasible が true ならば最短パスを構築し,
// false ならばもう少しランダムに解を構築する.
pair<vector<int>, vector<int>> twopaths(const vector<int> &used_vs, bool already_feasible, int from, int to1, int to2) {
    const int gt = N * 2;
    atcoder::mcf_graph<int, int> graph(gt + 1);

    vector<int> valid_v(N, 1);
    for (auto i : used_vs) valid_v[i] = 0;

    if (!already_feasible) {
        REP(i, N) {
            if (rand_int() % 100 == 0) valid_v[i] = 0;
        }
    }

    valid_v[to1] = valid_v[to2] = 1;

    for (int i = 0; i < N; ++i) {
        graph.add_edge(i, i + N, valid_v[i], 0);
    }

    for (int i = 0; i < N; ++i) {
        for (auto j : to[i]) {
            int cost = 1;
            if (!already_feasible) cost = rand_int() % 5 + 1;
            graph.add_edge(i + N, j, 1, cost);
        }
    }
    graph.add_edge(to1 + N, gt, 1, 0);
    graph.add_edge(to2 + N, gt, 1, 0);
    auto f = graph.flow(from + N, gt, 2);
    if (f.first < 2) return {{}, {}};

    vector<int> conn(N);
    for (auto e : graph.edges()) {
        if (e.flow) {
            if (e.to == gt) continue;
            int s = e.from % N, t = e.to % N;
            conn[s] ^= t;
            conn[t] ^= s;  // ループがないので生えている辺の xor だけ持っておけば後で解が復元できる
        }
    }

    vector<int> ret1, ret2;
    while (to1 != from) {
        ret1.push_back(to1);
        to1 = conn[to1];
        conn[to1] ^= ret1.back();
    }
    while (to2 != from) {
        ret2.push_back(to2);
        to2 = conn[to2];
        conn[to2] ^= ret2.back();
    }
    ret1.push_back(from);
    ret2.push_back(from);
    reverse(ret1.begin(), ret1.end());
    reverse(ret2.begin(), ret2.end());
    return {ret1, ret2};
}

// a から b を経由し c に辿り着く点素なパスで,banned にあるものを使わないもののうち
// 最短で辞書順最小のものを求め,{[a, ..., b], [b, ..., c]} という pair of vector を返す.
pair<vector<int>, vector<int>> refine_path(int a, int b, int c, vector<int> banned) {
    vector<int> is_banned(N);
    for (auto i : banned) is_banned[i] = 1;
    auto [v1, v2] = twopaths(banned, true, b, a, c);
    const int len = v1.size() + v2.size();
    vector<int> ab{a};
    banned.push_back(a);
    is_banned[a] = 1;
    while (ab.back() != b) {
        int cur = ab.back();
        for (auto j : to[cur]) {
            if (j == b) {
                ab.push_back(b);
                break;
            }
            if (is_banned[j]) continue;
            if (j == c) continue;
            auto [p1, p2] = twopaths(banned, true, b, j, c);
            if (p1.empty()) continue;
            if (int(p1.size() + p2.size() + ab.size()) != len) continue;

            ab.push_back(j);
            banned.push_back(j);
            is_banned[j] = 1;
            break;
        }
    }

    ShortestPath<int, 1 << 20> sssp(N);
    for (int i = 0; i < N; ++i) {
        if (is_banned[i]) continue;
        for (auto j : to[i]) {
            if (is_banned[j]) continue;
            sssp.add_edge(i, j, 1);
        }
    }
    sssp.ZeroOneBFS(b);
    const auto Db = sssp.dist;
    sssp.ZeroOneBFS(c);
    const auto Dc = sssp.dist;

    vector<int> bc{b};
    int cur = b;
    while (cur != c) {
        for (auto nxt : to[cur]) {
            if (Db[nxt] == Db[cur] + 1 and Dc[nxt] == Dc[cur] - 1) {
                cur = nxt;
                bc.push_back(cur);
                break;
            }
        }
    }
    return {ab, bc};
}

// x->y->z->x の feasible な解について, z->x には手を加えず残りを辞書順最小にする
void refine_xyz(State &state) {
    if (!state.feasible()) return;  // infeasible なら何もしない
    const int x = state.xy[0], y = state.yz[0], z = state.zx[0];
    auto [xy, yz] = refine_path(x, y, z, vector<int>(state.zx.begin() + 1, state.zx.end() - 1));
    state.xy = move(xy);
    state.yz = move(yz);
}

// x->y->z->x の feasible な解について,x->y には手を加えず残りを辞書順最小にする
void refine_yzx(State &state) {
    if (!state.feasible()) return; // infeasible なら何もしない
    const int x = state.xy[0], y = state.yz[0], z = state.zx[0];
    auto [yz, zx] = refine_path(y, z, x, vector<int>(state.xy.begin() + 1, state.xy.end() - 1));
    state.yz = move(yz);
    state.zx = move(zx);
}

void refine(State &state) {
    if (!state.feasible()) return;
    refine_xyz(state);
    refine_yzx(state);
    auto v = state.render();
    auto vrev = v;
    reverse(vrev.begin(), vrev.end());
    if (vrev < v) {
        state.rev();
        refine_xyz(state);
        refine_yzx(state);
    }
}

void step(State &state, int x, int y, int z) {
    while (state.nb_good_e() >= 2) {
        int i = rand_int() % 3;
        if (i == 0) state.xy.clear();
        if (i == 1) state.yz.clear();
        if (i == 2) state.zx.clear();
    }

    for (int t = 0; t < 10; ++t) {
        if (rand_int() % 5 == 0 and state.xy.empty() and state.zx.empty()) {
            auto [xy, xz] = twopaths(state.enum_inner_vs(), state.feasible(), x, y, z);
            reverse(xz.begin(), xz.end());
            state.xy = xy;
            state.zx = xz;
        }
        if (rand_int() % 5 == 0 and state.xy.empty() and state.yz.empty()) {
            auto [yx, yz] = twopaths(state.enum_inner_vs(), state.feasible(), y, x, z);
            reverse(yx.begin(), yx.end());
            state.xy = yx;
            state.yz = yz;
        }
        if (rand_int() % 5 == 0 and state.yz.empty() and state.zx.empty()) {
            auto [zx, zy] = twopaths(state.enum_inner_vs(), state.feasible(), z, x, y);
            reverse(zy.begin(), zy.end());
            state.zx = zx;
            state.yz = zy;
        }
    }
}

int main() {
    int M;
    int x, y, z;
    cin >> N >> M;
    cin >> x >> y >> z;

    --x, --y, --z;
    vector conn(N, vector<int>(N, 1));
    REP(i, N) conn[i][i] = 0;
    while (M--) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        conn[a][b] = conn[b][a] = 0;
    }
    to.assign(N, {});
    REP(i, N) REP(j, N) if (conn[i][j]) to[i].push_back(j);

    State best_all;
    for (int dir = 0; dir < 2; ++dir) {
        State best, state;
        state.init(x, y, z);
        FOR(t, 1, 301) {
            step(state, x, y, z);
            if (state < best) best = state;
            if (t % 100 == 0) {
                refine_xyz(state);
                refine_yzx(state);
                if (state < best) best = state;
            }
        }
        // refine(state);
        // if (state < best) best = state;
        refine(best);
        if (best < best_all) best_all = best;

        swap(y, z);
    }

    cout << best_all.length() << '\n';
    auto path = best_all.render();
    if (path.size()) {
        for (auto x : path) cout << x + 1 << ' ';
        cout << '\n';
    }
}
0