poj 1274 The Perfect Stall(二分图匹配)
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 ( <= N <= ) and M ( <= M <= ). 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 ( <= 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 (..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
Sample Output
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define N 206
int n,m;
vector<int> v[N];
int vis[N];
int match[N]; bool dfs(int u){
for(int i=;i<v[u].size();i++){
int x=v[u][i];
if(!vis[x]){
vis[x]=;
if(match[x]==- || dfs(match[x])){
match[x]=u;
return true;
}
}
}
return false;
} void solve(){
int ans=;
memset(match,-,sizeof(match));
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
if(dfs(i)){
ans++;
}
}
printf("%d\n",ans);
}
int main()
{ while(scanf("%d%d",&n,&m)==){ for(int i=;i<=n;i++){
v[i].clear();
} for(int i=;i<=n;i++){
int num;
scanf("%d",&num);
int y;
for(int j=;j<=num;j++){
scanf("%d",&y);
v[i].push_back(y);
}
} solve(); }
return ;
}
poj 1274 The Perfect Stall(二分图匹配)的更多相关文章
- poj 1274 The Perfect Stall (二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17768 Accepted: 810 ...
- [POJ] 1274 The Perfect Stall(二分图最大匹配)
题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)
Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...
- poj——1274 The Perfect Stall
poj——1274 The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25709 A ...
- POJ 1274 The Perfect Stall (二分图匹配)
[题目链接] http://poj.org/problem?id=1274 [题目大意] 给出一些奶牛和他们喜欢的草棚,一个草棚只能待一只奶牛, 问最多可以满足几头奶牛 [题解] 奶牛和喜欢的草棚连线 ...
- POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
- poj 1274 The Perfect Stall 解题报告
题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...
- poj —— 1274 The Perfect Stall
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26274 Accepted: 116 ...
- POJ 1274 The Perfect Stall
题意:有n只牛,m个牛圈(大概是),告诉你每只牛想去哪个牛圈,每个牛只能去一个牛圈,每个牛圈只能装一只牛,问最多能让几只牛有牛圈住. 解法:二分图匹配.匈牙利裸题…… 代码: #include< ...
随机推荐
- Javascript:DOM动态创建元素实例应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 关闭钩子(shutdown hook)的作用
DK1.3介绍了java.lang.Runtime class的addShutdownHook()方法.如果你需要在你的程序关闭前采取什么措施,那么关闭钩子(shutdown hook)是很有用的. ...
- cocos2dx--cocos2dx3.1.1执行报无法解析的外部符号
使用cocos2dx3.1.1和VS2012 新建了一个名为test的工程.放在D:\cocos2dx\cocos2d-x-3.1.1\projects下 编译通过,没问题 用cocostudio的场 ...
- uva 10626 - Buying Coke(记忆化搜索)
题目链接:10626 - Buying Coke 题目大意:给出要买可乐的数量, 以及1元,5元和10元硬币的数量, 每瓶可乐8元,每次照钱会按照最少硬币的方式找回, 问如何投币可使得投入的硬币数最少 ...
- [ES6] WeakMap vs Map
WeakMap: is a type of Map where only objects can be passed as keys. Primitive data type -- such are ...
- ArcGis ToolBar为灰色
问题描述:添加完导航按钮,测试时发现按钮一直都是灰色的,不可用状态. 可能原因:跟控件添加的顺序有关,导致toolbar跟map控件无法正确关联. 解决方案:删掉toolBar,重新添加.
- MySQL sql 执行步骤
基本步骤是 1.from 2.join on 3.where 4.group by 5.having 6.order by 7.select 8.distinct ,sum,... ...
- Swift--集合类型 数组 字典 集合
数组 1.创建一个数组 var someInts = [Int]()空数组 someInts = []清空 var threeDoubles = Array(repeating: 0.0, count ...
- UVA 10037 贪心算法
题目链接:http://acm.hust.edu.cn/vjudge/contest/122829#problem/A 题目大意:N个人夜里过河,总共只有一盏灯,每次最多过两个人,然后需要有人将灯送回 ...
- Turtle库
下列turtle库的简单常用指令 forward(distance) #将箭头移到某一指定坐标 left(angel) right(angel) penup() #提起笔,用于另起一个地方绘制时 ...