結果

問題 No.1774 Love Triangle (Hard)
ユーザー hitonanodehitonanode
提出日時 2021-09-28 00:24:17
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,978 ms / 8,000 ms
コード長 5,374 bytes
コンパイル時間 2,186 ms
コンパイル使用メモリ 163,792 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-07-26 20:50:54
合計ジャッジ時間 343,688 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 11 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 16 ms
6,940 KB
testcase_12 AC 2,683 ms
8,192 KB
testcase_13 AC 1,460 ms
6,944 KB
testcase_14 AC 110 ms
6,944 KB
testcase_15 AC 1,111 ms
6,944 KB
testcase_16 AC 408 ms
6,940 KB
testcase_17 AC 20 ms
6,940 KB
testcase_18 AC 1,328 ms
6,944 KB
testcase_19 AC 45 ms
6,944 KB
testcase_20 AC 112 ms
6,944 KB
testcase_21 AC 67 ms
6,940 KB
testcase_22 AC 4,530 ms
10,144 KB
testcase_23 AC 4,675 ms
10,204 KB
testcase_24 AC 4,666 ms
10,256 KB
testcase_25 AC 4,583 ms
10,240 KB
testcase_26 AC 4,654 ms
10,136 KB
testcase_27 AC 4,580 ms
10,240 KB
testcase_28 AC 4,607 ms
10,240 KB
testcase_29 AC 4,737 ms
10,264 KB
testcase_30 AC 4,753 ms
10,368 KB
testcase_31 AC 4,613 ms
10,256 KB
testcase_32 AC 4,646 ms
10,260 KB
testcase_33 AC 4,684 ms
10,244 KB
testcase_34 AC 4,663 ms
10,264 KB
testcase_35 AC 4,654 ms
10,272 KB
testcase_36 AC 4,483 ms
10,012 KB
testcase_37 AC 4,692 ms
10,276 KB
testcase_38 AC 4,607 ms
10,368 KB
testcase_39 AC 4,577 ms
10,220 KB
testcase_40 AC 4,849 ms
10,264 KB
testcase_41 AC 4,789 ms
10,276 KB
testcase_42 AC 4,731 ms
10,368 KB
testcase_43 AC 4,733 ms
10,368 KB
testcase_44 AC 4,712 ms
10,368 KB
testcase_45 AC 4,697 ms
10,368 KB
testcase_46 AC 4,710 ms
10,368 KB
testcase_47 AC 4,709 ms
10,336 KB
testcase_48 AC 4,744 ms
10,368 KB
testcase_49 AC 4,658 ms
10,368 KB
testcase_50 AC 4,671 ms
10,496 KB
testcase_51 AC 4,683 ms
10,368 KB
testcase_52 AC 4,679 ms
10,240 KB
testcase_53 AC 4,728 ms
10,368 KB
testcase_54 AC 4,699 ms
10,368 KB
testcase_55 AC 4,731 ms
10,368 KB
testcase_56 AC 4,707 ms
10,368 KB
testcase_57 AC 4,716 ms
10,368 KB
testcase_58 AC 4,761 ms
10,368 KB
testcase_59 AC 4,742 ms
10,368 KB
testcase_60 AC 4,695 ms
10,368 KB
testcase_61 AC 4,719 ms
10,496 KB
testcase_62 AC 4,723 ms
10,332 KB
testcase_63 AC 4,738 ms
10,368 KB
testcase_64 AC 4,580 ms
10,368 KB
testcase_65 AC 4,732 ms
10,368 KB
testcase_66 AC 4,687 ms
10,496 KB
testcase_67 AC 4,539 ms
10,240 KB
testcase_68 AC 4,978 ms
10,368 KB
testcase_69 AC 4,693 ms
10,240 KB
testcase_70 AC 4,572 ms
10,240 KB
testcase_71 AC 4,732 ms
10,368 KB
testcase_72 AC 4,759 ms
10,240 KB
testcase_73 AC 4,683 ms
10,368 KB
testcase_74 AC 4,717 ms
10,368 KB
testcase_75 AC 4,694 ms
10,368 KB
testcase_76 AC 4,589 ms
10,116 KB
testcase_77 AC 4,666 ms
10,368 KB
testcase_78 AC 4,716 ms
10,240 KB
testcase_79 AC 4,502 ms
10,152 KB
testcase_80 AC 4,730 ms
10,368 KB
testcase_81 AC 4,710 ms
10,368 KB
testcase_82 AC 4,612 ms
10,240 KB
testcase_83 AC 4,630 ms
10,240 KB
testcase_84 AC 4,537 ms
10,280 KB
testcase_85 AC 4,606 ms
10,180 KB
testcase_86 AC 4,622 ms
10,112 KB
testcase_87 AC 4,704 ms
10,368 KB
testcase_88 AC 4,614 ms
10,244 KB
testcase_89 AC 4,615 ms
10,244 KB
testcase_90 AC 4,694 ms
10,368 KB
testcase_91 AC 4,470 ms
10,128 KB
testcase_92 AC 4,516 ms
10,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <chrono>
#include <iostream>
#include <numeric>
#include <random>
#include <utility>
#include <vector>
using namespace std;

#include <atcoder/modint>
using mint = atcoder::static_modint<1000000009>;
// using mint = atcoder::modint1000000007;


template <class T>
std::vector<T> mat_vec_mul(const std::vector<std::vector<T>> &mat, const std::vector<T> &vec) {
    int H = mat.size(), W = mat[0].size();
    assert(W == int(vec.size()));
    std::vector<T> ret(H);
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) ret[i] += mat[i][j] * vec[j];
    }
    return ret;
}

