USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)
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)
5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
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)
4 ——————————————————————————————————
二分图匹配匈牙利算法模板
算法简述:
对于当前点x,选一个点y进行匹配,如果这个点y与另一点x'匹配,进行递归知道为x'找到另一个分配或无法找到另一分配
/*
ID:ivorysi
PROG:stall4
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#define inf 0x7fffffff
#define ivorysi
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define p(x) (x)*(x)
using namespace std;
vector<int> g[];
int used[],from[];
int n,m,ans;
bool find(int x){
used[x]=;
for(int i=;i<g[x].size();++i) {
if(from[g[x][i]]) {
if(used[from[g[x][i]]]== && find(from[g[x][i]])) {
from[g[x][i]]=x;
return true;
}
}
else {
from[g[x][i]]=x;
return true;
}
}
return false;
}
void init() {
scanf("%d%d",&n,&m);
int s,b;
siji(i,,n) {
scanf("%d",&s);
siji(j,,s) {
scanf("%d",&b);
g[i].push_back(b+);
}
} }
void solve() {
init();
siji(i,,n) {
memset(used,,sizeof(used));
if(find(i)) ++ans;
}
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("stall4.in","r",stdin);
freopen("stall4.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)的更多相关文章
- POJ1274 The Perfect Stall 二分图,匈牙利算法
N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙 ...
- HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))
Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- Codevs 1222 信与信封问题 二分图匹配,匈牙利算法
题目: http://codevs.cn/problem/1222/ 1222 信与信封问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 ...
- BZOJ1433 [ZJOI2009]假期的宿舍 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8372785.html 题目传送门 - BZOJ1433 题解 我们理一理题目. 在校的学生,有自己的床,还可以睡 ...
- HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色
原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...
- (转)二分图匹配匈牙利算法与KM算法
匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...
- BZOJ1059 [ZJOI2007]矩阵游戏 二分图匹配 匈牙利算法
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1059 题意概括 有一个n*n(n<=200)的01矩阵,问你是否可以通过交换整行和整列使得左 ...
- 网络流24题 第三题 - CodeVS1904 洛谷2764 最小路径覆盖问题 有向无环图最小路径覆盖 最大流 二分图匹配 匈牙利算法
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - CodeVS1904 题目传送门 - 洛谷2764 题意概括 给出一个有向无环图,现在请你求一些路径,这些路径 ...
- 矩阵游戏|ZJOI2007|BZOJ1059|codevs1433|luoguP1129|二分图匹配|匈牙利算法|Elena
1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec Memory Limit: 162 MB Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩 ...
随机推荐
- Web学习-apache视图log刊物
视图apache刊物 apache日志位置 不同的系统位置不同. widnows 假如是windows的话,xampp下应该是都存在的,直接去找apache的folder/log/access.log ...
- .Net下简单地实现MD5加密
在.Net中为我们提供了一个方法HashPasswordForStoringInConfigFile,可以简单方便地实现MD5加密.该方法位于System.Web命名空间下,所以需要在引用中添加Sys ...
- 值为NULL的对象指针
相信大家对NULL不会很陌生,NULL 是一个标准规定的宏定义,用来表示空指针常量,当一个指针变量被赋值为NULL时,表示它不再指向任何有效地址,无法在访问任何数据.在VS2012库文件stdio.h ...
- Android中常用的颜色
代码: <?xml version=”″ ?> <resources> <color name=”white”>#ffffff</color><! ...
- probuf了解
人们一直在强调,同 XML 相比, Protobuf 的主要优点在于性能高.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍. 对于这些 “小 3 到 10 倍”, ...
- 用Haproxy给MySQL做负载均衡
1.安装 # wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz # tar zcvf haproxy-1.4.24.t ...
- JavaScript插件——按钮
Bootstrap3.0学习第二十四轮(JavaScript插件——按钮) 前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/ ...
- windows下Jdk和Tomcat的安装配置
Jdk全称Java development Kit,Jdk是sun公司提供的免费开源的java语言开发工具包,现在最新版本是8.0,说道jdk的版本,有些人可能会有疑惑,怎么有说jdk1.6,jdk1 ...
- [转]Creating an iPhone Daemon
ref: http://chrisalvares.com/blog/7/creating-an-iphone-daemon-part-1/ http://chrisalvares.com/blog/3 ...
- [google面试CTCI]1-3.字符串去重
[字符串与数组] Q:Design an algorithm and write code to remove the duplicate characters in a string without ...