結果

問題 No.1775 Love Triangle 2
ユーザー 👑 hitonanodehitonanode
提出日時 2021-11-27 12:41:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,273 ms / 8,000 ms
コード長 6,697 bytes
コンパイル時間 1,728 ms
コンパイル使用メモリ 117,096 KB
実行使用メモリ 4,596 KB
最終ジャッジ日時 2023-09-16 22:25:49
合計ジャッジ時間 197,560 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2,239 ms
4,532 KB
testcase_05 AC 2,238 ms
4,544 KB
testcase_06 AC 2,236 ms
4,596 KB
testcase_07 AC 2,235 ms
4,496 KB
testcase_08 AC 2,239 ms
4,412 KB
testcase_09 AC 2,236 ms
4,376 KB
testcase_10 AC 2,239 ms
4,380 KB
testcase_11 AC 2,238 ms
4,380 KB
testcase_12 AC 2,237 ms
4,376 KB
testcase_13 AC 2,237 ms
4,380 KB
testcase_14 AC 2,235 ms
4,376 KB
testcase_15 AC 2,237 ms
4,376 KB
testcase_16 AC 2,239 ms
4,376 KB
testcase_17 AC 2,238 ms
4,380 KB
testcase_18 AC 2,235 ms
4,516 KB
testcase_19 AC 2,235 ms
4,380 KB
testcase_20 AC 2,233 ms
4,380 KB
testcase_21 AC 2,237 ms
4,380 KB
testcase_22 AC 2,235 ms
4,384 KB
testcase_23 AC 2,234 ms
4,380 KB
testcase_24 AC 2,235 ms
4,384 KB
testcase_25 AC 2,238 ms
4,380 KB
testcase_26 AC 2,237 ms
4,380 KB
testcase_27 AC 2,238 ms
4,384 KB
testcase_28 AC 2,235 ms
4,380 KB
testcase_29 AC 2,241 ms
4,564 KB
testcase_30 AC 2,273 ms
4,496 KB
testcase_31 AC 2,239 ms
4,376 KB
testcase_32 AC 2,242 ms
4,380 KB
testcase_33 AC 2,240 ms
4,376 KB
testcase_34 AC 2,241 ms
4,380 KB
testcase_35 AC 2,240 ms
4,376 KB
testcase_36 AC 2,235 ms
4,376 KB
testcase_37 AC 2,234 ms
4,380 KB
testcase_38 AC 2,232 ms
4,376 KB
testcase_39 AC 2,239 ms
4,500 KB
testcase_40 AC 2,244 ms
4,380 KB
testcase_41 AC 2,240 ms
4,376 KB
testcase_42 AC 2,248 ms
4,380 KB
testcase_43 AC 2,241 ms
4,384 KB
testcase_44 AC 2,236 ms
4,384 KB
testcase_45 AC 2,240 ms
4,380 KB
testcase_46 AC 2,234 ms
4,384 KB
testcase_47 AC 2,235 ms
4,380 KB
testcase_48 AC 2,240 ms
4,380 KB
testcase_49 AC 2,238 ms
4,444 KB
testcase_50 AC 2,236 ms
4,384 KB
testcase_51 AC 2,239 ms
4,500 KB
testcase_52 AC 2,246 ms
4,380 KB
testcase_53 AC 2,246 ms
4,384 KB
testcase_54 AC 2,240 ms
4,424 KB
testcase_55 AC 2,241 ms
4,380 KB
testcase_56 AC 2,243 ms
4,380 KB
testcase_57 AC 2,239 ms
4,376 KB
testcase_58 AC 2,234 ms
4,380 KB
testcase_59 AC 2,238 ms
4,504 KB
testcase_60 AC 2,242 ms
4,388 KB
testcase_61 AC 2,241 ms
4,380 KB
testcase_62 AC 2,242 ms
4,380 KB
testcase_63 AC 2,237 ms
4,380 KB
testcase_64 AC 2,238 ms
4,384 KB
testcase_65 AC 2,238 ms
4,376 KB
testcase_66 AC 2,234 ms
4,504 KB
testcase_67 AC 2,234 ms
4,376 KB
testcase_68 AC 2,240 ms
4,380 KB
testcase_69 AC 2,240 ms
4,376 KB
testcase_70 AC 2,235 ms
4,380 KB
testcase_71 AC 2,237 ms
4,380 KB
testcase_72 AC 2,235 ms
4,380 KB
testcase_73 AC 2,239 ms
4,376 KB
testcase_74 AC 2,238 ms
4,376 KB
testcase_75 AC 2,239 ms
4,380 KB
testcase_76 AC 2,237 ms
4,380 KB
testcase_77 AC 2,238 ms
4,380 KB
testcase_78 AC 2,239 ms
4,380 KB
testcase_79 AC 2,247 ms
4,380 KB
testcase_80 AC 2,240 ms
4,376 KB
testcase_81 AC 2,243 ms
4,380 KB
testcase_82 AC 2,240 ms
4,380 KB
testcase_83 AC 2,240 ms
4,380 KB
testcase_84 AC 2,242 ms
4,376 KB
testcase_85 AC 2,241 ms
4,376 KB
testcase_86 AC 2,242 ms
4,376 KB
testcase_87 AC 2,241 ms
4,380 KB
testcase_88 AC 2,239 ms
4,380 KB
testcase_89 AC 2,238 ms
4,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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};
}

