// Validator #include "testlib.h" #include #include #include #include #include #include using namespace std; // https://epubs.siam.org/doi/10.1137/1.9781611973099.139 // Nimber (16 bit) // Reference: // - https://en.wikipedia.org/wiki/Nimber // - https://kyopro-friends.hatenablog.com/entry/2020/04/07/195850 // - https://judge.yosupo.jp/submission/4542 (implementation idea) struct Nimber { using ull = unsigned; ull v; const static std::array, 256> small_table; const static std::array, 2>, 2> precalc; explicit operator bool() const { return v != 0; } Nimber(ull val = 0) : v(val) {} Nimber operator+(const Nimber &x) const { return Nimber(v ^ x.v); } Nimber operator-(const Nimber &x) const { return Nimber(v ^ x.v); } Nimber operator-() const { return *this; } Nimber &operator+=(const Nimber &x) { return *this = *this + x; } Nimber &operator-=(const Nimber &x) { return *this = *this + x; } template friend IStream &operator>>(IStream &is, Nimber &x) { ull v; return is >> v, x = Nimber(v), is; } template friend OStream &operator<<(OStream &os, const Nimber &x) { return os << x.v; } bool operator==(const Nimber &x) const { return v == x.v; } bool operator!=(const Nimber &x) const { return v != x.v; } bool operator<(const Nimber &x) const { return v < x.v; } static ull _rec(ull x, ull y) { if (x == 0 or y == 0) return 0; if (x < y) x ^= y, y ^= x, x ^= y; // Make x >= y if (y == 1) return x; for (int shift = 16 / 2;; shift >>= 1) { ull mask = (1ULL << shift) - 1; if (y >> shift) { ull v00 = _rec(x & mask, y & mask); ull v01 = _rec(x & mask, y >> shift); ull v10 = _rec(x >> shift, y & mask); ull v11 = _rec(x >> shift, y >> shift); return v00 ^ ((v01 ^ v10 ^ v11) << shift) ^ _rec(v11, 1ULL << (shift - 1)); } else if (x >> shift) { return (_rec(x >> shift, y) << shift) ^ _rec(x & mask, y); } } } Nimber operator*(const Nimber &x) const { if (this->v < 256 and x.v < 256) return small_table[this->v][x.v]; ull ret = 0; for (int d = 0; d < 2; ++d) { // 16 bit for (int e = 0; e < 2; ++e) { // 16 bit ret ^= precalc[d][e][small_table[(v >> (d * 8)) & 255][(x.v >> (e * 8)) & 255]]; } } return Nimber(ret); } Nimber &operator*=(const Nimber &x) { return *this = *this * x; } }; const std::array, 256> Nimber::small_table = []() { std::array, 256> ret; for (int i = 0; i < 256; ++i) { for (int j = 0; j < 256; ++j) ret[i][j] = _rec(i, j); } return ret; }(); const std::array, 2>, 2> Nimber::precalc = []() { std::array, 2>, 2> ret; for (int d = 0; d < 2; ++d) { for (int e = 0; e < 2; ++e) { ull p = _rec(1ULL << (8 * d), 1ULL << (8 * e)); for (int i = 0; i < 256; ++i) ret[d][e][i] = _rec(p, i); } } return ret; }(); struct rand_int_ { using lint = long long; mt19937 mt; rand_int_() : mt(chrono::steady_clock::now().time_since_epoch().count()) {} lint operator()(lint x) { return this->operator()(0, x); } // [0, x) lint operator()(lint l, lint r) { uniform_int_distribution d(l, r - 1); return d(mt); } } rnd_generator; template int can_reach(const vector> &w, int x, int y, int z, int dlim) { // x を始点に y, z を経由し N に到達する長さ dlim 以下のパスがあるかを判定 int N = w.size() - 1; // dp[visited y][visited z][current v][previous v] vector dp(2, vector(2, vector(N + 1, vector(N + 1, 0)))); dp[0][0][N][N] = 1; for (int d = 1; d <= dlim; ++d) { vector dpnxt(2, vector(2, vector(N + 1, vector(N + 1, 0)))); for (int visy = 0; visy < 2; ++visy) { for (int visz = 0; visz < 2; ++visz) { for (int now = 0; now <= N; ++now) { Fq sum = 0; for (int prv = 0; prv <= N; ++prv) sum += dp[visy][visz][now][prv]; for (int nxt = 0; nxt <= N; ++nxt) { if (visy and (nxt == y)) continue; if (visz and (nxt == z)) continue; Fq wadd = sum - ((now == y or now == z) ? dp[visy][visz][now][nxt] : 0); dpnxt[visy | (nxt == y)][visz | (nxt == z)][nxt][now] += wadd * w[now][nxt]; } } } } dp = dpnxt; for (int i = 0; i < N; ++i) { if (dp[1][1][x][i] != 0) return d; } } return 0; // not found } #include constexpr int NMAX = 150; Nimber dp[2][2][NMAX + 1][NMAX + 1]; Nimber dpnxt[2][2][NMAX + 1][NMAX + 1]; template int next_step(const vector> &w, int h, int vy, int vz, int y, int z, int D) { // h を始点に y, z を経由し N に到達する長さ D のパスの次の一歩 int N = w.size() - 1; // dp[visited y][visited z][current v][previous v] // vector dp(2, vector(2, vector(N + 1, vector(N + 1, 0)))); memset(dp, 0, sizeof(dp)); dp[0][0][N][N] = 1; for (int d = 1; d < D; ++d) { // vector dpnxt(2, vector(2, vector(N + 1, vector(N + 1, 0)))); memset(dpnxt, 0, sizeof(dpnxt)); for (int visy = 0; visy < 2; ++visy) { for (int visz = 0; visz < 2; ++visz) { for (int now = 0; now <= N; ++now) { Fq sum = 0; for (int prv = 0; prv <= N; ++prv) sum += dp[visy][visz][now][prv]; // if (!sum) continue; // NG for (int nxt = 0; nxt <= N; ++nxt) { if (visy and (nxt == y)) continue; if (visz and (nxt == z)) continue; // Fq wadd = sum - ((now == y or now == z) ? dp[visy][visz][now][nxt] : 0); Fq wadd = sum - dp[visy][visz][now][nxt]; if (nxt == h) continue; if (!wadd or !w[now][nxt]) continue; dpnxt[visy | (nxt == y)][visz | (nxt == z)][nxt][now] += wadd * w[now][nxt]; } } } } // dp = dpnxt; swap(dp, dpnxt); } for (int i = 0; i < N; ++i) { if (!w[i][h]) continue; Fq sum = 0; for (int j = 0; j <= N; ++j) sum += dp[vy][vz][i][j]; if (!sum) continue; return i; } return N; } int main(int argc, char *argv[]) { registerValidation(argc, argv); int N = inf.readInt(1, 150); inf.readSpace(); int M = inf.readInt(3, N * (N - 1) / 2); inf.readEoln(); int x = inf.readInt(1, N); inf.readSpace(); int y = inf.readInt(1, N); inf.readSpace(); int z = inf.readInt(1, N); inf.readEoln(); assert(x < y and y < z); --x, --y, --z; vector weight(N + 1, vector(N + 1)); vector weight2(N + 1, vector(N + 1)); for (int i = 0; i < N; ++i) { for (int j = 0; j < i; ++j) { Nimber w = rnd_generator(1LL << 16); Nimber w2 = rnd_generator(1LL << 16); Nimber wn = rnd_generator(1LL << 16); Nimber wn2 = rnd_generator(1LL << 16); if (j != x) { weight[j][i] = w; weight2[j][i] = w2; } else { weight[N][i] = wn; weight2[N][i] = wn2; } if (i != x) { weight[i][j] = w; weight2[i][j] = w2; } else { weight[N][j] = wn; weight2[N][j] = wn2; } } } vector conn(N, vector(N)); for (int i = 1; i <= M; ++i) { int a = inf.readInt(1, N); inf.readSpace(); int b = inf.readInt(1, N); inf.readEoln(); assert(a < b); --a, --b; if (i == 1) assert(a == x and b == y); if (i == 2) assert(a == x and b == z); if (i == 3) assert(a == y and b == z); assert(conn[a][b] == 0); conn[a][b] = conn[b][a] = 1; weight[a][b] = weight[b][a] = 0; weight2[a][b] = weight2[b][a] = 0; if (a == x) weight[N][b] = weight2[N][b] = 0; if (b == x) weight[N][a] = weight2[N][a] = 0; } inf.readEof(); int len = can_reach(weight, x, y, z, N); int len2 = can_reach(weight2, x, y, z, N); if (!len) { puts("-1"); return 0; } cout << len << '\n'; vector path{x}; int vy = 1, vz = 1; for (int l = 1; l < len; ++l) { int cur = path.back(); if (y == cur) vy = 0; if (z == cur) vz = 0; int ok = min(next_step(weight, cur, vy, vz, y, z, len - l + 1), next_step(weight2, cur, vy, vz, y, z, len - l + 1)); path.push_back(ok); for (int i = 0; i <= N; ++i) { weight[cur][i] = weight[i][cur] = 0; weight2[cur][i] = weight2[i][cur] = 0; } } path.push_back(x); for (auto x : path) cout << x + 1 << ' '; cout << '\n'; }