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
65
66
67
68
#pragma once


#include <atcoder/maxflow><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


#include "snippet/aliases.hpp"
#include "snippet/iterations.hpp"

#include "internal/dev_env.hpp"
#include "internal/types.hpp"

#include "adaptor/vector.hpp"


namespace uni {

struct maximum_bipartite_matching {
    // using size_type = internal::size_t;
    using size_type = int;

  protected:
    using MF = atcoder::mf_graph<size_type>;

    size_type _m, _n, _s, _edges = 0;
    MF _mf;

  public:
    explicit maximum_bipartite_matching(const size_type n) noexcept(NO_EXCEPT) : maximum_bipartite_matching(n, n) {}
    maximum_bipartite_matching(const size_type m, const size_type n) noexcept(NO_EXCEPT)
      : _m(m), _n(n), _s(m + n), _mf(m + n + 2)
    {
        REP(i, m) {
            this->_mf.add_edge(this->_s, i, 1);
        }
        REP(i, n) {
            this->_mf.add_edge(m + i, this->_s + 1, 1);
        }
    }

    inline auto& add(const size_type i, const size_type j) noexcept(NO_EXCEPT) {
        assert(0 <= i && i < this->_m);
        assert(0 <= j && j < this->_n);
        this->_mf.add_edge(i, this->_m + j, 1);
        ++this->_edges;
        return *this;
    }

    inline size_type max_matched() noexcept(NO_EXCEPT) {
        return this->_mf.flow(this->_s, this->_s + 1);
    }

    inline auto get_matching() noexcept(NO_EXCEPT) {
        this->max_matched();

        vector<spair<size_type>> res;

        REP(i, this->_edges) {
            const auto edge = this->_mf.get_edge(this->_s + i);
            if(edge.flow == 0) continue;
            res.emplace_back(edge.from, edge.to - this->_m);
        }

        return res;
    }
};

} // namespace uni