题目链接:http://poj.org/problem?id=1274

The Perfect Stall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23088   Accepted: 10285

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
题意:
有n个奶牛和m个谷仓,现在每个奶牛有自己喜欢去的谷仓,并且它们只会去自己喜欢的谷仓吃东西,问最多有多少奶牛能够吃到东西

输入第一行给出n与m
接着n行
每行第一个数代表这个奶牛喜欢的谷仓的个数P,后面接着P个数代表这个奶牛喜欢哪个谷仓

分析: 最大匹配,用匈牙利即可。

#include <stdio.h>
#include <string.h>bool maps[205][205];
bool use[205];
int match[205];
int n,m;bool DFS(int u)
{for(int i=1;i<=m;i++){if(!use[i]&&maps[u][i]){use[i] = true;if(match[i]==-1||DFS(match[i])){match[i] = u;return true;}}}return false;
}int main()
{while(scanf("%d%d",&n,&m)!=EOF){memset(match,-1,sizeof(match));memset(maps,false,sizeof(maps));for(int i=1;i<=n;i++){int num;scanf("%d",&num);for(int j=1;j<=num;j++){int v;scanf("%d",&v);maps[i][v] = true;}}int num = 0;for(int i=1;i<=n;i++){memset(use,false,sizeof(use));if(DFS(i))num++;}printf("%d\n",num);}return 0;
}

转载于:https://www.cnblogs.com/TreeDream/p/5760870.html

Poj(1274),二分图匹配相关推荐

  1. POJ 2724 Purifying Machine (二分图匹配)

    题意 给定m个长度为n的01串(*既表示0 or 1.如*01表示001和101).现在要把这些串都删除掉,删除的方法是:①一次删除任意指定的一个:②如果有两个串仅有一个字符不同,则可以同时删除这两个 ...

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

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

  3. poj 1274 The Perfect Stall

    2019独角兽企业重金招聘Python工程师标准>>> The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total ...

  4. 二分图匹配 Hopcroft-Carp (HK) 算法详解 附例题

    了解这个算法之前 首先了解一个概念 :增广路 增广路 :简单的说 ,是二分图匹配中的一条边,他总是从 左边集合的一个点出发通过一条没有被匹配的边连接到右边集合,再从该点通过一条 匹配过的边连接到右边集 ...

  5. Dinic二分图匹配 || Luogu P3386

    题面:[模板]二分图匹配 思路:Dinic实现二分图匹配,要建一个超级源点(S)和超级汇点(T),分别定为N+M+1和N+M+2 然后S去和N中的数建正边和反边,正边权值为1,反边权值为0:M中的数去 ...

  6. 二分图匹配匈牙利算法DFS实现

    1 /*==================================================*\ 2 | 二分图匹配(匈牙利算法DFS 实现) 3 | INIT: g[][]邻接矩阵; ...

  7. poj1274(二分图匹配)

    (一道基础的二分图匹配) 题目意思大概为N个牛和M个栅栏,一个牛和一个栅栏只能匹配一次,求最大匹配 直接套用二分图最大匹配模板即可 #include <iostream> #include ...

  8. poj2724(二分图匹配)

    题目大概意思为有部分奶酪需要处理,若两个奶酪的二进制只有一位不同,则可以一起处理,问最少需要处理几次 题目思路: 将可以一起处理的两个奶酪用边连接在一起,相当于边只连接二进制中有偶数个1的奶酪和二进制 ...

  9. 算法模板——二分图匹配

    实现功能为二分图匹配 原理:匈牙利算法,核心思想--匹配上了就配,没直接匹配上也要通过前面的腾出位置让这个匹配上(详见:趣写算法系列之--匈牙利算法) 本程序以Codevs2776为例 详见Codev ...

  10. 算法:ACM二分图匹配 HDU2063

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 摘录于互联网,原创作者redraiment,很详细的二分图匹配入门资料! 2063 过山车 Pr ...

最新文章

  1. DateGridView列的输出顺序反了
  2. 别再说你不会 ElasticSearch 调优了,都给你整理好了
  3. ubuntu 交叉编译 armv7_32 ffmpeg x264(已完成)
  4. 手机qpython下载_QPython
  5. VC,VB程序button、图标样式美化
  6. 使用SPEncode.HtmlEncodePreserverSpace保存输入
  7. pythonwindows文件_python查询windows文件
  8. 深度学习框架PyTorch:入门与实践 学习(三)
  9. 为了机器学习把MacBook Pro换成Asus TUF Gaming 全家桶
  10. 运放放大倍数计算公式_运算放大器基本电路大全(转)
  11. c++勾股定理解直三角形边长
  12. 安利自己读CS博士的鸡汤
  13. luckysheet 只读模式和编辑模式
  14. python pdf解析毕业论文_电影数据读取、分析与展示毕业论文+任务书+Python项目源码...
  15. 苹果7 无线流量连接不上网络连接服务器,iPhone7连不上wifi无线网的四种解决方法...
  16. Spring Data Commons 官方文档学习
  17. wps 分节符(连续) 自动变成 分节符(下一页) 解决办法
  18. C、c++ .h 头文件的作用
  19. android2019开源框架
  20. 第一次接触智能家居,该从哪里入手?智汀告诉你

热门文章

  1. Container With Most Water(C++)
  2. MQAM(M元正交幅度调制)
  3. servlet的配置(映射)以及生命周期
  4. validation 开始日期 结束日期_Spring Boot集成validation用于优雅的校验API参数的合法性...
  5. bfs和dfs:poj2386和leetcode130
  6. NP、P、NPC、NP-hard 概念辨析
  7. 后缀数组算法概述及习题
  8. 【HDU - 4794】Arnold【斐波那契数列循环节】
  9. 网易2018校园招聘编程题真题 合唱
  10. 均匀带电直线外一点的场强