結果

問題 No.1776 Love Triangle 2 (Hard)
ユーザー hitonanodehitonanode
提出日時 2021-11-20 01:12:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 8,195 bytes
コンパイル時間 2,439 ms
コンパイル使用メモリ 145,396 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-07-02 12:04:16
合計ジャッジ時間 77,642 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
5,248 KB
testcase_01 AC 23 ms
5,376 KB
testcase_02 AC 26 ms
5,376 KB
testcase_03 AC 28 ms
5,376 KB
testcase_04 AC 64 ms
5,632 KB
testcase_05 AC 64 ms
5,632 KB
testcase_06 AC 65 ms
5,760 KB
testcase_07 AC 66 ms
5,760 KB
testcase_08 AC 64 ms
5,760 KB
testcase_09 AC 94 ms
5,760 KB
testcase_10 AC 113 ms
5,504 KB
testcase_11 AC 65 ms
5,632 KB
testcase_12 AC 56 ms
5,760 KB
testcase_13 AC 45 ms
5,504 KB
testcase_14 AC 40 ms
5,760 KB
testcase_15 AC 36 ms
5,760 KB
testcase_16 AC 33 ms
5,760 KB
testcase_17 AC 32 ms
5,632 KB
testcase_18 AC 31 ms
5,504 KB
testcase_19 AC 87 ms
5,632 KB
testcase_20 AC 84 ms
5,632 KB
testcase_21 AC 66 ms
5,632 KB
testcase_22 AC 54 ms
5,632 KB
testcase_23 AC 45 ms
5,632 KB
testcase_24 AC 65 ms
5,632 KB
testcase_25 AC 55 ms
5,504 KB
testcase_26 AC 45 ms
5,632 KB
testcase_27 AC 54 ms
5,632 KB
testcase_28 AC 42 ms
5,760 KB
testcase_29 AC 32 ms
5,632 KB
testcase_30 AC 50 ms
5,504 KB
testcase_31 AC 115 ms
5,760 KB
testcase_32 AC 216 ms
5,504 KB
testcase_33 AC 356 ms
5,760 KB
testcase_34 AC 357 ms
5,888 KB
testcase_35 AC 474 ms
5,760 KB
testcase_36 AC 631 ms
5,888 KB
testcase_37 AC 796 ms
5,504 KB
testcase_38 AC 966 ms
5,760 KB
testcase_39 AC 38 ms
5,632 KB
testcase_40 AC 80 ms
5,632 KB
testcase_41 AC 129 ms
5,504 KB
testcase_42 AC 148 ms
5,760 KB
testcase_43 AC 152 ms
5,760 KB
testcase_44 AC 87 ms
5,632 KB
testcase_45 AC 448 ms
5,632 KB
testcase_46 AC 168 ms
5,760 KB
testcase_47 AC 50 ms
5,632 KB
testcase_48 RE -
testcase_49 AC 31 ms
5,632 KB
testcase_50 AC 49 ms
5,760 KB
testcase_51 AC 114 ms
5,632 KB
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 AC 41 ms
5,632 KB
testcase_60 AC 63 ms
5,760 KB
testcase_61 AC 94 ms
5,760 KB
testcase_62 AC 55 ms
5,504 KB
testcase_63 AC 70 ms
5,760 KB
testcase_64 AC 103 ms
5,504 KB
testcase_65 AC 45 ms
5,888 KB
testcase_66 AC 90 ms
5,632 KB
testcase_67 AC 152 ms
5,632 KB
testcase_68 AC 64 ms
5,760 KB
testcase_69 AC 169 ms
6,144 KB
testcase_70 AC 170 ms
6,400 KB
testcase_71 AC 170 ms
6,144 KB
testcase_72 AC 172 ms
6,272 KB
testcase_73 AC 170 ms
6,272 KB
testcase_74 AC 145 ms
6,144 KB
testcase_75 AC 104 ms
6,272 KB
testcase_76 AC 87 ms
6,272 KB
testcase_77 AC 76 ms
6,016 KB
testcase_78 AC 58 ms
6,144 KB
testcase_79 AC 51 ms
6,272 KB
testcase_80 AC 48 ms
6,400 KB
testcase_81 AC 42 ms
6,272 KB
testcase_82 AC 40 ms
6,016 KB
testcase_83 AC 39 ms
6,016 KB
testcase_84 AC 127 ms
6,144 KB
testcase_85 AC 100 ms
6,016 KB
testcase_86 AC 92 ms
6,144 KB
testcase_87 AC 67 ms
6,400 KB
testcase_88 AC 59 ms
6,016 KB
testcase_89 AC 89 ms
6,016 KB
testcase_90 AC 67 ms
6,272 KB
testcase_91 AC 59 ms
6,016 KB
testcase_92 AC 73 ms
6,016 KB
testcase_93 AC 56 ms
6,144 KB
testcase_94 AC 39 ms
6,016 KB
testcase_95 AC 94 ms
6,272 KB
testcase_96 AC 271 ms
6,144 KB
testcase_97 AC 623 ms
6,144 KB
testcase_98 AC 1,129 ms
6,144 KB
testcase_99 AC 1,121 ms
6,272 KB
testcase_100 AC 1,725 ms
6,144 KB
testcase_101 AC 2,243 ms
6,144 KB
testcase_102 AC 2,581 ms
6,400 KB
testcase_103 AC 2,617 ms
6,016 KB
testcase_104 AC 46 ms
6,400 KB
testcase_105 AC 113 ms
6,144 KB
testcase_106 AC 232 ms
6,016 KB
testcase_107 AC 84 ms
6,272 KB
testcase_108 AC 336 ms
6,016 KB
testcase_109 AC 108 ms
6,272 KB
testcase_110 AC 130 ms
6,272 KB
testcase_111 AC 146 ms
6,272 KB
testcase_112 AC 113 ms
6,144 KB
testcase_113 AC 236 ms
6,272 KB
testcase_114 AC 39 ms
6,272 KB
testcase_115 RE -
testcase_116 AC 277 ms
6,272 KB
testcase_117 RE -
testcase_118 RE -
testcase_119 RE -
testcase_120 RE -
testcase_121 RE -
testcase_122 RE -
testcase_123 RE -
testcase_124 AC 58 ms
6,144 KB
testcase_125 AC 68 ms
6,272 KB
testcase_126 AC 104 ms
6,272 KB
testcase_127 AC 79 ms
6,144 KB
testcase_128 AC 81 ms
6,144 KB
testcase_129 AC 78 ms
6,272 KB
testcase_130 AC 89 ms
6,272 KB
testcase_131 AC 118 ms
6,144 KB
testcase_132 AC 130 ms
6,272 KB
testcase_133 AC 140 ms
6,144 KB
testcase_134 AC 170 ms
5,504 KB
testcase_135 RE -
testcase_136 AC 786 ms
5,632 KB
testcase_137 AC 941 ms
5,632 KB
testcase_138 AC 844 ms
5,504 KB
testcase_139 AC 1,043 ms
5,760 KB
testcase_140 AC 974 ms
5,632 KB
testcase_141 AC 771 ms
5,632 KB
testcase_142 AC 973 ms
5,760 KB
testcase_143 AC 980 ms
5,504 KB
testcase_144 AC 119 ms
5,760 KB
testcase_145 RE -
testcase_146 RE -
testcase_147 RE -
testcase_148 RE -
testcase_149 RE -
testcase_150 RE -
testcase_151 RE -
testcase_152 RE -
testcase_153 RE -
testcase_154 AC 446 ms
5,632 KB
testcase_155 AC 1,865 ms
6,144 KB
testcase_156 RE -
testcase_157 AC 1,876 ms
6,144 KB
testcase_158 AC 1,304 ms
6,144 KB
testcase_159 AC 2,218 ms
6,272 KB
testcase_160 AC 1,033 ms
6,144 KB
testcase_161 AC 1,309 ms
6,144 KB
testcase_162 AC 2,548 ms
6,144 KB
testcase_163 AC 2,604 ms
6,272 KB
testcase_164 AC 2,621 ms
6,400 KB
testcase_165 RE -
testcase_166 AC 394 ms
6,272 KB
testcase_167 RE -
testcase_168 RE -
testcase_169 RE -
testcase_170 RE -
testcase_171 RE -
testcase_172 RE -
testcase_173 RE -
testcase_174 RE -
testcase_175 AC 1,599 ms
6,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
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<std::array<ull, 256>, 256> small_table;
    const static std::array<std::array<std::array<ull, 256>, 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 <class IStream> friend IStream &operator>>(IStream &is, Nimber &x) {
        ull v;
        return is >> v, x = Nimber(v), is;
    }
    template <class OStream> 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<std::array<unsigned, 256>, 256> Nimber::small_table = []() {
    std::array<std::array<unsigned, 256>, 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<std::array<std::array<unsigned, 256>, 2>, 2> Nimber::precalc = []() {
    std::array<std::array<std::array<unsigned, 256>, 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<lint> d(l, r - 1);
        return d(mt);
    }
} rnd;

