The Perfect Stall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22809   Accepted: 10161

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

Source

 

最简单的匈牙利算法
直接套两个模块就行了
之前把n,m搞反了
题意:其实n指的是牛的头数,而m是牛栏,接下来n行意思是第i头牛愿意在哪些牛栏产奶
实际就是一个最大匹配问题
AC代码
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int cow[];
int fence[];
int map[][];
int match[];
bool visited[];
int count;
int n,m;
bool DFS(int k)
{
for(int i=;i<=m;i++)
if(map[k][i]&&!visited[i])
{
visited[i]=true;
if(match[i]==-||DFS(match[i]))
{
match[i]=k;
return true;
}
}
return false;
}
void hungary()
{
count=;
for(int i=;i<=n;i++)
{
memset(visited,,sizeof(visited));
if(DFS(i))count++;
}
cout<<count<<endl;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
memset(map,,sizeof(map));
for(int i=;i<=n;i++)//n,m读题!!!
{int f;
cin>>f;
for(int j=;j<=f;j++)
{int c;
cin>>c;
map[c][i]=;// 牛指向牛栏
}
}
memset(match,-,sizeof(match));
hungary();
}
return ;
}

poj1274(匈牙利算法)的更多相关文章

  1. poj1274 匈牙利算法 二分图最大匹配

    poj1274 题意: 有n个奶牛, m个畜舍, 每个畜舍最多装1头牛,每只奶牛只有在自己喜欢的畜舍里才能产奶. 求最大产奶量. 分析: 其实题意很明显, 二分图的最大匹配, 匈牙利算法. #incl ...

  2. POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17895   Accepted: 814 ...

  3. POJ1274 The Perfect Stall 二分图,匈牙利算法

    N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙 ...

  4. POJ 1325 && 1274:Machine Schedule 匈牙利算法模板题

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12976   Accepted: 5529 ...

  5. ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)

    //匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring&g ...

  6. 匈牙利算法——S.B.S.

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...

  7. 匈牙利算法与KM算法

    匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...

  8. HDU1054 Strategic Game——匈牙利算法

    Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...

  9. 匈牙利 算法&模板

    匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...

随机推荐

  1. Apache伪静态在网站目录没有反斜杠后自动添加反斜杠

    第一步:确认网站开启REWRITE规则 一般有两种情况: i.apache安装的时候已经包含rewrite功能 ii.后续配置的时候新添加mod_rewrite.so.这种情况需要在httpd.con ...

  2. 更改XAMPP中MySQL数据库的端口号

    更改XAMPP中MySQL数据库的端口号 如果电脑上已安装MySql数据库,还想用XAMPP中自带的数据库就需要更改XAMPP中数据库的端口号,避免和已安装的数据库冲突.本例以更改为3307端口号为例 ...

  3. oracle 数据库学习3 --oracle 数据库中常使用的函数

    1.字符函数: ASCII(X) -->返回X字符的ASCII码 concat(x,y)-->将y连接到X上输出 select concat(first_name,last_name) f ...

  4. 【Python全栈笔记】05 [模块二] 19 Oct 文件的操作

    文件操作 一般步骤1. 文件打开 2. 文件操作 3. 文件关闭 1. 打开文件 使用open(文件名(绝对路径), 打开模式, 编码) 文件打开的模式有: r:  只读模式(默认) w: 只写模式 ...

  5. python自省函数getattr的用法

    getattr是python里的一个内建函数 getattr()这个方法最主要的作用是实现反射机制.也就是说可以通过字符串获取方法实例.这样,你就可以把一个类可能要调用的方法放在配置文件里,在需要的时 ...

  6. include,import,@class的区别

    1.#include与#import功能一样,都是导入头文件 2.区别是#include是单纯导入头文件,如果重复导入头文件,头文件就被导入多分 3.#import在导入头文件之前会检查之前是否导入过 ...

  7. [刘阳Java]_MyBatis_映射文件的resultMap标签入门_第4讲

    <resultMap>:用于解决实体类中属性和表字段名不相同的问题 id:表示当前<resultMap>标签的唯一标识 result:定义表字段和实体类属性的对应关系 prop ...

  8. js 基本类型与引用类型的区别

    1. 基本类型: string,number,boolean,null,undefined 2. 引用类型: Function,Array,Object 访问方式                   ...

  9. List集合基于某个字段排序

    using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Product { publ ...

  10. python模拟登陆知乎并爬取数据

    一些废话 看了一眼上一篇日志的时间 已然是5个月前的事情了 不禁感叹光阴荏苒其实就是我懒 几周前心血来潮想到用爬虫爬些东西 于是先后先重写了以前写过的求绩点代码 爬了草榴贴图,妹子图网,后来想爬婚恋网 ...