結果
| 問題 |
No.1775 Love Triangle 2
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2021-11-25 23:50:57 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 7,957 bytes |
| コンパイル時間 | 6,660 ms |
| コンパイル使用メモリ | 162,492 KB |
| 最終ジャッジ日時 | 2025-01-26 01:17:38 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 77 WA * 10 TLE * 3 |
ソースコード
// 方針(正当性なし)
// - 現在使われていない頂点のみを使い x -> y に流量 2 を流して,一方の x-y パスを採択する
// - 現在使われていない頂点のみを使い x -> y, x -> z に流量 1 ずつ同時に流して,両方のパスを採択する
// - x -> y パスを消す
// - ランダムに頂点を使用不能にする
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#include <tuple>
namespace INPUT {
using namespace std;
tuple<int, int, int, int, vector<vector<int>>> read() {
int N, M, X, Y, Z;
cin >> N >> M >> X >> Y >> Z;
--X, --Y, --Z;
vector mat(N, vector<int>(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<vector<int>> 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 <atcoder/mincostflow>
#include <cassert>
#line 8 "twopaths.hpp"
namespace TWOPATHS {
using namespace std;
// used_vs に含まれる頂点は使わずに,from -> to1 と from->to2 の点素なパスを構成する.
// 両方のパスが構築できなければ empty vector の組を返す.
pair<vector<int>, vector<int>> twopaths(const vector<vector<int>> &to, const vector<int> &used_vs, int from, int to1, int to2) {
const int N = to.size();
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;
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<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;
if (s != t) conn[s].push_back(t);
}
}
vector<vector<int>> ret;
while (conn[from].size()) {
int now = from;
vector<int> 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)};
}
}
namespace Yuki7140 {
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 {
int x, y, z;
vector<vector<int>> to;
vector<int> xy, yz, zx;
State(int x_, int y_, int z_, vector<vector<int>> 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<int> inner(std::vector<int> vs) {
if (vs.empty()) return {};
vs.pop_back();
swap(vs.front(), vs.back());
vs.pop_back();
return vs;
}
vector<int> enum_inner_vs() const {
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 opt_xyz() {
if (!zx_good()) return;
auto [yxnew, yznew] = TWOPATHS::twopaths(to, inner(zx), y, x, z);
xy = yxnew;
reverse(xy.begin(), xy.end());
yz = yznew;
}
void opt_yzx() {
if (!xy_good()) return;
auto [zynew, zxnew] = TWOPATHS::twopaths(to, inner(xy), z, y, x);
yz = zynew;
reverse(yz.begin(), yz.end());
zx = zxnew;
}
void opt_zxy() {
if (!yz_good()) return;
auto [xznew, xynew] = TWOPATHS::twopaths(to, inner(yz), x, z, y);
zx = xznew;
reverse(zx.begin(), zx.end());
xy = xynew;
}
void init_xyxy() {
auto [xy1, xy2] = TWOPATHS::twopaths(to, {}, x, y, y);
if (rand_int() & 1) {
xy = xy1;
} else {
xy = xy2;
}
}
vector<int> dump() const { // return [x, ..., y, ..., z, ..., x]
if (!is_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;
}
};
}; // namespace Yuki7140
#line 4 "simple_circle.hpp"
namespace SimpleCircle {
using namespace std;
int solve(int x, int y, int z, const vector<vector<int>> &to) {
auto [xy1, xy2] = TWOPATHS::twopaths(to, {}, x, y, y);
bool z_exists = false;
for (auto i : xy1) z_exists |= (i == z);
for (auto i : xy2) z_exists |= (i == z);
if (z_exists) return xy1.size() + xy2.size() - 1;
return to.size() + 1;
}
};
#line 12 "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<vector<int>> drop_graph(vector<vector<int>> to, int x, int y, int z, int k) {
const int n = to.size();
vector<int> 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<int> xyz{X, Y, Z};
int ret = N + 1;
for (int nerase = 0; nerase < 5; ++nerase) {
cerr << nerase << ':';
for (int t = 0; t < 3; ++t) {
ret = min(ret, SimpleCircle::solve(X, Y, Z, to));
for (int ntry = 0; ntry < 100; ++ntry) {
Yuki7140::State state(xyz[0], xyz[1], xyz[2], drop_graph(to, X, Y, Z, nerase));
state.init_xyxy();
state.opt_yzx();
for (int iter = 0; iter < 10; ++iter) {
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.is_feasible()) ret = min<int>(ret, state.dump().size() - 1);
cerr << ' ' << state.dump().size();
}
}
cerr << '\n';
rotate(xyz.begin(), xyz.begin() + 1, xyz.end());
}
cout << (ret <= N ? ret : -1) << '\n';
}
hitonanode