// Try to calculate inverse of M and return rank of M (destructive)
template <class T>
int inverse_matrix(std::vector<std::vector<T>> &M) {
    const int N = M.size();
    assert(N and M[0].size() == M.size());
    std::vector<std::vector<T>> ret(N, std::vector<T>(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 M[ti][i] == 0) ti++;
        if (ti == N) {
            continue;
        }
        ++rank;
        ret[i].swap(ret[ti]), M[i].swap(M[ti]);
        T inv = T(1) / M[i][i];
        for (int j = 0; j < N; ++j) ret[i][j] *= inv;
        for (int j = i + 1; j < N; ++j) M[i][j] *= inv;
        for (int h = 0; h < N; ++h) {
            if (i == h) continue;
            const T c = -M[h][i];
            for (int j = 0; j < N; ++j) ret[h][j] += ret[i][j] * c;
            for (int j = i + 1; j < N; ++j) M[h][j] += M[i][j] * c;
        }
    }
    M = ret;
    return rank;
}


template <class ModInt>
std::vector<int>
linear_matroid_parity(const std::vector<std::pair<std::vector<ModInt>, std::vector<ModInt>>> &bcs) {
    if (bcs.empty()) return {};
    const int r = bcs[0].first.size(), m = bcs.size(), r2 = (r + 1) / 2;
    std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count());
    std::uniform_int_distribution<int> d(0, ModInt::mod() - 1);

    auto gen_random_vector = [&]() -> std::vector<ModInt> {
        std::vector<ModInt> v(r2 * 2);
        for (int i = 0; i < r2 * 2; i++) v[i] = d(mt);
        return v;
    };

    std::vector<ModInt> x(m);
    std::vector<std::pair<vector<ModInt>, vector<ModInt>>> bcadd(r2);

    std::vector<std::vector<ModInt>> Yinv; // r2 * r2 matrices
    int rankY = -1;
    while (rankY < r2 * 2) {
        Yinv.assign(r2 * 2, std::vector<ModInt>(r2 * 2, 0));
        for (auto &[b, c] : bcadd) {
            b = gen_random_vector(), c = gen_random_vector();
            for (int j = 0; j < r2 * 2; j++) {
                for (int k = 0; k < r2 * 2; k++) Yinv[j][k] += b[j] * c[k] - c[j] * b[k];
            }
        }
        rankY = inverse_matrix(Yinv);
    }

    std::vector<std::vector<ModInt>> tmpmat(r2 * 2, std::vector<ModInt>(r2 * 2));


    std::vector<int> ret(m, -1);
    int additional_dim = bcadd.size();
    for (int i = 0; i < m; i++) {
        {
            x[i] = d(mt);
            auto b = bcs[i].first, c = bcs[i].second;
            b.resize(r2 * 2, 0), c.resize(r2 * 2, 0);
            std::vector<ModInt> Yib = mat_vec_mul(Yinv, b), Yic = mat_vec_mul(Yinv, c);
            ModInt bYic = std::inner_product(b.begin(), b.end(), Yic.begin(), ModInt(0));
            ModInt v = 1 + x[i] * bYic;

            if (v == 0) break; // failed

            const auto coeff = x[i] / v;
            for (int j = 0; j < r2 * 2; j++) {
                for (int k = 0; k < r2 * 2; k++) {
                    tmpmat[j][k] = Yib[j] * Yic[k] - Yic[j] * Yib[k];
                }
            }

            for (int j = 0; j < r2 * 2; j++) {
                for (int k = 0; k < r2 * 2; k++) Yinv[j][k] -= tmpmat[j][k] * coeff;
            }
        }

        if (additional_dim) {
            const auto &[b, c] = bcadd[additional_dim - 1];
            std::vector<ModInt> Yib = mat_vec_mul(Yinv, b), Yic = mat_vec_mul(Yinv, c);
            ModInt bYic = std::inner_product(b.begin(), b.end(), Yic.begin(), ModInt(0));
            const ModInt v = 1 + bYic;
            if (v != 0) {
                // 消しても正則
                additional_dim--;
                const auto coeff = 1 / v;
                for (int j = 0; j < r2 * 2; j++) {
                    for (int k = 0; k < r2 * 2; k++) tmpmat[j][k] = Yib[j] * Yic[k] - Yic[j] * Yib[k];
                }
                for (int j = 0; j < r2 * 2; j++) {
                    for (int k = 0; k < r2 * 2; k++) Yinv[j][k] -= tmpmat[j][k] * coeff;
                }
            }
        }

        ret[i] = r2 - additional_dim;
    }

    return ret;
}

vector<int> solve(int N, vector<pair<pair<int, int>, pair<int, int>>> bcs) {
    vector<pair<vector<mint>, vector<mint>>> vs;
    for (auto [ab, cd] : bcs) {
        auto [a, b] = ab;
        auto [c, d] = cd;
        vector<mint> B(N), C(N);
        B.at(a) += 1;
        B.at(b) -= 1;
        C.at(c) += 1;
        C.at(d) -= 1;
        vs.emplace_back(B, C);
    }

    return linear_matroid_parity<mint>(vs);
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<pair<pair<int, int>, pair<int, int>>> edges;
    while (M--) {
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--, w--;
        edges.push_back({{u, w}, {v, w}});
    }
    auto ret = solve(N, edges);
    for (auto x : ret) cout << x << '\n';
}
0