2019独角兽企业重金招聘Python工程师标准>>>

The Perfect Stall

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16888 Accepted: 7721

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

USACO 40

我的解答:

二分图匹配.匈牙利算法.

/*=============================================================================
#     FileName: 1274.cpp
#         Desc: poj 1274
#       Author: zhuting
#        Email: cnjs.zhuting@gmail.com
#     HomePage: my.oschina.net/locusxt
#      Version: 0.0.1
#    CreatTime: 2013-12-07 15:54:43
#   LastChange: 2013-12-07 15:54:43
#      History:
=============================================================================*/
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#define maxn 205bool mymap[maxn][maxn] = {0};/*记录是否两点相连*/
int link[maxn] = {0};/*记录右边的点所连接的点,没有时置-1*/
bool cover[maxn] = {0};/*记录右边的某个点有没有被覆盖,防止死循环*/
int n = 0, m = 0;bool find (int x)/*寻找左边的点*/
{for (int i = 0; i < m; ++i)/*对右边的点进行遍历*/{if (!cover[i] && mymap[x][i])/*如果该右点没有被覆盖,并且与左点x之间有线相连*/{cover[i] = 1;if (link[i] == -1 || find(link[i])){link[i] = x;return 1;}}}return 0;
}void init()
{memset(mymap, 0, sizeof(mymap));memset(cover, 0, sizeof(cover));memset(link, 0xff, sizeof(link));return;
}int main()
{int link_num = 0;int stall = 0;while (scanf("%d%d", &n, &m) != EOF){int ans = 0;init();for (int i = 0; i < n; ++i){scanf("%d", &link_num);for (int j = 0; j < link_num; ++j){scanf("%d", &stall);mymap[i][stall - 1] = 1;}}for (int i = 0; i < n; ++i)/*遍历所有左点*/{memset(cover, 0, sizeof(cover));if (find(i))++ans;}printf("%d\n", ans);}return 0;
}

第一道二分图,匈牙利算法

转载于:https://my.oschina.net/locusxt/blog/182353

poj 1274 The Perfect Stall相关推荐

  1. 【Step1】【二分图匹配】poj 1274-The Perfect Stall

    题目链接 题目大意 输入数据中,第一行给出n,表示n个奶牛. 接下来n行,每行一个x,xi表示第i头奶牛可以选择x个谷仓中的一个进行匹配.接下来x个数,表示谷仓的编号(1~n之间) 一个谷仓也只能有一 ...

  2. POJ1274 The Perfect Stall(二分图)

    题意: 一些奶牛只有在特定的围栏中才能产奶,要求合理安排使能产奶的奶牛数达到最大. 要点: 二分图裸题,最近刚学了二分图,看下面的参考博客,写的比较好: 参考博客:匈牙利算法 15479500 Sea ...

  3. usaco The Perfect Stall(二分匹配模板)

    其实二分匹配都是很早之前看的不过又忘了现在再看看比第一次好理解多了. /* ID:jinbo wu TASK:stall4 LANG:C++ */ #include<bits/stdc++.h& ...

  4. Poj_1274 The Perfect Stall -二分图裸题

    题目:给牛找棚,每个棚只能容一只牛,牛在对应的棚才能产奶,问最多能让几只牛产奶. /************************************************ Author :D ...

  5. [USACO4.2]完美的牛栏The Perfect Stall

    https://www.luogu.org/problemnew/show/P1894 题解:网络流+最大流 /* *@Author: STZG *@Language: C++ */ #include ...

  6. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  7. [USACO 4.2.2] The Perfect Stall 完美的牛栏

    题目链接 匈牙利算法模板题 #include <iostream> #include <cstring> #include <cstdlib> #include & ...

  8. POJ(1274) 完美的牛棚 {网络流 + 匈牙利求最大匹配}

    题目: 思路一:网络流: 增加超源超,构建网络求解最大流即可! 思路二:匈牙利求最大匹配: 将每头牛与其喜爱的牛棚都连接一条容量为1的边,用匈牙利算法,求最大匹配. 一共V个顶点,边集为E,时间复杂度 ...

  9. poj 1469 COURSES 解题报告

    题目链接:http://poj.org/problem?id=1469 题目意思:有 N 个人,P个课程,每一个课程有一些学生参加(0个.1个或多个参加).问 能否使得 P 个课程 恰好与 P 个学生 ...

最新文章

  1. numpy、matplot、sklearn的安装与使用
  2. python绘制三维散点图-python 画三维图像 曲面图和散点图的示例
  3. zabbix报警收到tcmime.1456.1456.1878.bin附件邮件
  4. hibernate基于单表curd
  5. 数字图像处理 空间域高斯低通滤波 MATLAB实验
  6. python学习-- django 2.1.7 ajax 请求 进阶版
  7. 如何使用@vue/cli 3.0在npm上创建,发布和使用你自己的Vue.js组件库
  8. ppt上的倒计时小工具_英孚线上精品小班课平台操作指南这些课堂小工具你都知道吗?更多课前指引看这里!...
  9. SQL Server 存储
  10. 00110_Class类
  11. SENT协议译码的深入探讨
  12. 20179214 2017-2018-2 《密码与安全新技术》第一周作业
  13. Android秀翻天的操作——使用协程进行网络请求
  14. java数字连连看实验报告_2019年全国高校计算机能力挑战赛初赛java语言解答
  15. 苹果计算机转换,便携毕亚兹苹果计算机转换器,超极本的少接口都能转换身份...
  16. 7-237 组合数的和
  17. R语言用load(xxx.Rdata)报错 bad restore file magic number (file may be corrupted) -- no data loaded
  18. DirectX 修复工具增强版
  19. lasso.m matlab,lasso算法matlab代码
  20. Eclipse常用配置

热门文章

  1. 帧同步和状态同步(二)案例分析
  2. 关于wParam和lParam
  3. 《Python Cookbook 3rd》笔记(5.8):固定大小记录的文件迭代
  4. Python中[:]与[::]的用法
  5. linux mount 查看挂载目录,Linux下使用mount来挂载设备到目录
  6. Socket通信 客户端加密数据,传递数据密文到服务端,服务端解密密文 输出明文
  7. STL源码剖析 迭代器的概念和traits编程技法
  8. 白话解说:阻塞和非阻塞,同步和异步
  9. RPC 远程过程调用协议
  10. oracle orion hugepages_settings.sh(支持OEL 7,4.1内核)