0
点赞
收藏
分享

微信扫一扫

acwing 861 二分图的最大匹配 2022/03/08

敬亭阁主 2022-03-12 阅读 73

遍历男生,把女生清空,表示这些女生都还没有考虑过

如果这个男生找到了一个女生,最后输出结果

#include<iostream>
#include<cstring>

using namespace std ;
const int N = 510 , M = 100010 ;
int n1, n2 , m ;
int e[M] , h[N] , ne[M] , idx ;
int match[N] ;
bool st[N] ;

void add(int a , int b)
{
    e[idx] = b , ne[idx] = h[a] , h[a] = idx++ ;
}
void init()
{
    memset(h, -1 , sizeof h)  ;
}

int 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] || find(match[j]))
            {
                match[j] = x ;
                return true ;
            }
        }
    }
    return false ;
}
int main()
{
    init() ;
    cin >> n1 >> n2 >> m ;
    while(m--)
    {
        int a , b ;
        cin >> a >> b ;
        add(a , b);
    }
    int res = 0 ;
    for(int i = 1 ; i <= n1 ; i++)
    {
         memset(st , false , sizeof st) ;
         if(find(i)) res ++ ;
    }
    cout << res ;
    return 0 ;
}
举报

相关推荐

0 条评论