【模板】匈牙利算法——二分图最大匹配

#include <iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define maxn 1000
using namespace std;


int nx,ny,match[maxn];
bool vis[maxn],w[maxn][maxn];


bool find(int x){
    for(int i=1;i<=ny;i++){
        if(vis[i]) continue;
        vis[i]=true;
        if(match[i]==-1||find(match[i])){
            match[i]=x;
            return true;
        }
    }
    return false;
}

int suan(){
    int ans=0;
    memset(w,0,sizeof(w));
    memset(match,-1,sizeof(match));
    for(int i=1;i<=nx;i++){
        memset(vis,0,sizeof(vis));
        if(find(i)) ans++;
    }
    return ans;
}

int m,a,b; 

int main(){
    scanf("%d%d",&nx,&ny);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&a,&b);
        w[a][b]=true;
    }
    int out=suan();
    if(out) printf("%d",out);
    else puts("NO!!!!!");
    return 0;
}
posted @ 2016-11-11 14:43  Leo.Tan  阅读(114)  评论(0编辑  收藏  举报