Skip to content

匈牙利算法求解二分图 ​

cpp
int st[N],timestamp;//表示当轮是否被匹配过,bool改用时间戳
int match[N];//表示匹配的对象
bool find(int u){
    for(auto v:adj[u]){
        if(st[v]!=timestamp){
            st[v]=timestamp;
            if(match[v]==0||find(match[v])){
                //没被匹配或者找到对应的增广路
                match[v]=u;
                return true;
            }
        }
    }
    return false;
}
int main(){
    int res=0;
    for(int i=1;i<=n;i++){
        timestamp++;
        if(find(i))res++;
    }
    cout<<res<<"\n";
    return 0;
}