結果

問題 No.1775 Love Triangle 2
ユーザー 👑 hitonanodehitonanode
提出日時 2021-12-04 18:07:59
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 8,000 ms
コード長 8,003 bytes
コンパイル時間 3,253 ms
コンパイル使用メモリ 99,232 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-21 06:04:44
合計ジャッジ時間 10,146 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 29 ms
4,380 KB
testcase_05 AC 29 ms
4,384 KB
testcase_06 AC 29 ms
4,384 KB
testcase_07 AC 25 ms
4,380 KB
testcase_08 AC 27 ms
4,376 KB
testcase_09 AC 27 ms
4,376 KB
testcase_10 AC 31 ms
4,376 KB
testcase_11 AC 35 ms
4,380 KB
testcase_12 AC 35 ms
4,376 KB
testcase_13 AC 42 ms
4,380 KB
testcase_14 AC 45 ms
4,376 KB
testcase_15 AC 47 ms
4,376 KB
testcase_16 AC 47 ms
4,376 KB
testcase_17 AC 48 ms
4,376 KB
testcase_18 AC 45 ms
4,380 KB
testcase_19 AC 30 ms
4,380 KB
testcase_20 AC 32 ms
4,380 KB
testcase_21 AC 34 ms
4,380 KB
testcase_22 AC 38 ms
4,380 KB
testcase_23 AC 40 ms
4,380 KB
testcase_24 AC 32 ms
4,384 KB
testcase_25 AC 39 ms
4,376 KB
testcase_26 AC 43 ms
4,388 KB
testcase_27 AC 38 ms
4,380 KB
testcase_28 AC 44 ms
4,376 KB
testcase_29 AC 49 ms
4,380 KB
testcase_30 AC 47 ms
4,384 KB
testcase_31 AC 42 ms
4,376 KB
testcase_32 AC 40 ms
4,384 KB
testcase_33 AC 35 ms
4,380 KB
testcase_34 AC 34 ms
4,380 KB
testcase_35 AC 32 ms
4,380 KB
testcase_36 AC 29 ms
4,380 KB
testcase_37 AC 27 ms
4,380 KB
testcase_38 AC 25 ms
4,380 KB
testcase_39 AC 45 ms
4,384 KB
testcase_40 AC 45 ms
4,380 KB
testcase_41 AC 42 ms
4,380 KB
testcase_42 AC 40 ms
4,376 KB
testcase_43 AC 36 ms
4,380 KB
testcase_44 AC 37 ms
4,380 KB
testcase_45 AC 32 ms
4,376 KB
testcase_46 AC 32 ms
4,380 KB
testcase_47 AC 31 ms
4,380 KB
testcase_48 AC 27 ms
4,376 KB
testcase_49 AC 49 ms
4,376 KB
testcase_50 AC 48 ms
4,380 KB
testcase_51 AC 42 ms
4,380 KB
testcase_52 AC 40 ms
4,376 KB
testcase_53 AC 35 ms
4,376 KB
testcase_54 AC 36 ms
4,376 KB
testcase_55 AC 34 ms
4,380 KB
testcase_56 AC 33 ms
4,376 KB
testcase_57 AC 31 ms
4,380 KB
testcase_58 AC 29 ms
4,376 KB
testcase_59 AC 45 ms
4,380 KB
testcase_60 AC 45 ms
4,380 KB
testcase_61 AC 42 ms
4,376 KB
testcase_62 AC 43 ms
4,380 KB
testcase_63 AC 38 ms
4,376 KB
testcase_64 AC 40 ms
4,380 KB
testcase_65 AC 36 ms
4,380 KB
testcase_66 AC 33 ms
4,384 KB
testcase_67 AC 31 ms
4,380 KB
testcase_68 AC 34 ms
4,380 KB
testcase_69 AC 29 ms
4,380 KB
testcase_70 AC 27 ms
4,380 KB
testcase_71 AC 27 ms
4,384 KB
testcase_72 AC 27 ms
4,380 KB
testcase_73 AC 28 ms
4,380 KB
testcase_74 AC 27 ms
4,380 KB
testcase_75 AC 26 ms
4,380 KB
testcase_76 AC 27 ms
4,380 KB
testcase_77 AC 26 ms
4,376 KB
testcase_78 AC 28 ms
4,376 KB
testcase_79 AC 31 ms
4,380 KB
testcase_80 AC 32 ms
4,384 KB
testcase_81 AC 31 ms
4,376 KB
testcase_82 AC 34 ms
4,376 KB
testcase_83 AC 33 ms
4,384 KB
testcase_84 AC 32 ms
4,376 KB
testcase_85 AC 31 ms
4,384 KB
testcase_86 AC 33 ms
4,384 KB
testcase_87 AC 30 ms
4,380 KB
testcase_88 AC 31 ms
4,380 KB
testcase_89 AC 27 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// O(n^3) weighted-linear-matroid-parity-based solution
#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;

