結果
| 問題 |
No.1775 Love Triangle 2
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2021-11-25 23:58:17 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
CE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 10,261 bytes |
| コンパイル時間 | 761 ms |
| コンパイル使用メモリ | 90,156 KB |
| 最終ジャッジ日時 | 2025-01-26 01:17:43 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
twopaths.hpp:11:1: error: ‘uint32_t’ does not name a type twopaths.hpp:5:1: note: ‘uint32_t’ is defined in header ‘<cstdint>’; did you forget to ‘#include <cstdint>’? twopaths.hpp: In function ‘std::pair<std::vector<int>, std::vector<int> > TWOPATHS::twopaths(const std::vector<std::vector<int> >&, const std::vector<int>&, int, int, int, bool)’: twopaths.hpp:36:26: error: variable ‘std::array<int, 6> weights’ has initializer but incomplete type twopaths.hpp:41:32: error: ‘rand_int’ was not declared in this scope; did you mean ‘rand_r’? twopaths.hpp: At global scope: twopaths.hpp:83:1: error: ‘uint32_t’ does not name a type twopaths.hpp:83:1: note: ‘uint32_t’ is defined in header ‘<cstdint>’; did you forget to ‘#include <cstdint>’? twopaths.hpp: In member function ‘void Heuristic::State::init_xyxy()’: twopaths.hpp:152:13: error: ‘rand_int’ was not declared in this scope; did you mean ‘rand_r’? uso_drop.cpp: At global scope: uso_drop.cpp:19:1: error: ‘uint32_t’ does not name a type uso_drop.cpp:15:1: note: ‘uint32_t’ is defined in header ‘<cstdint>’; did you forget to ‘#include <cstdint>’? uso_drop.cpp: In function ‘std::vector<std::vector<int> > drop_graph(std::vector<std::vector<int> >, int, int, int, int)’: uso_drop.cpp:34:43: error: ‘rand_int’ was not declared in this scope; did you mean ‘rand_r’? uso_drop.cpp: In function ‘int main()’: uso_drop.cpp:62:22: error: ‘rand_int’ was not declared in this scope; did you mean ‘rand_r’?
ソースコード
// 方針(正当性なし)
// - 現在使われていない頂点のみを使い 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 <algorithm>
#include <cassert>
#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;
}
assert(mat[X][Y] + mat[Y][X] + mat[X][Z] + mat[Z][X] + mat[Y][Z] + mat[Z][Y] == 0);
vector<vector<int>> 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 <atcoder/mincostflow>
#include <cassert>
#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<int>, vector<int>> twopaths(const vector<vector<int>> &to, const vector<int> &used_vs, int from, int to1, int to2, bool randomize=false) {
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;
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<int, 6> 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<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]);
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<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;
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<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;
}
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<int> 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<vector<int>> drop_graph(vector<vector<int>> to, int x, int y, int z, int k) {
// 最大 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 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';
}
hitonanode