結果
| 問題 |
No.1774 Love Triangle (Hard)
|
| ユーザー |
hitonanode
|
| 提出日時 | 2021-10-11 23:25:58 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 7,192 ms / 8,000 ms |
| コード長 | 5,490 bytes |
| コンパイル時間 | 4,189 ms |
| コンパイル使用メモリ | 152,992 KB |
| 実行使用メモリ | 10,412 KB |
| 最終ジャッジ日時 | 2024-09-17 07:48:04 |
| 合計ジャッジ時間 | 493,667 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 90 |
ソースコード
#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<(1 << 30) + 3>; // prime
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);
}
auto ret = linear_matroid_parity<mint>(vs);
auto ret2 = linear_matroid_parity<mint>(vs);
for (int i = 0; i < int(ret.size()); i++) ret[i] = max(ret[i], ret2[i]);
return ret;
}
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';
}
hitonanode