首页 > 其他 > 详细

【模板】二分图最大匹配

时间:2019-11-15 16:19:26      阅读:85      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!