// 各辺にコスト1を割り当て,x->y, x->z に流量 2 の最小費用流を流し,x->y のパスを固定して // 残りの頂点と辺で z->x, z->y に流量 2 の最小費用流を流す #include #include #include #include using namespace std; #include #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; } vector> to(N); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { if (mat[i][j]) to[i].push_back(j); } } return {N, X, Y, Z, to}; } }; // namespace INPUT #line 3 "twopaths.hpp" #include #line 8 "twopaths.hpp" // 各辺にコスト1を割り当て,x->y, x->z に流量 2 の最小費用流を流し,x->y のパスを固定して // 残りの頂点と辺で z->x, z->y に流量 2 の最小費用流を流す namespace TWOPATHS { using namespace std; // used_vs に含まれる頂点は使わずに,from -> to1 と from->to2 の点素なパスを構成する. // 両方のパスが構築できなければ empty vector の組を返す. pair, vector> twopaths(const vector> &to, const vector &used_vs, int from, int to1, int to2) { 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; 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); for (int i = 0; i < N; ++i) { for (auto j : to[i]) { int cost = 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; 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]); return {ret.at(0), ret.at(1)}; } constexpr int inf = 1 << 20; int solve_fake_flow_sub(const vector> &to, const vector &banxy, int z, int x, int y) { auto [p1, p2] = twopaths(to, banxy, z, x, y); if (p1.empty()) return inf; return p1.size() + p2.size() + banxy.size() - 1; } int solve_by_flow_twice(const vector> &to, int x, int y, int z) { int N = to.size(); vector vs{x, y, z}; sort(vs.begin(), vs.end()); int ret = inf; do { auto [p01, p02] = twopaths(to, {}, vs[0], vs[1], vs[2]); if (p01.empty() or p02.empty()) continue; auto ban01 = p01, ban02 = p02; for (int t = 0; t < 2; ++t) { ban01.pop_back(), ban02.pop_back(); reverse(ban01.begin(), ban01.end()), reverse(ban02.begin(), ban02.end()); } ret = min(ret, solve_fake_flow_sub(to, ban01, vs[2], vs[0], vs[1])); ret = min(ret, solve_fake_flow_sub(to, ban02, vs[1], vs[0], vs[2])); } while (next_permutation(vs.begin(), vs.end())); for (int u = 0; u < 3; ++u) { auto [p01a, p01b] = twopaths(to, {}, vs[0], vs[1], vs[1]); if (p01a.empty()) continue; if (count(p01a.begin(), p01a.end(), vs[2]) or count(p01b.begin(), p01b.end(), vs[2])) { ret = min(ret, p01a.size() + p01b.size() - 2); } else { auto ban01a = p01a, ban01b = p01b; for (int t = 0; t < 2; ++t) { ban01a.pop_back(), ban01b.pop_back(); reverse(ban01a.begin(), ban01a.end()), reverse(ban01b.begin(), ban01b.end()); } ret = min(ret, solve_fake_flow_sub(to, ban01a, vs[2], vs[0], vs[1])); ret = min(ret, solve_fake_flow_sub(to, ban01b, vs[2], vs[0], vs[1])); } rotate(vs.begin(), vs.begin() + 1, vs.end()); } return ret <= N ? ret : -1; } } #line 11 "uso_determin.cpp" int main() { cin.tie(nullptr), ios::sync_with_stdio(false); auto [N, X, Y, Z, to] = INPUT::read(); int ret = TWOPATHS::solve_by_flow_twice(to, X, Y, Z); cout << ret << '\n'; }