This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/general_matching"
#include "../misc/macros.h"
#include "../graph/GeneralMatching.h"
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vi> graph(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
graph[u].pb(v), graph[v].pb(u);
}
auto match = GeneralMatching(graph);
vector<pii> mate;
for (int i = 0; i < n; ++i) {
if (i < match[i]) mate.pb({i, match[i]});
}
cout << sz(mate) << '\n';
for (auto [u, v] : mate) cout << u << ' ' << v << '\n';
}#line 1 "tests/General_Matching.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/general_matching"
#line 1 "misc/macros.h"
// #pragma GCC optimize("Ofast,unroll-loops") // unroll long, simple loops
// #pragma GCC target("avx2,fma") // vectorizing code
// #pragma GCC target("lzcnt,popcnt,abm,bmi,bmi2") // for fast bitset operation
#include <bits/extc++.h>
#include <tr2/dynamic_bitset>
using namespace std;
using namespace __gnu_pbds; // ordered_set, gp_hash_table
// using namespace __gnu_cxx; // rope
// for templates to work
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define pb push_back
#define eb emplace_back
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using ld = long double;
using pii = pair<i32, i32>;
using vi = vector<i32>;
// fast map
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash { // customize hash function for gp_hash_table
int operator()(int x) const { return x ^ RANDOM; }
};
gp_hash_table<int, int, chash> table;
/* ordered set
find_by_order(k): returns an iterator to the k-th element (0-based)
order_of_key(k): returns the number of elements in the set that are strictly less than k
*/
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/* rope
rope <int> cur = v.substr(l, r - l + 1);
v.erase(l, r - l + 1);
v.insert(v.mutable_begin(), cur);
*/
#line 1 "graph/GeneralMatching.h"
vi GeneralMatching(vector<vi>& graph) {
int n = graph.size(), timer = -1;
vi mate(n, -1), label(n), parent(n), orig(n), aux(n, -1), q;
auto lca = [&](int x, int y) {
for (timer++;; swap(x, y)) {
if (x == -1) continue;
if (aux[x] == timer) return x;
aux[x] = timer;
x = (mate[x] == -1 ? -1 : orig[parent[mate[x]]]);
}
};
auto blossom = [&](int v, int w, int a) {
while (orig[v] != a) {
parent[v] = w;
w = mate[v];
if (label[w] == 1) label[w] = 0, q.push_back(w);
orig[v] = orig[w] = a;
v = parent[w];
}
};
auto augment = [&](int v) {
while (v != -1) {
int pv = parent[v], nv = mate[pv];
mate[v] = pv;
mate[pv] = v;
v = nv;
}
};
auto bfs = [&](int root) {
fill(label.begin(), label.end(), -1);
iota(orig.begin(), orig.end(), 0);
q.clear();
label[root] = 0;
q.push_back(root);
for (int i = 0; i < (int) q.size(); ++i) {
int v = q[i];
for (auto x : graph[v]) {
if (label[x] == -1) {
label[x] = 1;
parent[x] = v;
if (mate[x] == -1)
return augment(x), 1;
label[mate[x]] = 0;
q.push_back(mate[x]);
} else if (label[x] == 0 && orig[v] != orig[x]) {
int a = lca(orig[v], orig[x]);
blossom(x, v, a);
blossom(v, x, a);
}
}
}
return 0;
};
// Time halves if you start with (any) maximal matching.
for (int i = 0; i < n; i++)
if (mate[i] == -1)
bfs(i);
return mate;
}
#line 5 "tests/General_Matching.test.cpp"
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vi> graph(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
graph[u].pb(v), graph[v].pb(u);
}
auto match = GeneralMatching(graph);
vector<pii> mate;
for (int i = 0; i < n; ++i) {
if (i < match[i]) mate.pb({i, match[i]});
}
cout << sz(mate) << '\n';
for (auto [u, v] : mate) cout << u << ' ' << v << '\n';
}