結果
| 問題 | No.1774 Love Triangle (Hard) |
| ユーザー |
|
| 提出日時 | 2026-11-26 13:53:04 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 682 ms / 8,000 ms |
| + 745µs | |
| コード長 | 7,791 bytes |
| 記録 | |
| コンパイル時間 | 3,670 ms |
| コンパイル使用メモリ | 374,328 KB |
| 実行使用メモリ | 9,924 KB |
| 最終ジャッジ日時 | 2026-11-26 13:54:15 |
| 合計ジャッジ時間 | 49,947 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge4_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 92 |
ソースコード
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
namespace triangle_parity {
constexpr int MOD = 998244353;
using i64 = long long;
inline int add(int a, int b) {
int s = a + b;
return s >= MOD ? s - MOD : s;
}
inline int sub(int a, int b) {
int s = a - b;
return s < 0 ? s + MOD : s;
}
inline int neg(int a) { return a ? MOD - a : 0; }
inline int mul(int a, int b) { return i64(a) * b % MOD; }
int power(int a, int exponent) {
int result = 1;
for (; exponent; exponent >>= 1, a = mul(a, a))
if (exponent & 1) result = mul(result, a);
return result;
}
struct Triple { int a, b, c; };
// A is the explicit sparse skew matrix. The selected principal block
// A[I,I] is nonsingular, and K is its inverse; its size equals rank(A).
class RankTracker {
int n, rank_ = 0;
vector<int> A, K, position, selected;
vector<vector<int>> neighbors;
vector<pair<int, int>> support;
vector<unsigned char> present;
vector<int> a, b, f, g, scaled_a, scaled_b, e1, e2;
mt19937_64 rng;
uniform_int_distribution<int> nonzero{1, MOD - 1};
void add_edge(int u, int v, int weight) {
if (u > v) {
swap(u, v);
weight = neg(weight);
}
int cell = u * n + v;
if (!present[cell]) {
present[cell] = 1;
support.emplace_back(u, v);
neighbors[u].push_back(v);
neighbors[v].push_back(u);
}
A[cell] = add(A[cell], weight);
A[v * n + u] = neg(A[cell]);
}
// K += scale*(x*y^T-y*x^T), preserving exact skew symmetry.
void skew_inverse_update(const vector<int>& x, const vector<int>& y,
int scale) {
for (int i = 0; i < rank_; ++i) {
scaled_a[i] = mul(scale, x[i]);
scaled_b[i] = mul(scale, y[i]);
}
for (int i = 0; i < rank_; ++i) {
int* row = K.data() + i * n;
const i64 sx = scaled_a[i], sy = scaled_b[i];
for (int j = i + 1; j < rank_; ++j) {
int value = (row[j] + sx * y[j] - sy * x[j]) % MOD;
if (value < 0) value += MOD;
row[j] = value;
K[j * n + i] = neg(value);
}
}
}
public:
RankTracker(int vertices, uint64_t seed)
: n(vertices), A(n * n), K(n * n), position(n, -1), neighbors(n),
present(n * n), a(n), b(n), f(n), g(n), scaled_a(n),
scaled_b(n), e1(n), e2(n), rng(seed) {
selected.reserve(n);
}
int answer() const { return rank_ / 2; }
int insert(const Triple& triangle) {
const int va = triangle.a, vb = triangle.b, vc = triangle.c;
const int pa = position[va], pb = position[vb], pc = position[vc];
// u=e_b-e_a, v=e_c-e_a; a=K*u_I and b=K*v_I.
bool nonzero_a = false, nonzero_b = false;
for (int i = 0; i < rank_; ++i) {
const int* row = K.data() + i * n;
int common = pa < 0 ? 0 : row[pa];
a[i] = sub(pb < 0 ? 0 : row[pb], common);
b[i] = sub(pc < 0 ? 0 : row[pc], common);
nonzero_a |= a[i] != 0;
nonzero_b |= b[i] != 0;
}
// Outside-I projections f=u-A*extend(a), g=v-A*extend(b).
// Only active/inactive support edges contribute.
fill(f.begin(), f.end(), 0);
fill(g.begin(), g.end(), 0);
if (pa < 0) f[va] = g[va] = MOD - 1;
if (pb < 0) f[vb] = 1;
if (pc < 0) g[vc] = 1;
for (auto [u, v] : support) {
int pu = position[u], pv = position[v];
int weight = A[u * n + v];
if (pu >= 0 && pv < 0) {
f[v] = add(f[v], mul(weight, a[pu]));
g[v] = add(g[v], mul(weight, b[pu]));
} else if (pu < 0 && pv >= 0) {
f[u] = sub(f[u], mul(weight, a[pv]));
g[u] = sub(g[u], mul(weight, b[pv]));
}
}
int first = -1, second = -1, determinant = 0;
for (int v = 0; v < n; ++v)
if (position[v] < 0 && f[v]) { first = v; break; }
if (first >= 0) {
for (int v = 0; v < n; ++v) if (position[v] < 0) {
int d = sub(mul(f[first], g[v]), mul(g[first], f[v]));
if (d) {
second = v;
determinant = d;
break;
}
}
}
int s = sub(pb < 0 ? 0 : b[pb], pa < 0 ? 0 : b[pa]);
int weight, delta;
do {
weight = nonzero(rng);
delta = sub(1, mul(weight, s));
} while (!delta); // At most one nonzero weight is forbidden.
int alpha = mul(weight, power(delta, MOD - 2));
// Keep the old principal block nonsingular after the rank-two update.
if (nonzero_a && nonzero_b)
skew_inverse_update(a, b, alpha);
add_edge(va, vb, weight);
add_edge(vb, vc, weight);
add_edge(vc, va, weight);
if (second < 0) return answer();
// Append two outside vertices whose Schur complement is nonsingular.
// e1=K*C_first, e2=K*C_second. The columns C are sparse in A.
vector<pair<int, int>> column1, column2;
column1.reserve(neighbors[first].size());
column2.reserve(neighbors[second].size());
for (int v : neighbors[first]) if (position[v] >= 0)
column1.emplace_back(position[v], A[v * n + first]);
for (int v : neighbors[second]) if (position[v] >= 0)
column2.emplace_back(position[v], A[v * n + second]);
for (int i = 0; i < rank_; ++i) {
const int* row = K.data() + i * n;
int x = 0, y = 0;
for (auto [j, value] : column1) x = add(x, mul(row[j], value));
for (auto [j, value] : column2) y = add(y, mul(row[j], value));
e1[i] = x;
e2[i] = y;
}
int h = mul(alpha, determinant);
#ifndef NDEBUG
int actual_h = A[first * n + second];
for (auto [j, value] : column1)
actual_h = add(actual_h, mul(value, e2[j]));
assert(h == actual_h && h != 0);
#endif
int inverse_h = power(h, MOD - 2);
skew_inverse_update(e2, e1, inverse_h);
for (int i = 0; i < rank_; ++i) {
int x = mul(e1[i], inverse_h), y = mul(e2[i], inverse_h);
K[i * n + rank_] = neg(y);
K[rank_ * n + i] = y;
K[i * n + rank_ + 1] = x;
K[(rank_ + 1) * n + i] = neg(x);
}
K[rank_ * n + rank_ + 1] = neg(inverse_h);
K[(rank_ + 1) * n + rank_] = inverse_h;
position[first] = rank_;
position[second] = rank_ + 1;
selected.push_back(first);
selected.push_back(second);
rank_ += 2;
return answer();
}
};
} // namespace triangle_parity
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
if (!(cin >> n >> m)) return 0;
vector<triangle_parity::Triple> triangles(m);
for (auto& t : triangles) {
cin >> t.a >> t.b >> t.c;
--t.a; --t.b; --t.c;
}
uint64_t seed = chrono::steady_clock::now().time_since_epoch().count();
random_device random;
seed ^= uint64_t(random()) << 32;
seed ^= random();
mt19937_64 seeds(seed);
vector<int> answer(m);
const int upper_bound = (n - 1) / 2;
for (int trial = 0; trial < 2; ++trial) {
triangle_parity::RankTracker tracker(n, seeds());
for (int i = 0; i < m; ++i) {
int value = tracker.answer() == upper_bound
? upper_bound : tracker.insert(triangles[i]);
answer[i] = max(answer[i], value);
}
}
for (int value : answer) cout << value << '\n';
}