【模板】二分图最大匹配

https://www.luogu.org/problem/P3386

注意对匈牙利增广路算法的理解

#include<iostream>
#include<cstdio>

#define ri register int
#define u int

namespace opt {

    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<'0'||s>'9') {
            if(s=='-') f=-1;
            s=getchar();
        }
        while(s>='0'&&s<='9') {
            x=(x<<1)+(x<<3)+s-'0';
            s=getchar();
        }
        return x*f;
    }

}

using opt::in;

#define NN 1000005

#include<cstring>

namespace mainstay {
    
    u N,M,E,cnt,vt[NN],to[NN],h[NN];
    
    struct node{
        u to,next;
    }a[NN<<1];
    
    inline void add(const u &x,const u &y){
        a[++cnt].next=h[x],a[cnt].to=y,h[x]=cnt;
    }
    
    u dfs(const u &x){
        if(vt[x]) return 0;
        vt[x]=1;
        for(ri i(h[x]);i;i=a[i].next){
            u _y(a[i].to);
            if(!to[_y]||dfs(to[_y])){
                to[_y]=x;
                return 1;
            } 
        }
        return 0;
    }

    inline void solve() {
        N=in(),M=in(),E=in();
        for(ri i(1);i<=E;++i){
            u _a(in()),_b(in());
            if(_a<=N&&_b<=M) add(_a,_b);
        }
        u ans(0);
        for(ri i(1);i<=N;++i){
            std::memset(vt,0,sizeof(vt));
            if(dfs(i)) ++ans;
        }
        printf("%d",ans);
    }

}

int main() {

    //freopen("x.txt","r",stdin);
    mainstay::solve();

}
原文地址:https://www.cnblogs.com/ling-zhi/p/11865993.html