二分图匹配(匈牙利算法模板)

刚写了网络流,再写一个二分图匹配经典算法,一样能解决二分图匹配问题

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int m,n,e;
int nex[999999];
int to[999999];
int head[999999];
int cos[99999];
int tot;
int vis[99999];
int match[999999];
int add(int x,int y)
{
    nex[++tot]=head[x];
    to[tot]=y;
    head[x]=tot;
}
int spfa(int x){
    for(int i=head[x];i;i=nex[i]){
        int tmp=to[i];
        if(!vis[tmp])
        {
        vis[tmp]=1;
        if(!match[tmp]||spfa(match[tmp]))
        {
            match[tmp]=x;
            match[x]=tmp;

            return 1;
        }   
        }
    }
    return 0;
}
int main()
{

    scanf("%d%d%d",&n,&m,&e);
    for(int i=1;i<=e;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        if(y<=m)
        {
            add(x,y+n);
        add(y+n,x);
        }
    }int sum=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n+m;j++)
        vis[j]=0;
        if(spfa(i)) sum++;
    }
    printf("%d",sum);

}
原文地址:https://www.cnblogs.com/wspl98765/p/6819869.html