This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/graph/max_matching.hpp"
Edges<T> max_matching(int n, Edges<T> es)
$n$ 頂点無向グラフの辺集合 es
からグラフの最大マッチングを計算します.
返り値は採用された辺集合です.
注: #include "graph/max_matching.hpp"
を #include "template/template.hpp"
の上に書かないとCompile Errorが起こります.
制約
計算量
頂点数,辺数をそれぞれ $V, E$ として,
#pragma once
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/max_cardinality_matching.hpp>
#include "../template/template.hpp"
#include "../graph/graph_template.hpp"
template <typename T>
Edges<T> max_matching(const int n, const Edges<T>& es) {
using namespace boost;
using G = adjacency_list<vecS, vecS, undirectedS>;
using V = graph_traits<G>::vertex_descriptor;
G g(n);
for(const Edge<T>& e : es) {
assert(e.cost == 1);
add_edge(e.from, e.to, g);
}
vector<V> mate(n);
edmonds_maximum_cardinality_matching(g, &mate[0]);
Edges<T> res;
for(V v = 0; v < num_vertices(g); ++v) {
if(mate[v] != graph_traits<G>::null_vertex() and v < mate[v]) {
res.push_back({(int)v, (int)mate[v], 1});
}
}
return res;
}
#line 2 "src/graph/max_matching.hpp"
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/max_cardinality_matching.hpp>
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
int from, to;
T cost;
int idx;
Edge()
: from(-1), to(-1), cost(-1), idx(-1) {}
Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
: from(from), to(to), cost(cost), idx(idx) {}
operator int() const {
return to;
}
};
template <typename T>
struct Graph {
Graph(const int N)
: n(N), es(0), g(N) {}
int size() const {
return n;
}
int edge_size() const {
return es;
}
void add_edge(const int from, const int to, const T& cost = 1) {
assert(0 <= from and from < n);
assert(0 <= to and to < n);
g[from].emplace_back(from, to, cost, es);
g[to].emplace_back(to, from, cost, es++);
}
void add_directed_edge(const int from, const int to, const T& cost = 1) {
assert(0 <= from and from < n);
assert(0 <= to and to < n);
g[from].emplace_back(from, to, cost, es++);
}
inline vector<Edge<T>>& operator[](const int& k) {
assert(0 <= k and k < n);
return g[k];
}
inline const vector<Edge<T>>& operator[](const int& k) const {
assert(0 <= k and k < n);
return g[k];
}
private:
int n, es;
vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 7 "src/graph/max_matching.hpp"
template <typename T>
Edges<T> max_matching(const int n, const Edges<T>& es) {
using namespace boost;
using G = adjacency_list<vecS, vecS, undirectedS>;
using V = graph_traits<G>::vertex_descriptor;
G g(n);
for(const Edge<T>& e : es) {
assert(e.cost == 1);
add_edge(e.from, e.to, g);
}
vector<V> mate(n);
edmonds_maximum_cardinality_matching(g, &mate[0]);
Edges<T> res;
for(V v = 0; v < num_vertices(g); ++v) {
if(mate[v] != graph_traits<G>::null_vertex() and v < mate[v]) {
res.push_back({(int)v, (int)mate[v], 1});
}
}
return res;
}