[Bipartite graph] Multiple matching of bipartite graph

In some problems, you will encounter a one-to-many bipartite graph model, which allows one element in set Y to match multiple elements in set X (usually with a maximum limit n)

/* Bipartite graph multiple matching algorithm */ 
const  int MAXN= 1001 ; // Maximum number of vertices 
int bmap[MAXN][MAXN]; // Bipartite graph 
bool bmask[MAXN]; // Look for the flag array int of the augmented path
 nx,ny; // The number of vertices in the left and right sets int vcy[MAXN]; // vcy[i], the number of vertices in the right set i vertices matched to the left set int cy[MAXN][MAXN]; // cy[i] [j] The jth element matched by the right set i vertex int limit; // The maximum number of left set vertices matched by each right set vertex int left,right; // Used for binary search 
/* find augmentation path */ bool findpath( int




 u){
    int i,j;
    for(i=0;i<ny;i++){
        if(bmap[u][i]&&!bmask[i]){
            bmask[i]=1;
            if(vcy[i]<limit){
                cy[i][vcy[i]++]=u;
                return true;
            }
            for(j=0;j<vcy[i];j++){
                if(findpath(cy[i][j])){
                    cy[i][j]=u;
                    return true;
                }
            }
        }
    }
    return false;
}
/* Find multiple matches */ 
bool MulMath(){
    memset(vcy,0,sizof(vcy));
    for(int i=0;i<nx;i++){
        memset(bmask,0,sizof(bmask));
        if(!findpath(i)) return false;
    }
    return true;
}
int main(){
    /*coding*/
    left=0;
    right=nx;
    while(left<right){
        limit=(left+right)/2;
        if(MulMath) right=limit;
        else l=limit+1;
    }
    /*coding*/
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326298451&siteId=291194637