int N;
#include <cstring>
constexpr int NMAX = 200;
Nimber dp[2][2][NMAX + 1][NMAX + 1];
Nimber dpnxt[2][2][NMAX + 1][NMAX + 1];
Nimber weight[NMAX + 1][NMAX + 1];

int can_reach(int x, int y, int z, int dlim) {
    // x を始点に y, z を経由し N に到達する長さ dlim 以下のパスがあるかを判定
    // dp[visited y][visited z][current v][previous v]
    vector dp(2, vector(2, vector(N + 1, vector<Nimber>(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<Nimber>(N + 1, 0))));
        for (int visy = 0; visy < 2; ++visy) {
            for (int visz = 0; visz < 2; ++visz) {
                for (int now = 0; now <= N; ++now) {
                    Nimber 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;
                        Nimber wadd = sum - ((now == y or now == z) ? dp[visy][visz][now][nxt] : 0);
                        dpnxt[visy | (nxt == y)][visz | (nxt == z)][nxt][now] += wadd * weight[now][nxt];
                    }
                }
            }
        }
        dp = dpnxt;
        for (int i = 0; i < N; ++i) {
            if (dp[1][1][x][i] != 0) return d;
        }
    }
    return 0;  // not found
}

int next_step(int h, int vy, int vz, int y, int z, int D) {
    // h を始点に y, z を経由し N に到達する長さ D のパスの次の一歩
    // dp[visited y][visited z][current v][previous v]
    // vector dp(2, vector(2, vector(N + 1, vector<Fq>(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<Fq>(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) {
                    Nimber sum = 0;
                    for (int prv = 0; prv <= N; ++prv) sum += dp[visy][visz][now][prv];
                    if (!sum) continue;
                    for (int nxt = 0; nxt <= N; ++nxt) {
                        if (visy and (nxt == y)) continue;
                        if (visz and (nxt == z)) continue;
                        if (nxt == h) continue;
                        if (!weight[now][nxt]) continue;
                        // Nimber wadd = sum - ((now == y or now == z) ? dp[visy][visz][now][nxt] : 0);
                        Nimber wadd = sum - dp[visy][visz][now][nxt];
                        if (!wadd) continue;
                        dpnxt[visy | (nxt == y)][visz | (nxt == z)][nxt][now] += wadd * weight[now][nxt];
                    }
                }
            }
        }
        // dp = dpnxt;
        swap(dp, dpnxt);
    }
    for (int i = 0; i < N; ++i) {
        if (!weight[i][h]) continue;
        Nimber sum = 0;
        for (int j = 0; j <= N; ++j) sum += dp[vy][vz][i][j];
        if (!sum) continue;
        return i;
    }
    throw;
}

int main() {
    int M;
    cin >> N >> M;
    int x, y, z;
    cin >> x >> y >> z;

    --x, --y, --z;
    // vector weight(N + 1, vector<Nimber>(N + 1));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < i; ++j) {
            Nimber w = rnd(1LL << 16);
            Nimber wn = rnd(1LL << 16);
            if (j != x) {
                weight[j][i] = w;
            } else {
                weight[N][i] = wn;
            }
            if (i != x) {
                weight[i][j] = w;
            } else {
                weight[N][j] = wn;
            }
        }
    }

    while (M--) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        weight[a][b] = weight[b][a] = 0;
        if (a == x) weight[N][b] = 0;
        if (b == x) weight[N][a] = 0;
    }
    int len = can_reach(x, y, z, N);
    if (!len) {
        puts("-1");
        return 0;
    }
    cout << len << '\n';

    vector<int> 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 = next_step(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;
    }

    path.push_back(x);

    for (auto x : path) cout << x + 1 << ' ';
    cout << '\n';
}
0