#include <atcoder/modint>
using mint = atcoder::static_modint<(1 << 30) + 3>;

uint32_t rand_int() {
    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));
}

tuple<int, int, int, int, vector<vector<int>>> read_input() {
    int N, M, X, Y, Z;
    cin >> N >> M >> X >> Y >> Z;
    --X, --Y, --Z;
    vector mat(N, vector<int>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) mat[i][j] = (i != j);
    }
    while (M--) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        mat[a][b] = mat[b][a] = 0;
    }
    vector<vector<int>> to(N);
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            if (mat[i][j]) to[i].push_back(j);
        }
    }
    return {N, X, Y, Z, to};
}

// Upper Hessenberg reduction of square matrices
// Complexity: O(n^3)
// Reference:
// http://www.phys.uri.edu/nigh/NumRec/bookfpdf/f11-5.pdf
template <class Tp> void hessenberg_reduction(std::vector<std::vector<Tp>> &M) {
    assert(M.size() == M[0].size());
    const int N = M.size();
    for (int r = 0; r < N - 2; r++) {
        int piv = -1;
        for (int j = r + 1; j < N; ++j) if (M[j][r] != 0) {
            piv = j;
            break;
        }
        if (piv < 0) continue;
        for (int i = 0; i < N; i++) std::swap(M[r + 1][i], M[piv][i]);
        for (int i = 0; i < N; i++) std::swap(M[i][r + 1], M[i][piv]);

        const auto rinv = Tp(1) / M[r + 1][r];
        for (int i = r + 2; i < N; i++) {
            const auto n = M[i][r] * rinv;
            for (int j = 0; j < N; j++) M[i][j] -= M[r + 1][j] * n;
            for (int j = 0; j < N; j++) M[j][r + 1] += M[j][i] * n;
        }
    }
}

// Characteristic polynomial of matrix M (|xI - M|)
// Complexity: O(n^3)
// R. Rehman, I. C. Ipsen, "La Budde's Method for Computing Characteristic Polynomials," 2011.
template <class Tp> std::vector<Tp> characteristic_poly(std::vector<std::vector<Tp>> &M) {
    hessenberg_reduction(M);
    const int N = M.size();
    std::vector<std::vector<Tp>> p(N + 1); // p[i + 1] = (Characteristic polynomial of i-th leading principal minor)
    p[0] = {1};
    for (int i = 0; i < N; i++) {
        p[i + 1].assign(i + 2, 0);
        for (int j = 0; j < i + 1; j++) p[i + 1][j + 1] += p[i][j];
        for (int j = 0; j < i + 1; j++) p[i + 1][j] -= p[i][j] * M[i][i];
        Tp betas = 1;
        for (int j = i - 1; j >= 0; j--) {
            betas *= M[j + 1][j];
            Tp hb = -M[j][i] * betas;
            for (int k = 0; k < j + 1; k++) p[i + 1][k] += hb * p[j][k];
        }
    }
    return p[N];
}




