This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "tools/bipartite_matching.hpp"
It calculates the maximum matching on a given bipartite graph.
bipartite_matching graph(std::size_t n1, std::size_t n2);
It creates an bipartite graph with $0$ edges. Vertices of the graph can be divided into two disjoint and independent sets $U$ and $V$. $U$ consists of $n_1$ vertices and $V$ consists of $n_2$ vertices.
std::size_t graph.size1();
It returns $n_1$.
std::size_t graph.size2();
It returns $n_2$.
std::size_t graph.add_edge(std::size_t a, std::size_t b);
It adds an edge between $a \in U$ and $b \in V$. It returns an integer $k$ such that this is the $k$-th edge that is added.
struct edge {
std::size_t id;
std::size_t from;
std::size_t to;
};
edge graph.get_edge(std::size_t k);
It returns the $k$-th edge.
std::vector<edge> graph.edges();
It returns all the edges in the graph.
The edges are ordered in the same order as added by add_edge
.
std::vector<std::size_t> graph.query();
It returns the indices of the edges which can reach the maximum matching.
#ifndef TOOLS_BIPARTITE_MATCHING_HPP
#define TOOLS_BIPARTITE_MATCHING_HPP
#include <cstddef>
#include <vector>
#include <cassert>
#include "atcoder/maxflow.hpp"
namespace tools {
class bipartite_matching {
public:
struct edge {
::std::size_t id;
::std::size_t from;
::std::size_t to;
};
private:
::std::size_t m_size1;
::std::size_t m_size2;
::atcoder::mf_graph<int> m_graph;
::std::vector<edge> m_edges;
public:
bipartite_matching() = default;
bipartite_matching(const ::tools::bipartite_matching&) = default;
bipartite_matching(::tools::bipartite_matching&&) = default;
~bipartite_matching() = default;
::tools::bipartite_matching& operator=(const ::tools::bipartite_matching&) = default;
::tools::bipartite_matching& operator=(::tools::bipartite_matching&&) = default;
bipartite_matching(const ::std::size_t size1, const ::std::size_t size2) :
m_size1(size1), m_size2(size2), m_graph(size1 + size2 + 2) {
for (::std::size_t i = 0; i < size1; ++i) {
this->m_graph.add_edge(size1 + size2, i, 1);
}
for (::std::size_t i = 0; i < size2; ++i) {
this->m_graph.add_edge(size1 + i, size1 + size2 + 1, 1);
}
}
::std::size_t size1() const {
return this->m_size1;
}
::std::size_t size2() const {
return this->m_size2;
}
::std::size_t add_edge(const ::std::size_t i, const ::std::size_t j) {
assert(i < this->size1());
assert(j < this->size2());
this->m_graph.add_edge(i, this->m_size1 + j, 1);
this->m_edges.emplace_back(edge({this->m_edges.size(), i, j}));
return this->m_edges.size() - 1;
}
const edge& get_edge(const ::std::size_t k) const {
assert(k < this->m_edges.size());
return this->m_edges[k];
}
const ::std::vector<edge>& edges() const {
return this->m_edges;
}
::std::vector<::std::size_t> query() {
::std::vector<::std::size_t> edges;
this->m_graph.flow(this->m_size1 + this->m_size2, this->m_size1 + this->m_size2 + 1);
for (::std::size_t i = 0; i < this->m_edges.size(); ++i) {
if (this->m_graph.get_edge(this->m_size1 + this->m_size2 + i).flow == 1) {
edges.push_back(i);
}
}
return edges;
}
};
}
#endif
#line 1 "tools/bipartite_matching.hpp"
#include <cstddef>
#include <vector>
#include <cassert>
#line 1 "lib/ac-library/atcoder/maxflow.hpp"
#include <algorithm>
#line 6 "lib/ac-library/atcoder/maxflow.hpp"
#include <limits>
#include <queue>
#line 9 "lib/ac-library/atcoder/maxflow.hpp"
#line 1 "lib/ac-library/atcoder/internal_queue.hpp"
#line 5 "lib/ac-library/atcoder/internal_queue.hpp"
namespace atcoder {
namespace internal {
template <class T> struct simple_queue {
std::vector<T> payload;
int pos = 0;
void reserve(int n) { payload.reserve(n); }
int size() const { return int(payload.size()) - pos; }
bool empty() const { return pos == int(payload.size()); }
void push(const T& t) { payload.push_back(t); }
T& front() { return payload[pos]; }
void clear() {
payload.clear();
pos = 0;
}
void pop() { pos++; }
};
} // namespace internal
} // namespace atcoder
#line 11 "lib/ac-library/atcoder/maxflow.hpp"
namespace atcoder {
template <class Cap> struct mf_graph {
public:
mf_graph() : _n(0) {}
explicit mf_graph(int n) : _n(n), g(n) {}
int add_edge(int from, int to, Cap cap) {
assert(0 <= from && from < _n);
assert(0 <= to && to < _n);
assert(0 <= cap);
int m = int(pos.size());
pos.push_back({from, int(g[from].size())});
int from_id = int(g[from].size());
int to_id = int(g[to].size());
if (from == to) to_id++;
g[from].push_back(_edge{to, to_id, cap});
g[to].push_back(_edge{from, from_id, 0});
return m;
}
struct edge {
int from, to;
Cap cap, flow;
};
edge get_edge(int i) {
int m = int(pos.size());
assert(0 <= i && i < m);
auto _e = g[pos[i].first][pos[i].second];
auto _re = g[_e.to][_e.rev];
return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
}
std::vector<edge> edges() {
int m = int(pos.size());
std::vector<edge> result;
for (int i = 0; i < m; i++) {
result.push_back(get_edge(i));
}
return result;
}
void change_edge(int i, Cap new_cap, Cap new_flow) {
int m = int(pos.size());
assert(0 <= i && i < m);
assert(0 <= new_flow && new_flow <= new_cap);
auto& _e = g[pos[i].first][pos[i].second];
auto& _re = g[_e.to][_e.rev];
_e.cap = new_cap - new_flow;
_re.cap = new_flow;
}
Cap flow(int s, int t) {
return flow(s, t, std::numeric_limits<Cap>::max());
}
Cap flow(int s, int t, Cap flow_limit) {
assert(0 <= s && s < _n);
assert(0 <= t && t < _n);
assert(s != t);
std::vector<int> level(_n), iter(_n);
internal::simple_queue<int> que;
auto bfs = [&]() {
std::fill(level.begin(), level.end(), -1);
level[s] = 0;
que.clear();
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (auto e : g[v]) {
if (e.cap == 0 || level[e.to] >= 0) continue;
level[e.to] = level[v] + 1;
if (e.to == t) return;
que.push(e.to);
}
}
};
auto dfs = [&](auto self, int v, Cap up) {
if (v == s) return up;
Cap res = 0;
int level_v = level[v];
for (int& i = iter[v]; i < int(g[v].size()); i++) {
_edge& e = g[v][i];
if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
Cap d =
self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
if (d <= 0) continue;
g[v][i].cap += d;
g[e.to][e.rev].cap -= d;
res += d;
if (res == up) return res;
}
level[v] = _n;
return res;
};
Cap flow = 0;
while (flow < flow_limit) {
bfs();
if (level[t] == -1) break;
std::fill(iter.begin(), iter.end(), 0);
Cap f = dfs(dfs, t, flow_limit - flow);
if (!f) break;
flow += f;
}
return flow;
}
std::vector<bool> min_cut(int s) {
std::vector<bool> visited(_n);
internal::simple_queue<int> que;
que.push(s);
while (!que.empty()) {
int p = que.front();
que.pop();
visited[p] = true;
for (auto e : g[p]) {
if (e.cap && !visited[e.to]) {
visited[e.to] = true;
que.push(e.to);
}
}
}
return visited;
}
private:
int _n;
struct _edge {
int to, rev;
Cap cap;
};
std::vector<std::pair<int, int>> pos;
std::vector<std::vector<_edge>> g;
};
} // namespace atcoder
#line 8 "tools/bipartite_matching.hpp"
namespace tools {
class bipartite_matching {
public:
struct edge {
::std::size_t id;
::std::size_t from;
::std::size_t to;
};
private:
::std::size_t m_size1;
::std::size_t m_size2;
::atcoder::mf_graph<int> m_graph;
::std::vector<edge> m_edges;
public:
bipartite_matching() = default;
bipartite_matching(const ::tools::bipartite_matching&) = default;
bipartite_matching(::tools::bipartite_matching&&) = default;
~bipartite_matching() = default;
::tools::bipartite_matching& operator=(const ::tools::bipartite_matching&) = default;
::tools::bipartite_matching& operator=(::tools::bipartite_matching&&) = default;
bipartite_matching(const ::std::size_t size1, const ::std::size_t size2) :
m_size1(size1), m_size2(size2), m_graph(size1 + size2 + 2) {
for (::std::size_t i = 0; i < size1; ++i) {
this->m_graph.add_edge(size1 + size2, i, 1);
}
for (::std::size_t i = 0; i < size2; ++i) {
this->m_graph.add_edge(size1 + i, size1 + size2 + 1, 1);
}
}
::std::size_t size1() const {
return this->m_size1;
}
::std::size_t size2() const {
return this->m_size2;
}
::std::size_t add_edge(const ::std::size_t i, const ::std::size_t j) {
assert(i < this->size1());
assert(j < this->size2());
this->m_graph.add_edge(i, this->m_size1 + j, 1);
this->m_edges.emplace_back(edge({this->m_edges.size(), i, j}));
return this->m_edges.size() - 1;
}
const edge& get_edge(const ::std::size_t k) const {
assert(k < this->m_edges.size());
return this->m_edges[k];
}
const ::std::vector<edge>& edges() const {
return this->m_edges;
}
::std::vector<::std::size_t> query() {
::std::vector<::std::size_t> edges;
this->m_graph.flow(this->m_size1 + this->m_size2, this->m_size1 + this->m_size2 + 1);
for (::std::size_t i = 0; i < this->m_edges.size(); ++i) {
if (this->m_graph.get_edge(this->m_size1 + this->m_size2 + i).flow == 1) {
edges.push_back(i);
}
}
return edges;
}
};
}