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

    【题目链接】 http://poj.org/problem?id=1274

    【题目大意】

      给出一些奶牛和他们喜欢的草棚,一个草棚只能待一只奶牛,
      问最多可以满足几头奶牛

    【题解】

      奶牛和喜欢的草棚连线,做二分图匹配即可

    【代码】

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <vector> 
    using namespace std;
    const int MAX_V=1000;
    int V,match[MAX_V];
    vector<int> G[MAX_V];
    bool used[MAX_V];
    void add_edge(int u,int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    bool dfs(int v){
        used[v]=1;
        for(int i=0;i<G[v].size();i++){
            int u=G[v][i],w=match[u];
            if(w<0||!used[w]&&dfs(w)){
                match[v]=u;
                match[u]=v;
                return 1;
            }
        }return 0;
    }
    int bipartite_matching(){
        int res=0;
        memset(match,-1,sizeof(match));
        for(int v=0;v<V;v++){
            if(match[v]<0){
                memset(used,0,sizeof(used));
                if(dfs(v))res++;
            }
        }return res;
    }
    int N,M,k,x;
    void solve(){
        V=N+M;
        for(int i=0;i<V;i++)G[i].clear();
        for(int i=0;i<N;i++){
            scanf("%d",&k);
            for(int j=0;j<k;j++){
                scanf("%d",&x);
                add_edge(i,N+x-1);
            }
        }printf("%d
    ",bipartite_matching());
    }
    int main(){
        while(~scanf("%d%d",&N,&M)){
            solve();
        }return 0;
    }
  • 相关阅读:
    boot.asm
    C talk
    C 数据类型
    Locks, Deadlocks, and Synchronization
    C++的RTTI 观念和用途
    setup.asm
    驱动对象设备对象设备栈
    JNI 内存泄漏
    KMP 字符串匹配算法
    解开 Windows 下的临界区中的代码死锁
  • 原文地址:https://www.cnblogs.com/forever97/p/poj1274.html
Copyright © 2011-2022 走看看