0
点赞
收藏
分享

微信扫一扫

【acwing】861. 二分图的最大匹配**

古得曼_63b6 2022-04-01 阅读 59

穿越隧道

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510, M = 1e5 + 10;
int h[N],e[M],ne[M],idx;
bool st[N];
int match[N];
int n1,n2,m;
void add(int a, int b){
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool find(int x){
    for(int i = h[x]; i != -1; i = ne[i]){
        int j = e[i];
        if(!st[j]){
            st[j] = true;
            if(match[j] == 0 || find(match[j])){//如果女生没有对象,或者女生的男朋友有下一个对象,则成功匹配
                match[j] = x;
                return true;
            }
        }
    }
    return false;
}
int main(){
    scanf("%d%d%d",&n1,&n2,&m);
    memset(h,-1,sizeof(h));
    while(m--){
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    int res = 0;
    for(int i = 1; i <= n1; i++){
        memset(st,0,sizeof(st));
        if(find(i)){
           res++; 
        }
    }
    printf("%d\n",res);
    return 0;
}
举报

相关推荐

0 条评论