結果
| 問題 |
No.1776 Love Triangle 2 (Hard)
|
| ユーザー |
hitonanode
|
| 提出日時 | 2021-11-21 10:03:21 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 17,015 bytes |
| コンパイル時間 | 3,272 ms |
| コンパイル使用メモリ | 179,512 KB |
| 最終ジャッジ日時 | 2025-01-25 23:56:32 |
|
ジャッジサーバーID (参考情報) |
judge12 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 146 WA * 25 TLE * 5 |
ソースコード
// 嘘ヒューリスティック
// x-y, y-z, z-x のうち一つを固定して残り二つの長さの和を MCF で最小化するのを反復する山登り
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
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##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> 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 <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <typename T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &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
template <typename T, T INF = std::numeric_limits<T>::max() / 2, int INVALID = -1> struct ShortestPath {
int V, E;
bool single_positive_weight;
T wmin, wmax;
std::vector<std::vector<std::pair<int, T>>> to;
ShortestPath(int V = 0) : V(V), E(0), single_positive_weight(true), wmin(0), wmax(0), to(V) {}
void add_edge(int s, int t, T w) {
assert(0 <= s and s < V);
assert(0 <= t and t < V);
to[s].emplace_back(t, w);
E++;
if (w > 0 and wmax > 0 and wmax != w) single_positive_weight = false;
wmin = std::min(wmin, w);
wmax = std::max(wmax, w);
}
std::vector<T> dist;
std::vector<int> prev;
void ZeroOneBFS(int s) {
assert(0 <= s and s < V);
dist.assign(V, INF), prev.assign(V, INVALID);
dist[s] = 0;
std::deque<int> que;
que.push_back(s);
while (!que.empty()) {
int v = que.front();
que.pop_front();
for (auto nx : to[v]) {
T dnx = dist[v] + nx.second;
if (dist[nx.first] > dnx) {
dist[nx.first] = dnx, prev[nx.first] = v;
if (nx.second) {
que.push_back(nx.first);
} else {
que.push_front(nx.first);
}
}
}
}
}
};
#include <atcoder/mincostflow>
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 {
// x -> y -> z -> x のウォーク
int x, y, z;
vector<int> xy; // [x, ..., y] or (empty)
vector<int> yz; // [y, ..., z] or (empty)
vector<int> zx; // [z, ..., x] or (empty)
int nb_good_e() 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<int> render() const {
// 以下のいずれかを出力
// - 現状態が表す解 [x, ..., y, ..., z, ..., x] (feasible)
// - [] (infeasible)
if (!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;
}
// 状態に順序を導入
// - (feasible soluton) < (infeasible solution)
// - (shorter feasible solution) < (longer feasible solution)
// - (lexicographically smaller feasible solution) < (lexicographically greater feasible solution)
bool operator<(const State &x) const {
if (!this->feasible()) return false;
if (!x.feasible()) return true;
int len = this->length(), xlen = x.length();
if (len != xlen) return len < xlen;
return this->render() < x.render();
}
void clear() {
xy.clear();
yz.clear();
zx.clear();
}
vector<int> enum_inner_vs() const {
// 現在の状態でパス上にある,端点 (x, y, z) 以外の頂点を列挙
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 init(int x_, int y_, int z_) {
clear();
x = x_, y = y_, z = z_;
}
void rev() {
// パスの順序を逆転させる
swap(y, z);
swap(xy, zx);
reverse(xy.begin(), xy.end());
reverse(yz.begin(), yz.end());
reverse(zx.begin(), zx.end());
}
};
int N;
vector<vector<int>> to;
// used_vs に含まれる頂点は使わずに,from -> to1 と from->to2 の点素なパスを構成する.
// 両方のパスが構築できなければ empty vector の組を返す.
// already_feasible が true ならば最短パスを構築し,
// false ならばもう少しランダムに解を構築する.
pair<vector<int>, vector<int>> twopaths(const vector<int> &used_vs, bool already_feasible, int from, int to1, int to2) {
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 (!already_feasible) {
REP(i, N) {
if (rand_int() % 100 == 0) valid_v[i] = 0;
}
}
valid_v[to1] = valid_v[to2] = 1;
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]) {
int cost = 1;
if (!already_feasible) cost = rand_int() % 5 + 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<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;
conn[s] ^= t;
conn[t] ^= s; // ループがないので生えている辺の xor だけ持っておけば後で解が復元できる
}
}
vector<int> 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};
}
// a から b を経由し c に辿り着く点素なパスで,banned にあるものを使わないもののうち
// 最短で辞書順最小のものを求め,{[a, ..., b], [b, ..., c]} という pair of vector を返す.
pair<vector<int>, vector<int>> refine_path(int a, int b, int c, vector<int> banned) {
vector<int> is_banned(N);
for (auto i : banned) is_banned[i] = 1;
auto [v1, v2] = twopaths(banned, true, b, a, c);
const int len = v1.size() + v2.size();
vector<int> ab{a};
banned.push_back(a);
is_banned[a] = 1;
while (ab.back() != b) {
int cur = ab.back();
for (auto j : to[cur]) {
if (j == b) {
ab.push_back(b);
break;
}
if (is_banned[j]) continue;
if (j == c) continue;
auto [p1, p2] = twopaths(banned, true, b, j, c);
if (p1.empty()) continue;
if (int(p1.size() + p2.size() + ab.size()) != len) continue;
ab.push_back(j);
banned.push_back(j);
is_banned[j] = 1;
break;
}
}
ShortestPath<int, 1 << 20> sssp(N);
for (int i = 0; i < N; ++i) {
if (is_banned[i]) continue;
for (auto j : to[i]) {
if (is_banned[j]) continue;
sssp.add_edge(i, j, 1);
}
}
sssp.ZeroOneBFS(b);
const auto Db = sssp.dist;
sssp.ZeroOneBFS(c);
const auto Dc = sssp.dist;
vector<int> bc{b};
int cur = b;
while (cur != c) {
for (auto nxt : to[cur]) {
if (Db[nxt] == Db[cur] + 1 and Dc[nxt] == Dc[cur] - 1) {
cur = nxt;
bc.push_back(cur);
break;
}
}
}
return {ab, bc};
}
// x->y->z->x の feasible な解について, z->x には手を加えず残りを辞書順最小にする
void refine_xyz(State &state) {
if (!state.feasible()) return; // infeasible なら何もしない
const int x = state.xy[0], y = state.yz[0], z = state.zx[0];
auto [xy, yz] = refine_path(x, y, z, vector<int>(state.zx.begin() + 1, state.zx.end() - 1));
state.xy = move(xy);
state.yz = move(yz);
}
// x->y->z->x の feasible な解について,x->y には手を加えず残りを辞書順最小にする
void refine_yzx(State &state) {
if (!state.feasible()) return; // infeasible なら何もしない
const int x = state.xy[0], y = state.yz[0], z = state.zx[0];
auto [yz, zx] = refine_path(y, z, x, vector<int>(state.xy.begin() + 1, state.xy.end() - 1));
state.yz = move(yz);
state.zx = move(zx);
}
void refine(State &state) {
if (!state.feasible()) return;
refine_xyz(state);
refine_yzx(state);
auto v = state.render();
auto vrev = v;
reverse(vrev.begin(), vrev.end());
if (vrev < v) {
state.rev();
refine_xyz(state);
refine_yzx(state);
}
}
void step(State &state, int x, int y, int z) {
while (state.nb_good_e() >= 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.enum_inner_vs(), state.feasible(), 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.enum_inner_vs(), state.feasible(), 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.enum_inner_vs(), state.feasible(), z, x, y);
reverse(zy.begin(), zy.end());
state.zx = zx;
state.yz = zy;
}
}
}
int main() {
int M;
int x, y, z;
cin >> N >> M;
cin >> x >> y >> z;
--x, --y, --z;
vector conn(N, vector<int>(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, {});
int E = 0;
REP(i, N) REP(j, N) if (conn[i][j]) to[i].push_back(j), ++E;
dbg(E);
int n_iter = 10;
if (E > 5000) n_iter = 5;
State best_all;
REP(iter, n_iter) {
for (int dir = 0; dir < 2; ++dir) {
State best, state;
state.init(x, y, z);
FOR(t, 1, 31) {
step(state, x, y, z);
if (state < best) best = state;
if (t % 10 == 0) {
refine_xyz(state);
refine_yzx(state);
if (state < best) best = state;
}
}
refine(best);
if (best < best_all) best_all = best;
swap(y, z);
}
}
cout << best_all.length() << '\n';
auto path = best_all.render();
if (path.size()) {
for (auto x : path) cout << x + 1 << ' ';
cout << '\n';
}
}
hitonanode