二分图最大匹配模板

也称匈牙利算法。这里使用的邻接表的数据结构时间复杂度为O(n*m)空间复杂度为O(n+m)
板子如下:

struct node{
    int v,next;
}edge[maxn];
int pre[maxn],l,vis[maxn],match[maxn];
int tot,n,m;
void init(){
    l=0;
    memset(pre,-1,sizeof pre);
    memset(match,-1,sizeof match);
}
void add(int u,int v){
    edge[l].v=v;
    edge[l].next=pre[u];
    pre[u]=l++;
}
int dfs(int u){
    for(int i=pre[u];i+1;i=edge[i].next){
        int v=edge[i].v;
        if(!vis[v]){
            vis[v]=1;
            if(match[v]==-1||dfs(match[v])){
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int hungary(){
    tot=0;
    for(int i=1;i<=max(n,m);i++){
        cle(vis);
        if(dfs(i))tot++;
    }
    return tot; 
}

使用时注意二分图是有向还是无向的。

二分图的最小顶点覆盖=最大匹配数

http://acm.hnu.cn/online/?action=problem&type=show&id=13449&courseid=0

匈牙利算法讲解

http://fengweiding.blog.163.com/blog/static/23005412120151258049532/

原文地址:https://www.cnblogs.com/pk28/p/4857558.html