// 嘘ヒューリスティック // x-y, y-z, z-x のうち一つを固定して残り二つの長さの和を MCF で最小化するのを反復する山登り #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; 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##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template 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 pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector sort_unique(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } #endif template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &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 ::max() / 2, int INVALID = -1> struct ShortestPath { int V, E; bool single_positive_weight; T wmin, wmax; std::vector>> 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 dist; std::vector prev; void ZeroOneBFS(int s) { assert(0 <= s and s < V); dist.assign(V, INF), prev.assign(V, INVALID); dist[s] = 0; std::deque 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 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 xy; // [x, ..., y] or (empty) vector yz; // [y, ..., z] or (empty) vector 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 render() const { // 以下のいずれかを出力 // - 現状態が表す解 [x, ..., y, ..., z, ..., x] (feasible) // - [] (infeasible) if (!feasible()) return {}; vector 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 enum_inner_vs() const { // 現在の状態でパス上にある,端点 (x, y, z) 以外の頂点を列挙 vector 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> to; // used_vs に含まれる頂点は使わずに,from -> to1 と from->to2 の点素なパスを構成する. // 両方のパスが構築できなければ empty vector の組を返す. // already_feasible が true ならば最短パスを構築し, // false ならばもう少しランダムに解を構築する. pair, vector> twopaths(const vector &used_vs, bool already_feasible, int from, int to1, int to2) { const int gt = N * 2; atcoder::mcf_graph graph(gt + 1); vector 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 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 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> refine_path(int a, int b, int c, vector banned) { vector 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 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 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 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(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(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(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'; } }