zoukankan      html  css  js  c++  java
  • POJ 1466 Girls and Boys(二分图匹配)

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

    【题目大意】

      给出一些人和他们所喜欢的人,两个人相互喜欢就能配成一对,
      问最后没有配对的人的最少数量

    【题解】

      求最少数量,就是最多匹配的补集,因此做一遍二分图匹配即可。

    【代码】

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <vector> 
    using namespace std;
    const int MAX_V=1000;
    const int INF=0x3f3f3f3f;
    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,x,k,y;
    void init(){
        V=N;
        for(int i=0;i<V;i++)G[i].clear();
        for(int i=0;i<N;i++){
            scanf("%d: (%d)",&x,&k);
            for(int i=0;i<k;i++){
                scanf("%d",&y);
                add_edge(x,y);
            }
        }
    }
    void solve(){
        printf("%d
    ",N-bipartite_matching());
    }
    int main(){
        while(~scanf("%d",&N)){
            init();
            solve();
        }return 0;
    }
  • 相关阅读:
    日期正则表达式yyyyMMdd
    Markdown语法
    su: Authentication failure问题
    Git初始配置
    layui的layer.open()方法查看缩略图 原图缩放
    入驻博客园三年
    php学习笔记之动态生成一组单选button
    opencv直方图拉伸
    c++ 中const的使用
    LeetCode131:Palindrome Partitioning
  • 原文地址:https://www.cnblogs.com/forever97/p/poj1466.html
Copyright © 2011-2022 走看看