template <class Fp>
std::vector<std::vector<Fp>> inverse(std::vector<std::vector<Fp>> &tmp) {
    const int n = tmp.size();
    std::vector<std::vector<Fp>> ret(n, std::vector<Fp>(n));
    for (int i = 0; i < n; ++i) ret[i][i] = 1;

    int rank = 0;
    for (int i = 0; i < n; ++i) {
        int ti = i;
        while (ti < n and tmp[ti][i] == 0) ++ti;
        if (ti == n) continue;
        ++rank;

        ret[i].swap(ret[ti]), tmp[i].swap(tmp[ti]);
        Fp inv = Fp(1) / tmp[i][i];
        for (int j = 0; j < n; j++) ret[i][j] *= inv;
        for (int j = i + 1; j < n; j++) tmp[i][j] *= inv;
        for (int h = 0; h < n; h++) {
            if (i == h) continue;
            const Fp c = -tmp[h][i];
            for (int j = 0; j < n; j++) ret[h][j] += ret[i][j] * c;
            for (int j = i + 1; j < n; j++) tmp[h][j] += tmp[i][j] * c;
        }
    }
    return ret;
}

template <class Fp>
Fp determinant(std::vector<std::vector<Fp>> &M) {
    int n = M.size();
    assert(int(M[0].size()) == n);
    Fp ret = 1;
    for (int i = 0; i < n; ++i) {
        int h = i;
        while (h < n and M[h][i] == 0) ++h;
        if (h == n) return 0;
        if (h != i) {
            M[h].swap(M[i]);
            ret = -ret;
        }
        ret *= M[i][i];
        auto inv = M[i][i].inv();
        for (int j = i + 1; j < n; ++j) M[i][j] *= inv;
        for (int h = 0; h < n; ++h) {
            if (h == i) continue;
            Fp c = -M[h][i];
            for (int j = i + 1; j < n; ++j) M[h][j] += M[i][j] * c;
        }
    }
    return ret;
}


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

    auto Graph = read_input();
    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 = to.size() + 5, r = V * 2;

    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;
    };

    auto gen_edge_vec = [&](int i, int j) -> pair<vector<pair<int, mint>>, vector<pair<int, mint>>> {
        vector<mint> b(r), c(r);
        b[i * 2] = 1, b[j * 2] = -1;
        c[i * 2 + 1] = 1, c[j * 2 + 1] = -1;
        if (Label(i)) {
            b[i * 2] -= 1;
            b[i * 2 + 1] -= Label(i);
        }
        if (Label(j)) {
            b[j * 2] += 1;
            b[j * 2 + 1] += Label(j);
        }
        vector<pair<int, mint>> bret, cret;
        for (int i = 0; i < int(b.size()); ++i) {
            if (b[i] != 0) bret.emplace_back(i, b[i]);
            if (c[i] != 0) cret.emplace_back(i, c[i]);
        }
        return {bret, cret};
    };

    vector<tuple<vector<pair<int, mint>>, vector<pair<int, mint>>, int>> bcws;
    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);
    }

    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);
    }

    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();
        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;
                }
                if (w == 1) {
                    mat1[i][j] += v;
                    mat1[j][i] -= v;
                }
            }
        }
    }

    for (int t = 0; t < 8; ++t) {
        std::vector<mint> b(r), c(r);
        for (auto &x : b) x = rand_int() % mint::mod();
        for (auto &x : c) x = rand_int() % mint::mod();
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < r; ++j) mat0[i][j] += b[i] * c[j];
        }
    }
    std::vector<mint> poly_evals;
    for (int d = 1; d <= N * 2 + 1; ++d) {
        vector M(r, vector<mint>(r));
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < r; ++j) { M[i][j] = mat0[i][j] + mat1[i][j] * d; }
        }
        poly_evals.push_back(determinant(M));
    }

    vector A(poly_evals.size(), vector<mint>(poly_evals.size()));
    for (int i = 0; i < int(poly_evals.size()); ++i) {
        mint pow = 1;
        for (int j = 0; j < int(poly_evals.size()); ++j) {
            A[i][j] = pow;
            pow *= i + 1;
        }
    }
    A = inverse(A);
    vector<mint> det_poly(poly_evals.size());
    for (int i = 0; i < int(poly_evals.size()); ++i) {
        for (int j = 0; j < int(poly_evals.size()); ++j) det_poly[i] += A[i][j] * poly_evals[j];
    }

    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