-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtutte.test.cpp
64 lines (60 loc) · 1.68 KB
/
tutte.test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// @brief Matching on General Graph (Tutte matrix)
#define PROBLEM "https://judge.yosupo.jp/problem/general_matching"
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("tune=native")
#include "cp-algo/linalg/matrix.hpp"
#include "cp-algo/random/rng.hpp"
#include <bits/stdc++.h>
using namespace std;
using namespace cp_algo::math;
using namespace cp_algo::linalg;
using namespace cp_algo::random;
const int64_t mod = 998244353;
using base = modint<mod>;
void solve() {
int n, m;
cin >> n >> m;
matrix<base> T(n);
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
base x = rng();
T[u][v] += x;
T[v][u] -= x;
}
auto [pivots, free] = matrix(T).echelonize();
matrix<base> B(size(pivots));
for(int i = 0; i < ssize(pivots); i++) {
for(int j = 0; j < ssize(pivots); j++) {
B[i][j] = T[pivots[i]][pivots[j]];
}
}
auto [d, Bi] = B.inv();
vector<pair<int, int>> ans;
for(size_t i = 0; i < size(pivots); i++) {
for(size_t j = 0; j < size(pivots); j++) {
if(T[pivots[i]][pivots[j]] != 0 && Bi[i][j] != 0) {
ans.emplace_back(pivots[i], pivots[j]);
Bi.eliminate<gauss_mode::reverse>(i, j);
Bi.eliminate<gauss_mode::reverse>(j, i);
Bi.normalize();
Bi[i] *= 0;
Bi[j] *= 0;
}
}
}
cout << size(ans) << "\n";
for(auto [u, v]: ans) {
cout << u << ' ' << v << "\n";
}
}
signed main() {
//freopen("input.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}