zoukankan      html  css  js  c++  java
  • POJ 1274 The Perfect Stall(二分图最大匹配)

    题意:

    N头牛M个牛棚,每只牛都有它自己指定的若干个它愿意呆的牛棚。

    每个牛棚最多呆一头牛。

    问最多可以满足多少头牛的愿望。

    思路:

    裸二分图最大匹配。

    代码:

    int n,m;
    vector<int> graph[205];
    int cx[205],cy[205];
    bool bmask[205];
    
    int findPath(int u){
        int L=graph[u].size();
        rep(i,0,L-1){
            int v=graph[u][i];
            if(!bmask[v]){
                bmask[v]=true;
                if(cy[v]==-1||findPath(cy[v])){
                    cy[v]=u;
                    cx[u]=v;
                    return 1;
                }
            }
        }
        return 0;
    }
    int MaxMatch(){
        int ans=0;
        rep(i,1,n) cx[i]=-1;
        rep(i,1,m) cy[i]=-1;
        rep(i,1,n) if(cx[i]==-1){
            mem(bmask,false);
            ans+=findPath(i);
        }
        return ans;
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            rep(i,1,n) graph[i].clear();
            rep(i,1,n){
                int x,tx;
                scanf("%d",&x);
                while(x--){
                    scanf("%d",&tx);
                    graph[i].push_back(tx);
                }
            }
            int dd=MaxMatch();
            printf("%d
    ",dd);
        }
    }
  • 相关阅读:
    HGE tutorial04
    HGE tutorial03
    HGE tutorial02 plus
    HGE tutorial02
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
  • 原文地址:https://www.cnblogs.com/fish7/p/4094894.html
Copyright © 2011-2022 走看看