int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);

    auto Graph = read_input();
    const int X = get<1>(Graph), Y = get<2>(Graph), Z = get<3>(Graph);
    auto to = get<4>(Graph);
    const int N = to.size();

    const int V = N + 5, r = V * 2 - 8;

    // https://drops.dagstuhl.de/opus/volltexte/2016/6832/
    // 2.1 の Remark に従って行列を構築
    // X を (X, N) に,Y を (Y, N + 1) に,Z を (Z, N + 2) にそれぞれ分裂
    // 論文 Fig. 1 の b1, b2 が N + 3, N + 4 に対応
    const vector<int> terminal_vs{X, Y, Z, N, N + 1, N + 2, N + 3, N + 4};
    auto Label = [&](int i) -> int {
        if (i == X or i == N) return 1;
        if (i == Y or i == N + 1) return 2;
        if (i == Z or i == N + 2) return 3;
        if (i == N + 3) return 4;
        if (i == N + 4) return 5;
        return 0;
    };

    // 論文通りに実装すると Z は 2(N + 5) * M 行列となるが,all zero な行が 8 つ発生して厄介
    // なので,これらを潰して (2N + 2) * M 行列(feasible ならばこれは行フルランク)を構築.
    // 潰した行を飛ばした index を取得する関数
    auto truncate = [&](int i) -> int {
        int red = 0;
        for (auto l : terminal_vs) {
            if (l * 2 < i) ++red;
        }
        return i - red;
    };

    // Z の各列ベクトルを生成
    // 各列はスパースなので,ノンゼロな要素番号とその値のペアの列を返す
    auto gen_edge_vec = [&](int i, int j) -> pair<vector<pair<int, mint>>, vector<pair<int, mint>>> {
        vector<pair<int, mint>> bret, cret;
        if (Label(i)) {
            bret.emplace_back(truncate(i * 2 + 1), -Label(i));
        } else {
            bret.emplace_back(truncate(i * 2), 1);
        }
        if (Label(j)) {
            bret.emplace_back(truncate(j * 2 + 1), Label(j));
        } else {
            bret.emplace_back(truncate(j * 2), -1);
        }
        cret.emplace_back(truncate(i * 2 + 1), 1);
        cret.emplace_back(truncate(j * 2 + 1), -1);
        return {bret, cret};
    };


    to.resize(N + 3);
    for (auto i : to[X]) {
        to[N].push_back(i);
        to[i].push_back(N);
    }
    for (auto i : to[Y]) {
        to[N + 1].push_back(i);
        to[i].push_back(N + 1);
    }
    for (auto i : to[Z]) {
        to[N + 2].push_back(i);
        to[i].push_back(N + 2);
    }

    // 論文通りに辺を張る
    vector<tuple<vector<pair<int, mint>>, vector<pair<int, mint>>, int>> bcws;
    for (int i = 0; i < N + 3; ++i) {
        for (int j : to[i]) {
            if (i > j) continue;
            auto [b, c] = gen_edge_vec(i, j);
            bcws.emplace_back(b, c, 1);
        }

        if (!Label(i)) {
            auto [b, c] = gen_edge_vec(i, N + 3);
            bcws.emplace_back(b, c, 0);
        }
    }

    {
        auto [b, c] = gen_edge_vec(N + 3, N + 4);
        bcws.emplace_back(b, c, 0);
    }

    // M(x) = mat0 + mat1 * x
    vector mat0(r, vector<mint>(r));
    vector mat1(r, vector<mint>(r));
    for (const auto &[b, c, w] : bcws) {
        mint x = rand_int() % mint::mod();
        mint y = rand_int() % mint::mod();
        for (auto [i, bi] : b) {
            for (auto [j, cj] : c) {
                auto v = bi * cj * x;
                if (w == 0) {
                    mat0[i][j] += v;
                    mat0[j][i] -= v;
                    mat1[i][j] += v * y;
                    mat1[j][i] -= v * y;
                }
                if (w == 1) {
                    mat1[i][j] += v;
                    mat1[j][i] -= v;
                }
            }
        }
    }

    // det(x mat1 + mat0) を x の多項式として求めたい
    // mat1 を掃き出して det(\lambda I - A) の形に帰着させる
    for (int i = 0; i < r; ++i) {
        int piv = -1;
        for (int h = i; h < r; ++h) {
            if (mat1[h][i] != 0) piv = h;
        }
        if (piv < 0) {
            cout << "-1\n";
            return 0;
        }
        assert(piv >= i);
        swap(mat0[i], mat0[piv]);
        swap(mat1[i], mat1[piv]);

        if (i != piv) {
            for (int w = 0; w < r; ++w) {
                mat0[i][w] *= -1;
                mat1[i][w] *= -1;
            }
        }

        mint inv = mat1[i][i].inv();
        for (int w = 0; w < r; ++w) {
            mat0[i][w] *= inv;
            mat1[i][w] *= inv;
        }
        for (int h = 0; h < r; ++h) {
            if (h == i) continue;
            if (mat1[h][i] == 0) continue;
            const mint coeff = mat1[h][i];
            for (int w = 0; w < r; ++w) {
                mat1[h][w] -= coeff * mat1[i][w];
                mat0[h][w] -= coeff * mat0[i][w];
            }
        }
    }

    for(auto &v : mat0) for (auto &x : v) x = -x;

    auto det_poly = characteristic_poly<mint>(mat0);

    int ret = 0;
    while (ret < int(det_poly.size()) and det_poly[ret] == 0) ++ret;

    if (ret < int(det_poly.size())) {
        cout << ret / 2 << '\n';
    } else {
        cout << "-1\n";
    }
}
0