The Perfect Stall
Hal Burch

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.

PROGRAM NAME: stall4

INPUT FORMAT

Line 1: One line with 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.
Line 2..N+1: N lines, each corresponding 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.

SAMPLE INPUT (file stall4.in)

  1. 5 5
  2. 2 2 5
  3. 3 2 3 4
  4. 2 1 5
  5. 3 1 2 5
  6. 1 2

OUTPUT FORMAT

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

SAMPLE OUTPUT (file stall4.out)

  1. 4
  2.  
  3. ——————————————————————————————————
    二分图匹配匈牙利算法模板
    算法简述:
    对于当前点x,选一个点y进行匹配,如果这个点y与另一点x'匹配,进行递归知道为x'找到另一个分配或无法找到另一分配
  1. /*
  2. ID:ivorysi
  3. PROG:stall4
  4. LANG:C++
  5. */
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <queue>
  10. #include <set>
  11. #include <vector>
  12. #include <cmath>
  13. #define inf 0x7fffffff
  14. #define ivorysi
  15. #define siji(i,x,y) for(int i=(x);i<=(y);++i)
  16. #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
  17. #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
  18. #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
  19. #define p(x) (x)*(x)
  20. using namespace std;
  21. vector<int> g[];
  22. int used[],from[];
  23. int n,m,ans;
  24. bool find(int x){
  25. used[x]=;
  26. for(int i=;i<g[x].size();++i) {
  27. if(from[g[x][i]]) {
  28. if(used[from[g[x][i]]]== && find(from[g[x][i]])) {
  29. from[g[x][i]]=x;
  30. return true;
  31. }
  32. }
  33. else {
  34. from[g[x][i]]=x;
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. void init() {
  41. scanf("%d%d",&n,&m);
  42. int s,b;
  43. siji(i,,n) {
  44. scanf("%d",&s);
  45. siji(j,,s) {
  46. scanf("%d",&b);
  47. g[i].push_back(b+);
  48. }
  49. }
  50.  
  51. }
  52. void solve() {
  53. init();
  54. siji(i,,n) {
  55. memset(used,,sizeof(used));
  56. if(find(i)) ++ans;
  57. }
  58. printf("%d\n",ans);
  59. }
  60. int main(int argc, char const *argv[])
  61. {
  62. #ifdef ivorysi
  63. freopen("stall4.in","r",stdin);
  64. freopen("stall4.out","w",stdout);
  65. #else
  66. freopen("f1.in","r",stdin);
  67. #endif
  68. solve();
  69. return ;
  70. }
  1.  

USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)的更多相关文章

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

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

  2. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. Codevs 1222 信与信封问题 二分图匹配,匈牙利算法

    题目: http://codevs.cn/problem/1222/ 1222 信与信封问题   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 钻石 Diamond 题解 ...

  4. BZOJ1433 [ZJOI2009]假期的宿舍 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8372785.html 题目传送门 - BZOJ1433 题解 我们理一理题目. 在校的学生,有自己的床,还可以睡 ...

  5. HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色

    原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...

  6. (转)二分图匹配匈牙利算法与KM算法

    匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...

  7. BZOJ1059 [ZJOI2007]矩阵游戏 二分图匹配 匈牙利算法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1059 题意概括 有一个n*n(n<=200)的01矩阵,问你是否可以通过交换整行和整列使得左 ...

  8. 网络流24题 第三题 - CodeVS1904 洛谷2764 最小路径覆盖问题 有向无环图最小路径覆盖 最大流 二分图匹配 匈牙利算法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - CodeVS1904 题目传送门 - 洛谷2764 题意概括 给出一个有向无环图,现在请你求一些路径,这些路径 ...

  9. 矩阵游戏|ZJOI2007|BZOJ1059|codevs1433|luoguP1129|二分图匹配|匈牙利算法|Elena

    1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 162 MB Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩 ...

随机推荐

  1. 安装Visual Source Safe 2005 - 初学者系列 - 学习者系列文章

    本文介绍微软的文档管理工具Visual Source Safe 2005的安装 从下列地址获取该工具: ed2k://|file|en_vss_2005.iso|108048384|C4BEC1EC3 ...

  2. Oracle左连接,右连接

    Oracle左连接,右连接 数据表的连接有: 1.内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现 2.外连接: 包括 (1)左外连接(左边的表不加限制) (2)右外连接(右边的表不加限制 ...

  3. Memcached在.Net中的基本操作

    Memcached在.Net中的基本操作 一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅 ...

  4. Orchard Logging

    Orchard 刨析:Logging 最近事情比较多,有预研的,有目前正在研发的,都是很需要时间的工作,所以导致这周只写了两篇Orchard系列的文章,这边不能保证后期会很频繁的更新该系列,但我会写完 ...

  5. C#继承关系中【方发表】的创建和调用

    —C#继承关系中[方发表]的创建和调用 Insus.NET实现一个最炫最原创的验证码.你可以从下面的一步一步的演译. 实现一个验证码,需要了解的是,它最基本是随机产生字符串:<在ASP.NET ...

  6. [置顶] 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!

    前言 FineUI控件库发展至今已经有 5 个年头,目前论坛注册的QQ会员 5000 多人,捐赠用户 500 多人(捐赠用户转化率达到10%以上,在国内开源领域相信这是一个梦幻数字!也足以证明Fine ...

  7. 动手Jquery插件

    自己动手Jquery插件 最近Web应用程序中越来越多地用到了JQuery等Web前端技术.这些技术框架有效地改善了用户的操作体验,同时也提高了开发人员构造丰富客户 端UI的效率.JQuery本身提供 ...

  8. Dump Checking

    Dump Checking Debug相关的一些小技巧 摘要: 1. 如何Debug一个进程的子进程? 答: 使用WinDBG attach到父进程, 然后输入命令".childdbg 1& ...

  9. 自然语言处理(NLP)常用开源工具总结(转)

    ..................................内容纯转发+收藏................................... 学习自然语言这一段时间以来接触和听说了好多开 ...

  10. YPreLoad

    Javascript库   发布我的控件系列:图片预加载控件YPreLoad v1.0 摘要: 介绍大家好!很高兴向大家介绍我的图片预加载控件YPreLoad.它可以帮助您预加载图片,并且能显示加载的 ...