library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub firiexp/library

:heavy_check_mark: graph/bipartite_matching_lexmin.cpp

Depends on

Verified with

Code

#include "./bipartite_matching.cpp"
class Bipartite_Matching_LexMin : public Bipartite_Matching {
public:
    using Bipartite_Matching::Bipartite_Matching;

    int solve_LexMin() { // check sorted edge no
        int res = matching();
        for (int i = 0; i < l; ++i) {
            if(!~match[i]) continue;
            match[match[i]] = -1;
            match[i] = -1;
            ++t;
            dfs(i);
            alive[match[i]] = 0;
            alive[i] = 0;
        }
        return res;
    }
};
#line 1 "graph/bipartite_matching.cpp"
class Bipartite_Matching {
protected:
    vector<vector<int>> G;
    vector<int> used, alive;
    int t;
    int l, r;
public:
    vector<int> match;
    explicit Bipartite_Matching(int l, int r): l(l), r(r), t(0), G(l+r), used(l+r, 0), alive(l+r, -1), match(l+r, -1) {};

    void add_edge(int a, int b){
        G[a].emplace_back(b+l);
        G[b+l].emplace_back(a);
    }

    bool dfs(int x){
        used[x] = t;
        for (auto &&i : G[x]) {
            int w = match[i];
            if(alive[i] == 0) continue;
            if(w == -1 || (used[w] != t && dfs(w))){
                match[x] = i;
                match[i] = x;
                return true;
            }
        }
        return false;
    }

    int matching() {
        int ans = 0;
        for (int i = 0; i < G.size(); ++i) {
            if(alive[i] == 0) continue;
            if(match[i] == -1) {
                ++t;
                ans += dfs(i);
            }
        }
        return ans;
    }
};

/**
 * @brief 二部グラフの最大マッチング
 * @docs _md/bipartite_matching.md
 */
#line 2 "graph/bipartite_matching_lexmin.cpp"
class Bipartite_Matching_LexMin : public Bipartite_Matching {
public:
    using Bipartite_Matching::Bipartite_Matching;

    int solve_LexMin() { // check sorted edge no
        int res = matching();
        for (int i = 0; i < l; ++i) {
            if(!~match[i]) continue;
            match[match[i]] = -1;
            match[i] = -1;
            ++t;
            dfs(i);
            alive[match[i]] = 0;
            alive[i] = 0;
        }
        return res;
    }
};
Back to top page