結果
問題 | No.1776 Love Triangle 2 (Hard) |
ユーザー | hitonanode |
提出日時 | 2021-11-21 10:03:21 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 17,015 bytes |
コンパイル時間 | 2,889 ms |
コンパイル使用メモリ | 184,224 KB |
実行使用メモリ | 11,472 KB |
最終ジャッジ日時 | 2024-07-02 12:15:44 |
合計ジャッジ時間 | 118,397 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
11,472 KB |
testcase_01 | AC | 4 ms
5,376 KB |
testcase_02 | AC | 7 ms
5,376 KB |
testcase_03 | AC | 5 ms
5,376 KB |
testcase_04 | AC | 447 ms
5,376 KB |
testcase_05 | AC | 435 ms
5,376 KB |
testcase_06 | AC | 451 ms
5,376 KB |
testcase_07 | AC | 356 ms
5,376 KB |
testcase_08 | AC | 576 ms
5,376 KB |
testcase_09 | AC | 53 ms
5,376 KB |
testcase_10 | AC | 58 ms
5,376 KB |
testcase_11 | AC | 95 ms
5,376 KB |
testcase_12 | AC | 78 ms
5,376 KB |
testcase_13 | AC | 110 ms
5,376 KB |
testcase_14 | AC | 276 ms
5,376 KB |
testcase_15 | AC | 577 ms
5,376 KB |
testcase_16 | AC | 677 ms
5,376 KB |
testcase_17 | AC | 1,812 ms
5,376 KB |
testcase_18 | AC | 3,637 ms
5,376 KB |
testcase_19 | AC | 81 ms
5,376 KB |
testcase_20 | AC | 92 ms
5,376 KB |
testcase_21 | AC | 88 ms
5,376 KB |
testcase_22 | AC | 129 ms
5,376 KB |
testcase_23 | AC | 119 ms
5,376 KB |
testcase_24 | AC | 89 ms
5,376 KB |
testcase_25 | AC | 130 ms
5,376 KB |
testcase_26 | AC | 125 ms
5,376 KB |
testcase_27 | AC | 95 ms
5,376 KB |
testcase_28 | AC | 225 ms
5,376 KB |
testcase_29 | AC | 3,901 ms
5,376 KB |
testcase_30 | AC | 3,640 ms
5,376 KB |
testcase_31 | AC | 1,586 ms
5,376 KB |
testcase_32 | AC | 3,494 ms
5,376 KB |
testcase_33 | AC | 3,314 ms
5,376 KB |
testcase_34 | AC | 1,721 ms
5,376 KB |
testcase_35 | AC | 1,287 ms
5,376 KB |
testcase_36 | AC | 629 ms
5,376 KB |
testcase_37 | AC | 232 ms
5,376 KB |
testcase_38 | AC | 112 ms
5,376 KB |
testcase_39 | AC | 7,944 ms
5,376 KB |
testcase_40 | AC | 3,970 ms
5,376 KB |
testcase_41 | AC | 1,651 ms
5,376 KB |
testcase_42 | AC | 2,719 ms
5,376 KB |
testcase_43 | AC | 1,554 ms
5,376 KB |
testcase_44 | AC | 1,247 ms
5,376 KB |
testcase_45 | WA | - |
testcase_46 | AC | 984 ms
5,376 KB |
testcase_47 | AC | 150 ms
5,376 KB |
testcase_48 | AC | 56 ms
5,376 KB |
testcase_49 | AC | 3,090 ms
5,376 KB |
testcase_50 | AC | 3,107 ms
5,376 KB |
testcase_51 | AC | 636 ms
5,376 KB |
testcase_52 | AC | 1,975 ms
5,376 KB |
testcase_53 | AC | 959 ms
5,376 KB |
testcase_54 | AC | 405 ms
5,376 KB |
testcase_55 | AC | 720 ms
5,376 KB |
testcase_56 | AC | 305 ms
5,376 KB |
testcase_57 | AC | 94 ms
5,376 KB |
testcase_58 | AC | 43 ms
5,376 KB |
testcase_59 | AC | 4,507 ms
5,376 KB |
testcase_60 | AC | 3,029 ms
5,376 KB |
testcase_61 | AC | 1,149 ms
5,376 KB |
testcase_62 | AC | 2,965 ms
5,376 KB |
testcase_63 | AC | 993 ms
5,376 KB |
testcase_64 | AC | 1,015 ms
5,376 KB |
testcase_65 | AC | 552 ms
5,376 KB |
testcase_66 | AC | 164 ms
5,376 KB |
testcase_67 | AC | 84 ms
5,376 KB |
testcase_68 | AC | 61 ms
5,376 KB |
testcase_69 | AC | 1,308 ms
6,112 KB |
testcase_70 | AC | 1,321 ms
6,148 KB |
testcase_71 | AC | 1,348 ms
5,984 KB |
testcase_72 | AC | 369 ms
5,376 KB |
testcase_73 | AC | 1,326 ms
6,228 KB |
testcase_74 | AC | 121 ms
5,376 KB |
testcase_75 | AC | 108 ms
5,376 KB |
testcase_76 | AC | 147 ms
5,376 KB |
testcase_77 | AC | 171 ms
5,376 KB |
testcase_78 | AC | 368 ms
5,376 KB |
testcase_79 | AC | 603 ms
5,376 KB |
testcase_80 | AC | 791 ms
5,376 KB |
testcase_81 | AC | 2,381 ms
5,376 KB |
testcase_82 | AC | 3,089 ms
5,376 KB |
testcase_83 | AC | 7,402 ms
5,936 KB |
testcase_84 | AC | 117 ms
5,376 KB |
testcase_85 | AC | 152 ms
5,376 KB |
testcase_86 | AC | 135 ms
5,376 KB |
testcase_87 | AC | 165 ms
5,376 KB |
testcase_88 | AC | 559 ms
5,376 KB |
testcase_89 | AC | 165 ms
5,376 KB |
testcase_90 | AC | 294 ms
5,376 KB |
testcase_91 | AC | 376 ms
5,376 KB |
testcase_92 | AC | 174 ms
5,376 KB |
testcase_93 | AC | 500 ms
5,376 KB |
testcase_94 | TLE | - |
testcase_95 | -- | - |
testcase_96 | -- | - |
testcase_97 | -- | - |
testcase_98 | -- | - |
testcase_99 | -- | - |
testcase_100 | -- | - |
testcase_101 | -- | - |
testcase_102 | -- | - |
testcase_103 | -- | - |
testcase_104 | -- | - |
testcase_105 | -- | - |
testcase_106 | -- | - |
testcase_107 | -- | - |
testcase_108 | -- | - |
testcase_109 | -- | - |
testcase_110 | -- | - |
testcase_111 | -- | - |
testcase_112 | -- | - |
testcase_113 | -- | - |
testcase_114 | -- | - |
testcase_115 | -- | - |
testcase_116 | -- | - |
testcase_117 | -- | - |
testcase_118 | -- | - |
testcase_119 | -- | - |
testcase_120 | -- | - |
testcase_121 | -- | - |
testcase_122 | -- | - |
testcase_123 | -- | - |
testcase_124 | -- | - |
testcase_125 | -- | - |
testcase_126 | -- | - |
testcase_127 | -- | - |
testcase_128 | -- | - |
testcase_129 | -- | - |
testcase_130 | -- | - |
testcase_131 | -- | - |
testcase_132 | -- | - |
testcase_133 | -- | - |
testcase_134 | -- | - |
testcase_135 | -- | - |
testcase_136 | -- | - |
testcase_137 | -- | - |
testcase_138 | -- | - |
testcase_139 | -- | - |
testcase_140 | -- | - |
testcase_141 | -- | - |
testcase_142 | -- | - |
testcase_143 | -- | - |
testcase_144 | -- | - |
testcase_145 | -- | - |
testcase_146 | -- | - |
testcase_147 | -- | - |
testcase_148 | -- | - |
testcase_149 | -- | - |
testcase_150 | -- | - |
testcase_151 | -- | - |
testcase_152 | -- | - |
testcase_153 | -- | - |
testcase_154 | -- | - |
testcase_155 | -- | - |
testcase_156 | -- | - |
testcase_157 | -- | - |
testcase_158 | -- | - |
testcase_159 | -- | - |
testcase_160 | -- | - |
testcase_161 | -- | - |
testcase_162 | -- | - |
testcase_163 | -- | - |
testcase_164 | -- | - |
testcase_165 | -- | - |
testcase_166 | -- | - |
testcase_167 | -- | - |
testcase_168 | -- | - |
testcase_169 | -- | - |
testcase_170 | -- | - |
testcase_171 | -- | - |
testcase_172 | -- | - |
testcase_173 | -- | - |
testcase_174 | -- | - |
testcase_175 | -- | - |
ソースコード
// 嘘ヒューリスティック // 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'; } }