HDU 2063 匈牙利算法

https://vjudge.net/problem/HDU-2063

二分图找最大匹配入门

#include<bits/stdc++.h>
using namespace std;
const int N=2e3+10;
int head[N],ver[N],Next[N],v[N],match[N];
int tot=0;
void add(int x,int y){
    ver[++tot]=y;
    Next[tot]=head[x];
    head[x]=tot;
}

bool dfs(int x){
    for(int i=head[x],y;i;i=Next[i]){
        if(!v[y=ver[i]]){
            v[y]=1;
            if(match[y]==0||dfs(match[y])){
                match[y]=x;
                return true;
            }
        }
    }
    return false;
}
int main(){
    int n,a,b,x,y;
    while(cin>>n&&n){
        tot=0;
        for(int i=0;i<N;++i){
            head[i]=ver[i]=Next[i]=v[i]=match[i]=0;
        }
        cin>>a>>b;
        for(int i=1;i<=n;++i){
            cin>>x>>y;
            add(x,y);
        }
        int ans=0;
        for(int i=1;i<=a;++i){
            memset(v,0,sizeof v);
            if(dfs(i)) ++ans;
        }
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/gipsy_danger/article/details/80254719