// 嘘ヒューリスティック // 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 #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)); } int N; int x, y, z; vector> to; struct State { vector xy; vector yz; vector zx; int n_feasible() 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 { 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; } }; pair, vector> twopaths(const State &state, 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 : state.xy) valid_v[i] = 0; for (auto i : state.yz) valid_v[i] = 0; for (auto i : state.zx) valid_v[i] = 0; // REP(i, N) { // if (rand_int() % 100 == 0) valid_v[i] = 0; // } 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]) graph.add_edge(i + N, j, 1, 1); } graph.add_edge(to1, gt, 1, 0); graph.add_edge(to2, 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; } } 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}; } void step(State &state) { while (state.n_feasible() >= 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, 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, 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, z, x, y); reverse(zy.begin(), zy.end()); state.zx = zx; state.yz = zy; } } } int main() { int M; 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 state; REP(t, 100) step(state); cout << state.length() << '\n'; // auto path = state.render(); // for (auto x : path) cout << x << ' '; // cout << '\n'; }