P3386 【模板】二分图匹配 ·匈牙利算法 / 网络流

题意:求最大匹配

匈牙利算法入门

匈牙利算法 O ( n m ) O(nm) O(nm)

#include 
using namespace std;
const int N=1e3+10;
const int INF=0x3f3f3f3f;

int n,m,k;
namespace Match{
     //匈牙利算法
    
    int set_a;//集合a的大小
    int set_b;//集合b的大小
    
    int vis[N][N];//建图
    int matched[N];//是否已经匹配过
    int ask[N];//本次访问 该点是否被访问过

    bool found(int u){
     //dfs找增光路
        for (int i = 1; i <= set_b; ++i) {
     
            if(vis[u][i]){
     
                if(ask[i])continue;
                ask[i]=1;
                if(!matched[i] || found(matched[i])){
     
                    matched[i]=u;
                    return true;
                }
            }
        }
        return false;
    }

    int match(){
     
        int res=0;
        memset(matched, 0, sizeof(matched));
        for (int i = 1; i <= set_a; ++i) {
     
            memset(ask, 0, sizeof(ask));
            if(found(i)){
     
                res++;
            }
        }
        return res;
    }

    void init(){
     
        set_a=n;
        set_b=m;
    }
}
using namespace Match;

int main(){
     
    ios::sync_with_stdio(0);

    cin>>n>>m>>k;
    set_a=n;
    set_b=m;
    for (int i = 1,u,v; i <= k; ++i) {
     
        cin>>u>>v;
        if(u>n || v>m)continue;
        vis[u][v]=1;
    }
    cout <<match() << endl;
    
    return 0;
}

后来想想,最大匹配网络流它不香吗?

网络流 O ( n m ) O(n\sqrt{m}) O(nm )

#include 
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;

int n,m,k;
namespace Network_flows {
      //网络流板子
    //设定起点和终点
    int st;//起点-源点
    int ed;//终点-汇点

    struct egde {
     
        int to, next;
        int flow;//剩余流量
        //int capacity;//容量
    } e[N * 2];

    int head[N], tot = 1;

    void add(int u, int v, int w) {
     
        e[++tot] = {
     v, head[u], w};
        head[u] = tot;
        e[++tot] = {
     u, head[v], 0};
        head[v] = tot;//网络流反相边流量为0
    }

    int dep[N];//dep[]=-1时为炸点
    queue<int> q;

    bool bfs() {
     
        memset(dep, 0, sizeof(dep));//顺便起到vis的功能
        q.push(st);
        dep[st] = 1;
        while (!q.empty()) {
     
            int u = q.front();
            q.pop();
            for (int i = head[u]; i; i = e[i].next) {
     
                int v = e[i].to;
                if (!dep[v] && e[i].flow) {
     
                    dep[v] = dep[u] + 1;
                    q.push(v);
                }
            }
        }
        return dep[ed];
    }

    int dfs(int u, int Flow) {
     
        if (u == ed) return Flow;
        int now_flow = 0;//跑残流
        for (int i = head[u]; i; i = e[i].next) {
     
            int v = e[i].to;
            if (dep[v] == dep[u] + 1 && e[i].flow) {
     
                int f = dfs(v, min(Flow - now_flow, e[i].flow));
                e[i].flow -= f;
                e[i ^ 1].flow += f;
                now_flow += f;
                if (now_flow == Flow) return Flow;
            }
        }
        if (now_flow == 0)dep[u] = -1;
        return now_flow;
    }

	#define max_flow dinic
    int dinic() {
     //最大流
        int res = 0;
        while (bfs()) {
     
            res += dfs(st, INF);
        }
        return res;
    }

    void init() {
     
        tot = 1;
        memset(head, 0, sizeof(head));
        while (!q.empty()) q.pop();
    }
}
using namespace Network_flows;

int main(){
     
    ios::sync_with_stdio(0);

    cin>>n>>m>>k;
    st=0,ed=n+m+1;
    for (int i = 1,u,v; i <= k; ++i) {
     
        cin>>u>>v;
        if(u>n || v>m)continue;
        add(u,v+n,INF);// u的编号1~n 为区分v点(1~m) 加上一个基准值
    }
    for (int i = 1; i <= n; ++i) {
     
        add(st,i,1);
    }
    for (int i = 1; i <= m; ++i) {
     
        add(i+n,ed,1);
    }

    cout <<dinic() << endl;
    return 0;
}

你可能感兴趣的:(板子,洛谷)