// 方針(正当性なし) // - 現在使われていない頂点のみを使い x -> y に流量 2 を流して,一方の x-y パスを採択する // - 現在使われていない頂点のみを使い x -> y, x -> z に流量 1 ずつ同時に流して,両方のパスを採択する // - x -> y パスを消す // - ランダムに頂点を使用不能にする // - フローを流す時の重みはランダムに定める #line 2 "parameter.hpp" const int MAX_DEL_V = 2; // 最大ランダム消去頂点数 const int NITER = 3000; // 反復回数 const int EARLY_STOP = 200; // この回数反復して既に解が見つかっていたらすぐやめる #line 9 "uso_drop.cpp" #include #include #include #include #include using namespace std; #include namespace INPUT { using namespace std; tuple>> read() { int N, M, X, Y, Z; cin >> N >> M >> X >> Y >> Z; --X, --Y, --Z; vector mat(N, vector(N)); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { mat[i][j] = (i != j); } } while (M--) { int a, b; cin >> a >> b; --a, --b; mat[a][b] = mat[b][a] = 0; } assert(mat[X][Y] + mat[Y][X] + mat[X][Z] + mat[Z][X] + mat[Y][Z] + mat[Z][Y] == 0); vector> to(N); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { if (mat[i][j] > 0) to[i].push_back(j); } } return {N, X, Y, Z, to}; } }; // namespace INPUT #line 3 "twopaths.hpp" #include #include #line 8 "twopaths.hpp" namespace TWOPATHS { using namespace std; uint32_t rand_int() { 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)); } // used_vs に含まれる頂点は使わずに,from -> to1 と from->to2 の点素なパスを構成する. // 両方のパスが構築できなければ empty vector の組を返す. pair, vector> twopaths(const vector> &to, const vector &used_vs, int from, int to1, int to2, bool randomize=false) { const int N = to.size(); 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 (!valid_v[to1] or !valid_v[to2] or !valid_v[from]) return {{}, {}}; valid_v[from] = valid_v[to1] = valid_v[to2] = 0; for (int i = 0; i < N; ++i) { graph.add_edge(i, i + N, valid_v[i], 0); } graph.add_edge(to1, to1 + N, 1, 0); graph.add_edge(to2, to2 + N, 1, 0); static array weights{0, 1, 2, 4, 8, 100}; for (int i = 0; i < N; ++i) { for (auto j : to[i]) { int cost = 1; if (randomize) { cost = weights[rand_int() % weights.size()]; } 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; if (s != t) conn[s].push_back(t); } } vector> ret; while (conn[from].size()) { int now = from; vector vec{now}; while (conn[now].size()) { int nxt = conn[now].back(); conn[now].pop_back(); now = nxt; vec.push_back(now); } ret.push_back(vec); } assert(ret.size() == 2); if (ret[0].back() != to1) swap(ret[0], ret[1]); if (ret[1].back() != to2) swap(ret[0], ret[1]); assert(ret[0].front() == from and ret[0].back() == to1); assert(ret[1].front() == from and ret[1].back() == to2); return {ret.at(0), ret.at(1)}; } }; // namespace TWOPATHS namespace Heuristic { uint32_t rand_int() { 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 { int x, y, z; vector> to; vector xy, yz, zx; State(int x_, int y_, int z_, vector> to_) : x(x_), y(y_), z(z_), to(to_) {} bool xy_good() const { return xy.size() and xy.front() == x and xy.back() == y; } bool yz_good() const { return yz.size() and yz.front() == y and yz.back() == z; } bool zx_good() const { return zx.size() and zx.front() == z and zx.back() == x; } bool is_feasible() const { return xy_good() and yz_good() and zx_good(); } static vector inner(std::vector vs) { if (vs.empty()) return {}; vs.pop_back(); swap(vs.front(), vs.back()); vs.pop_back(); return vs; } vector enum_inner_vs() const { 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 opt_xyz() { if (!zx_good()) return; if (count(zx.begin(), zx.end(), y)) return; auto [yxnew, yznew] = TWOPATHS::twopaths(to, inner(zx), y, x, z, !is_feasible()); xy = yxnew; reverse(xy.begin(), xy.end()); yz = yznew; if (!verify()) cerr << "xyz bad" << endl; } void opt_yzx() { if (!xy_good()) return; if (count(xy.begin(), xy.end(), z)) return; auto [zynew, zxnew] = TWOPATHS::twopaths(to, inner(xy), z, y, x, !is_feasible()); yz = zynew; reverse(yz.begin(), yz.end()); zx = zxnew; if (!verify()) cerr << "yzx bad" << endl; } void opt_zxy() { if (!yz_good()) return; if (count(yz.begin(), yz.end(), x)) return; auto [xznew, xynew] = TWOPATHS::twopaths(to, inner(yz), x, z, y, !is_feasible()); zx = xznew; reverse(zx.begin(), zx.end()); xy = xynew; if (!verify()) cerr << "zxy bad" << endl; } void init_xyxy() { yz.clear(); zx.clear(); auto [xy1, xy2] = TWOPATHS::twopaths(to, {}, x, y, y, true); if (rand_int() & 1) { xy = xy1; } else { xy = xy2; } } vector dump() const { // return [x, ..., y, ..., z, ..., x] if (!is_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; } int optval() const { if (!is_feasible()) return to.size() + 1; assert(xy.front() == x and xy.back() == y); assert(yz.front() == y and yz.back() == z); assert(zx.front() == z and zx.back() == x); return xy.size() + yz.size() + zx.size() - 3; } bool verify() const { if (!is_feasible()) return true; auto p = dump(); int last = -1; vector cntr(to.size()); cntr.at(x)--; for (auto k : p) { cntr.at(k)++; if (cntr[k] > 1) { std::cerr << k << ' ' << x << ' ' << y << ' ' << z << std::endl; } if (last >= 0) { int n = count(to[last].begin(), to[last].end(), k); if (n == 0) return false; } last = k; } if (*max_element(cntr.begin(), cntr.end()) > 1) { cerr << "(" << x << ' ' << y << ' ' << z << ")\n"; for (auto i : xy) cerr << i << ' '; cerr << endl; for (auto i : yz) cerr << i << ' '; cerr << endl; for (auto i : zx) cerr << i << ' '; cerr << endl; return false; } return true; } }; }; // namespace Heuristic #line 18 "uso_drop.cpp" 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)); } vector> drop_graph(vector> to, int x, int y, int z, int k) { // 最大 k 頂点ランダムに消去 const int n = to.size(); vector is_erased(n); for (int i = 0; i < k; ++i) is_erased[rand_int() % n] = 1; is_erased[x] = is_erased[y] = is_erased[z] = 0; for (int i = 0; i < n; ++i) { if (is_erased[i]) { to[i].clear(); } else { for (int j = 0; j < int(to[i].size()); ++j) { if (is_erased[to[i][j]]) { swap(to[i][j], to[i].back()); to[i].pop_back(); --j; } } } } return to; } int main() { cin.tie(nullptr), ios::sync_with_stdio(false); auto [N, X, Y, Z, to] = INPUT::read(); vector xyz{X, Y, Z}; int ret = N + 1; for (int ntry = 0; ntry < NITER; ++ntry) { int nerase = rand_int() % (MAX_DEL_V + 1); // cerr << nerase << ':'; int d = rand_int() % 3; Heuristic::State state(xyz[d], xyz[(d + 1) % 3], xyz[(d + 2) % 3], drop_graph(to, X, Y, Z, nerase)); state.init_xyxy(); state.opt_yzx(); if (!state.is_feasible()) continue; for (int iter = 0; iter < 5; ++iter) { // auto p = state.dump().size(); int c = rand_int() % 3; if (c == 0) state.opt_xyz(); if (c == 1) state.opt_yzx(); if (c == 2) state.opt_zxy(); // if (state.dump().size() < p) cerr << " (" << iter << ")"; } state.verify(); ret = min(ret, state.optval()); if (ntry >= EARLY_STOP and ret <= N) break; // 既に解が見つかっているならば早めにやめる } cout << (ret <= N ? ret : -1) << '\n'; }