zoukankan      html  css  js  c++  java
  • poj p1274——The Perfect Stall(完美的牛栏)

    Description

    Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall. 
    Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible. 

    Input

    The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

    Output

    For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

    Sample Input

    5 5
    2 2 5
    3 2 3 4
    2 1 5
    3 1 2 5
    1 2 
    

    Sample Output

    4



    裸的匈牙利算法,每次找匹配,如果i结点还没有被匹配则匹配上并标记,return true
    如果i结点被匹配了,则找i结点的匹配点x是否还有别的匹配点,再依次向下更新标记。
    return true的话则表示多了一种匹配,ans++
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cmath>
     5 using namespace std;
     6 const int maxn=205;
     7 bool map[maxn][maxn];
     8 int n,m;//奶牛数,牛栏数 
     9 bool vis[maxn];
    10 int match[maxn];
    11 bool dfs(int x)
    12 {
    13     for(int i=1;i<=m;i++)
    14     {
    15         if(map[x][i]&&!vis[i])
    16         {
    17             vis[i]=true;
    18             if(match[i]==-1||dfs(match[i]))
    19             {
    20                 match[i]=x;
    21                 return true;
    22             }
    23         }
    24     }
    25     return false;
    26 }
    27 void hungary()
    28 {
    29     int count=0;
    30     for(int i=1;i<=n;i++)
    31     {
    32         memset(vis,false,sizeof(vis));
    33         if(dfs(i))count++;
    34     }
    35     
    36     printf("%d
    ",count);
    37     return ;
    38 }
    39 void gg()
    40 {
    41     //freopen("1.in","r",stdin);
    42     while(scanf("%d%d",&n,&m)==2)
    43     {
    44         memset(map,false,sizeof(map));
    45         memset(match,-1,sizeof(match));
    46         for(int i=1;i<=n;i++)
    47         {
    48             int x;scanf("%d",&x);
    49             for(int j=1;j<=x;j++)
    50             {
    51                 int k;
    52                 scanf("%d",&k);
    53                 map[i][k]=true;
    54             }
    55         }
    56         hungary();
    57     }
    58     return ;
    59 }
    60 int main()
    61 {
    62     gg();
    63     return 0; 
    64 }
    
    
    
     
  • 相关阅读:
    解决阿里云服务器磁盘报警
    linux服务器启动报错UNEXPECTED INCONSISTENCY解决方法
    记一次gitlab添加用户收不到邮件的解决办法
    php7安装redis拓展
    centos6.5安装部署zabbix监控服务端和客户端
    centos-6.5安装部署LNMP环境
    centos6.5编译安装php7
    centos6.5新增加硬盘挂载并实现开机自动挂载
    简单快速部署samba服务器
    第177天:常用正则表达式(最全)
  • 原文地址:https://www.cnblogs.com/937337156Zhang/p/6044638.html
Copyright © 2011-2022 走看看