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. [转载] Android动态加载Dex机制解析

    本文转载自: http://blog.csdn.net/wy353208214/article/details/50859422 1.什么是类加载器? 类加载器(class loader)是 Java ...

  2. 【POJ2699】The Maximum Number of Strong Kings(二分,最大流)

    题意: 有n个队伍,两两都有比赛 知道最后每支队伍获胜的场数 求最多有多少队伍,他们战胜了所有获胜场数比自己多的队伍,这些队伍被称为SK N<=50 思路:把每个队伍和它们两两之间的比赛都当做点 ...

  3. C# 委托和事件 与 观察者模式(发布-订阅模式)讲解 by天命

    使用面向对象的思想 用c#控制台代码模拟猫抓老鼠 我们先来分析一下猫抓老鼠的过程 1.猫叫了 2.所有老鼠听到叫声,知道是哪只猫来了 3.老鼠们逃跑,边逃边喊:"xx猫来了,快跑啊!我是老鼠 ...

  4. 转载:shell脚本之sed使用----替换、变量、转义字符

    sed替换的基本语法为:----s后面跟的是分隔符,原字符串可使用.*这种正则表达式进行整行替换 代码如下: sed 's/原字符串/替换字符串/' 单引号里面,s表示替换,三根斜线中间是替换的样式, ...

  5. Window下Qt Creator启动错误解决方法

    很多电脑现在都是用的是双显卡,高性能的独显和性能比较差但耗电少的集显,在Window10系统下右键点击软件,在"图形处理器"里面可以选择使用什么显卡操作此软件.下面是我在运行Qt ...

  6. 第四十章 微服务CICD(2)- jenkins(war版)

    一.下载 官网下载war包,放在tomcat下的webapps下, 第一章 tomcat安装与启动 第二章 部署war包到tomcat jenkins:2.19.1版本. 二.修改编码为utf-8 在 ...

  7. iOS10 的适配问题,你遇到了吗?导航栏标题和返回按钮神奇的消失了

    苹果系统升级后好多应用都发了新版本来适配,今天就来分享一下我的适配历程. 首先是出现的问题: 1.push一个控制器,返回按钮和标题神奇的消失了,打开三维视图(比较坑的是有的版本老到打不开三维视图 ) ...

  8. 使用re-sign.jar对apk进行重签名

    准备: ① re-sign.jar重签名工具:(下载地址为:http://troido.de/downloads/category/1): ② 待重签名的apk:      重签名步骤: ① 右键re ...

  9. ShellExecuteA

    //第三个参数是指令,可以是一个可执行程序(后面不能加参数).有默认打开方式的文件.路径.网址.各种协议地址如迅雷ftp邮箱ed2k等 MessageBoxA

  10. 使用 hexdump dump 文件内容

    名词解释 [dump] dump 是指把文件的内容,每个字节用2位十六进制数来表示的方式. 缘由 最近看矢泽久雄的<How Program Works>,了解到 dump “exe文件”( ...