結果

問題 No.1776 Love Triangle 2 (Hard)
ユーザー hitonanodehitonanode
提出日時 2021-11-28 01:23:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 9,557 bytes
コンパイル時間 7,499 ms
コンパイル使用メモリ 257,600 KB
実行使用メモリ 6,080 KB
最終ジャッジ日時 2023-09-15 07:59:32
合計ジャッジ時間 259,683 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
4,696 KB
testcase_01 AC 22 ms
4,564 KB
testcase_02 AC 26 ms
4,548 KB
testcase_03 AC 28 ms
4,548 KB
testcase_04 AC 101 ms
5,168 KB
testcase_05 AC 101 ms
5,172 KB
testcase_06 AC 102 ms
5,140 KB
testcase_07 AC 102 ms
5,296 KB
testcase_08 AC 100 ms
5,160 KB
testcase_09 AC 189 ms
5,136 KB
testcase_10 AC 232 ms
5,112 KB
testcase_11 AC 114 ms
5,124 KB
testcase_12 AC 95 ms
5,168 KB
testcase_13 AC 66 ms
5,224 KB
testcase_14 AC 56 ms
5,168 KB
testcase_15 AC 46 ms
5,124 KB
testcase_16 AC 42 ms
5,172 KB
testcase_17 AC 39 ms
5,136 KB
testcase_18 AC 38 ms
5,112 KB
testcase_19 AC 167 ms
5,176 KB
testcase_20 AC 157 ms
5,164 KB
testcase_21 AC 115 ms
5,116 KB
testcase_22 AC 89 ms
5,168 KB
testcase_23 AC 67 ms
5,140 KB
testcase_24 AC 114 ms
5,148 KB
testcase_25 AC 88 ms
5,264 KB
testcase_26 AC 67 ms
5,176 KB
testcase_27 AC 88 ms
5,288 KB
testcase_28 AC 61 ms
5,180 KB
testcase_29 AC 37 ms
5,372 KB
testcase_30 AC 71 ms
5,200 KB
testcase_31 AC 202 ms
5,300 KB
testcase_32 AC 403 ms
5,376 KB
testcase_33 AC 723 ms
5,208 KB
testcase_34 AC 719 ms
5,116 KB
testcase_35 AC 1,012 ms
5,224 KB
testcase_36 AC 1,398 ms
5,200 KB
testcase_37 AC 1,839 ms
5,368 KB
testcase_38 AC 2,460 ms
5,176 KB
testcase_39 AC 51 ms
5,200 KB
testcase_40 AC 129 ms
5,200 KB
testcase_41 AC 227 ms
5,144 KB
testcase_42 AC 272 ms
5,192 KB
testcase_43 AC 284 ms
5,136 KB
testcase_44 AC 152 ms
5,160 KB
testcase_45 AC 903 ms
5,168 KB
testcase_46 AC 339 ms
5,168 KB
testcase_47 AC 80 ms
5,296 KB
testcase_48 AC 466 ms
5,364 KB
testcase_49 AC 37 ms
5,140 KB
testcase_50 AC 71 ms
5,168 KB
testcase_51 AC 197 ms
5,140 KB
testcase_52 AC 396 ms
5,136 KB
testcase_53 AC 720 ms
5,164 KB
testcase_54 AC 714 ms
5,216 KB
testcase_55 AC 999 ms
5,144 KB
testcase_56 AC 1,415 ms
5,120 KB
testcase_57 AC 1,815 ms
5,140 KB
testcase_58 AC 2,587 ms
5,168 KB
testcase_59 AC 57 ms
5,120 KB
testcase_60 AC 97 ms
5,168 KB
testcase_61 AC 158 ms
5,296 KB
testcase_62 AC 85 ms
5,312 KB
testcase_63 AC 119 ms
5,140 KB
testcase_64 AC 188 ms
5,172 KB
testcase_65 AC 67 ms
5,168 KB
testcase_66 AC 170 ms
5,260 KB
testcase_67 AC 315 ms
5,368 KB
testcase_68 AC 114 ms
5,164 KB
testcase_69 AC 303 ms
5,956 KB
testcase_70 AC 304 ms
5,800 KB
testcase_71 AC 300 ms
5,824 KB
testcase_72 AC 304 ms
5,808 KB
testcase_73 AC 301 ms
6,036 KB
testcase_74 AC 386 ms
5,804 KB
testcase_75 AC 251 ms
6,044 KB
testcase_76 AC 196 ms
6,028 KB
testcase_77 AC 161 ms
5,864 KB
testcase_78 AC 107 ms
5,836 KB
testcase_79 AC 85 ms
5,832 KB
testcase_80 AC 78 ms
5,860 KB
testcase_81 AC 60 ms
5,844 KB
testcase_82 AC 56 ms
5,852 KB
testcase_83 AC 56 ms
5,924 KB
testcase_84 AC 318 ms
5,864 KB
testcase_85 AC 233 ms
5,840 KB
testcase_86 AC 212 ms
5,880 KB
testcase_87 AC 134 ms
5,832 KB
testcase_88 AC 110 ms
5,788 KB
testcase_89 AC 197 ms
6,028 KB
testcase_90 AC 133 ms
6,024 KB
testcase_91 AC 109 ms
5,788 KB
testcase_92 AC 147 ms
5,872 KB
testcase_93 AC 98 ms
5,808 KB
testcase_94 AC 53 ms
5,856 KB
testcase_95 AC 168 ms
5,784 KB
testcase_96 AC 545 ms
5,828 KB
testcase_97 AC 1,382 ms
5,852 KB
testcase_98 AC 2,876 ms
5,924 KB
testcase_99 AC 2,901 ms
5,780 KB
testcase_100 AC 4,832 ms
6,032 KB
testcase_101 AC 6,806 ms
5,784 KB
testcase_102 AC 8,430 ms
5,776 KB
testcase_103 AC 9,855 ms
5,772 KB
testcase_104 AC 69 ms
5,956 KB
testcase_105 AC 207 ms
5,884 KB
testcase_106 AC 456 ms
5,828 KB
testcase_107 AC 158 ms
5,800 KB
testcase_108 AC 825 ms
5,788 KB
testcase_109 AC 231 ms
6,020 KB
testcase_110 AC 285 ms
5,828 KB
testcase_111 AC 351 ms
5,856 KB
testcase_112 AC 273 ms
5,776 KB
testcase_113 AC 661 ms
5,892 KB
testcase_114 AC 53 ms
6,024 KB
testcase_115 AC 164 ms
5,956 KB
testcase_116 AC 532 ms
5,800 KB
testcase_117 AC 1,342 ms
5,804 KB
testcase_118 AC 2,862 ms
5,860 KB
testcase_119 AC 2,883 ms
5,816 KB
testcase_120 AC 4,840 ms
5,808 KB
testcase_121 AC 6,686 ms
5,856 KB
testcase_122 AC 8,516 ms
5,776 KB
testcase_123 TLE -
testcase_124 AC 93 ms
5,848 KB
testcase_125 AC 116 ms
5,848 KB
testcase_126 AC 198 ms
5,828 KB
testcase_127 AC 143 ms
5,996 KB
testcase_128 AC 157 ms
5,928 KB
testcase_129 AC 154 ms
6,024 KB
testcase_130 AC 184 ms
5,828 KB
testcase_131 AC 278 ms
5,788 KB
testcase_132 AC 321 ms
5,980 KB
testcase_133 AC 365 ms
5,784 KB
testcase_134 AC 377 ms
5,124 KB
testcase_135 AC 1,423 ms
5,320 KB
testcase_136 AC 1,943 ms
5,352 KB
testcase_137 AC 2,381 ms
5,200 KB
testcase_138 AC 2,146 ms
5,144 KB
testcase_139 AC 2,559 ms
5,156 KB
testcase_140 AC 2,524 ms
5,148 KB
testcase_141 AC 1,943 ms
5,224 KB
testcase_142 AC 2,505 ms
5,228 KB
testcase_143 AC 2,463 ms
5,108 KB
testcase_144 AC 235 ms
5,300 KB
testcase_145 AC 299 ms
5,112 KB
testcase_146 AC 594 ms
5,172 KB
testcase_147 AC 478 ms
5,296 KB
testcase_148 AC 723 ms
5,176 KB
testcase_149 AC 416 ms
5,292 KB
testcase_150 AC 711 ms
5,204 KB
testcase_151 AC 302 ms
5,144 KB
testcase_152 AC 609 ms
5,116 KB
testcase_153 AC 1,473 ms
5,212 KB
testcase_154 AC 1,074 ms
5,140 KB
testcase_155 AC 6,948 ms
5,848 KB
testcase_156 AC 6,933 ms
5,868 KB
testcase_157 AC 7,035 ms
5,784 KB
testcase_158 AC 4,819 ms
5,828 KB
testcase_159 AC 8,429 ms
6,080 KB
testcase_160 AC 3,719 ms
5,828 KB
testcase_161 AC 4,681 ms
5,816 KB
testcase_162 AC 9,489 ms
6,028 KB
testcase_163 AC 9,959 ms
5,780 KB
testcase_164 AC 9,999 ms
5,920 KB
testcase_165 AC 2,975 ms
6,036 KB
testcase_166 AC 1,076 ms
5,956 KB
testcase_167 AC 1,333 ms
6,020 KB
testcase_168 AC 1,211 ms
5,816 KB
testcase_169 AC 2,368 ms
5,828 KB
testcase_170 AC 2,297 ms
5,780 KB
testcase_171 AC 4,472 ms
5,840 KB
testcase_172 AC 1,864 ms
6,028 KB
testcase_173 AC 6,315 ms
5,868 KB
testcase_174 AC 2,097 ms
5,788 KB
testcase_175 AC 5,822 ms
5,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Validator
#include "testlib.h"
#include <array>
#include <cassert>
#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_generator;

template <class Fq>
int can_reach(const vector<vector<Fq>> &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<Fq>(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<Fq>(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 <cstring>
constexpr int NMAX = 150;
Nimber dp[2][2][NMAX + 1][NMAX + 1];
Nimber dpnxt[2][2][NMAX + 1][NMAX + 1];

template <class Fq>
int next_step(const vector<vector<Fq>> &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<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) {
                    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<Nimber>(N + 1));
    vector weight2(N + 1, vector<Nimber>(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<int>(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<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 = 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';
}
0