結果

問題 No.1775 Love Triangle 2
ユーザー 👑 hitonanodehitonanode
提出日時 2021-11-20 02:21:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 486 ms / 8,000 ms
コード長 5,695 bytes
コンパイル時間 2,400 ms
コンパイル使用メモリ 144,428 KB
実行使用メモリ 4,964 KB
最終ジャッジ日時 2023-09-16 22:11:48
合計ジャッジ時間 12,681 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
4,376 KB
testcase_01 AC 32 ms
4,376 KB
testcase_02 AC 32 ms
4,376 KB
testcase_03 AC 32 ms
4,376 KB
testcase_04 AC 465 ms
4,964 KB
testcase_05 AC 486 ms
4,736 KB
testcase_06 AC 486 ms
4,872 KB
testcase_07 AC 214 ms
4,560 KB
testcase_08 AC 466 ms
4,796 KB
testcase_09 AC 41 ms
4,740 KB
testcase_10 AC 42 ms
4,648 KB
testcase_11 AC 40 ms
4,632 KB
testcase_12 AC 40 ms
4,656 KB
testcase_13 AC 41 ms
4,376 KB
testcase_14 AC 41 ms
4,768 KB
testcase_15 AC 43 ms
4,504 KB
testcase_16 AC 42 ms
4,700 KB
testcase_17 AC 44 ms
4,712 KB
testcase_18 AC 54 ms
4,736 KB
testcase_19 AC 41 ms
4,644 KB
testcase_20 AC 41 ms
4,652 KB
testcase_21 AC 40 ms
4,668 KB
testcase_22 AC 41 ms
4,524 KB
testcase_23 AC 41 ms
4,488 KB
testcase_24 AC 40 ms
4,732 KB
testcase_25 AC 41 ms
4,480 KB
testcase_26 AC 41 ms
4,668 KB
testcase_27 AC 41 ms
4,456 KB
testcase_28 AC 42 ms
4,496 KB
testcase_29 AC 58 ms
4,716 KB
testcase_30 AC 76 ms
4,700 KB
testcase_31 AC 101 ms
4,672 KB
testcase_32 AC 111 ms
4,816 KB
testcase_33 AC 107 ms
4,556 KB
testcase_34 AC 106 ms
4,724 KB
testcase_35 AC 98 ms
4,832 KB
testcase_36 AC 81 ms
4,680 KB
testcase_37 AC 75 ms
4,764 KB
testcase_38 AC 66 ms
4,496 KB
testcase_39 AC 66 ms
4,712 KB
testcase_40 AC 88 ms
4,960 KB
testcase_41 AC 97 ms
4,768 KB
testcase_42 AC 89 ms
4,580 KB
testcase_43 AC 77 ms
4,584 KB
testcase_44 AC 65 ms
4,672 KB
testcase_45 AC 95 ms
4,564 KB
testcase_46 AC 62 ms
4,556 KB
testcase_47 AC 42 ms
4,508 KB
testcase_48 AC 51 ms
4,380 KB
testcase_49 AC 55 ms
4,816 KB
testcase_50 AC 75 ms
4,860 KB
testcase_51 AC 99 ms
4,656 KB
testcase_52 AC 107 ms
4,928 KB
testcase_53 AC 105 ms
4,516 KB
testcase_54 AC 105 ms
4,708 KB
testcase_55 AC 98 ms
4,532 KB
testcase_56 AC 82 ms
4,840 KB
testcase_57 AC 78 ms
4,608 KB
testcase_58 AC 72 ms
4,652 KB
testcase_59 AC 69 ms
4,704 KB
testcase_60 AC 79 ms
4,672 KB
testcase_61 AC 84 ms
4,588 KB
testcase_62 AC 60 ms
4,848 KB
testcase_63 AC 59 ms
4,560 KB
testcase_64 AC 60 ms
4,700 KB
testcase_65 AC 43 ms
4,452 KB
testcase_66 AC 47 ms
4,508 KB
testcase_67 AC 48 ms
4,496 KB
testcase_68 AC 41 ms
4,432 KB
testcase_69 AC 47 ms
4,500 KB
testcase_70 AC 61 ms
4,656 KB
testcase_71 AC 65 ms
4,468 KB
testcase_72 AC 69 ms
4,500 KB
testcase_73 AC 67 ms
4,612 KB
testcase_74 AC 69 ms
4,604 KB
testcase_75 AC 60 ms
4,712 KB
testcase_76 AC 64 ms
4,424 KB
testcase_77 AC 68 ms
4,448 KB
testcase_78 AC 68 ms
4,472 KB
testcase_79 AC 46 ms
4,660 KB
testcase_80 AC 47 ms
4,504 KB
testcase_81 AC 54 ms
4,496 KB
testcase_82 AC 52 ms
4,488 KB
testcase_83 AC 57 ms
4,572 KB
testcase_84 AC 51 ms
4,492 KB
testcase_85 AC 56 ms
4,592 KB
testcase_86 AC 48 ms
4,652 KB
testcase_87 AC 54 ms
4,500 KB
testcase_88 AC 67 ms
4,624 KB
testcase_89 AC 57 ms
4,512 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 (64 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 long long;
    ull v;
    const static std::array<std::array<unsigned, 256>, 256> small_table;
    const static std::array<std::array<std::array<ull, 256>, 8>, 8> 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 = 64 / 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 < 8; ++d) {
            for (int e = 0; e < 8; ++e) { 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 long long, 256>, 8>, 8> Nimber::precalc = []() {
    std::array<std::array<std::array<unsigned long long, 256>, 8>, 8> ret;
    for (int d = 0; d < 8; ++d) {
        for (int e = 0; e < 8; ++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 main() {
    int N, M;
    cin >> N >> M;
    int x, y, z;
    cin >> x >> y >> z;

    --x, --y, --z;
    vector<vector<pair<int, Nimber>>> to(N);

    std::vector exist_edge(N, std::vector<int>(N, 1));
    while (M--) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        exist_edge[a][b] = exist_edge[b][a] = 0;
    }
    for (int a = 0; a < N; ++a) {
        for (int b = 0; b < N; ++b) {
            if (a >= b or !exist_edge[a][b]) continue;
            Nimber w = rnd(1LL << 62);
            Nimber wn = rnd(1LL << 62);
            for (int t = 0; t < 2; ++t) {
                // to[a].emplace_back(b, w);
                if (b != x) to[a].emplace_back(b, w);
                if (b == x) to[a].emplace_back(N, wn);
                swap(a, b);
            }
        }
    }

    // dp[visited y flag][visited z flag][length][now][prv]
    vector dp(2, vector(2, vector(N + 1, vector<Nimber>(N + 1))));
    dp[0][0][x][N] = 1;
    for (int t = 0; t < N; ++t) {
        vector dpnxt(2, vector(2, vector(N + 1, vector<Nimber>(N + 1))));
        for (int visy = 0; visy < 2; ++visy) {
            for (int visz = 0; visz < 2; ++visz) {
                for (int now = 0; now < N; ++now) {
                    Nimber cur = 0;
                    for (int prv = 0; prv <= N; ++prv) cur += dp[visy][visz][now][prv];
                    for (auto [nxt, w] : to[now]) {
                        if (nxt == y and visy) continue;
                        if (nxt == z and visz) continue;
                        dpnxt[visy | (y == nxt)][visz | (z == nxt)][nxt][now] += (cur - dp[visy][visz][now][nxt]) * w;
                        // dpnxt[visy | (y == nxt)][visz | (z == nxt)][nxt][now] += (cur - ((now == y or now == z) ? dp[visy][visz][now][nxt] : 0)) * w;
                    }
                }
            }
        }
        dp = dpnxt;
        for (int i = 0; i < N; ++i) {
            if (dp[1][1][N][i]) {
                cout << t + 1 << '\n';
                // break;
                return 0;
            }
        }
    }
    puts("-1");
}
0