The Perfect Stall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16396   Accepted: 7502

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

题意很简单,就是裸的二分匹配,但是我是用最大流水过的
/*********
PRO: POJ 1274
TIT: The Perfect Stall
DAT: 2013-08-16-13.40
AUT: UKean
EMA: huyocan@163.com
*********/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define INF 1e9
using namespace std;
queue<int> que;//广搜需要使用的队列
int s,t;//源点和汇点
int flow[505][505];//残流量
int p[505];//广搜记录路径的父节点数组
int a[505];//路径上的最小残量
int cap[505][505];//容量网络
int ans;//最大流
int read()
{
int n,m;
if(!(cin>>n>>m)) return 0;
s=0;t=m+n+1;
//1->n是牛 n+1 ->n+m是牛位
memset(cap,0,sizeof(cap));
for(int i=1;i<=n;i++)
{
cap[s][i]=1;
int si;cin>>si;
for(int j=0;j<si;j++)
{
int temp;cin>>temp;
cap[i][temp+n]=1;
}
}
for(int i=n+1;i<=n+m;i++)
cap[i][t]=1;
return 1;
}
int deal()//增广路算法就不具体解释了,详细的解释可以看我关于网络流的第一篇博客
// http://blog.csdn.net/hikean/article/details/9918093
{
memset(flow,0,sizeof(flow));
ans=0;
while(1)
{
memset(a,0,sizeof(a));
a[s]=INF;
que.push(s);
while(!que.empty())
{
int u=que.front();que.pop();
for(int v=0;v<=t+1;v++)
if(!a[v]&&cap[u][v]-flow[u][v]>0)
{
p[v]=u;
que.push(v);
a[v]=min(a[u],cap[u][v]-flow[u][v]);//路径上的最小残流量
}
}
if(a[t]==0) break;
for(int u=t;u!=s;u=p[u])
{
flow[p[u]][u]+=a[t];
flow[u][p[u]]-=a[t];
}
ans+=a[t];
}
cout<<ans<<endl;
return ans;
}
int main()
{
while(read())
deal();
return 0;
}

poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下的更多相关文章

  1. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

  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 农夫约翰上个 ...

  3. poj——1274 The Perfect Stall

    poj——1274   The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25709   A ...

  4. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  5. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  6. [题解]poj 1274 The Perfect Stall(网络流)

    二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...

  7. poj 1274 The Perfect Stall【匈牙利算法模板题】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20874   Accepted: 942 ...

  8. poj —— 1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26274   Accepted: 116 ...

  9. POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题

    有n个人, 其中有男生和女生,接着有n行,分别给出了每一个人暗恋的对象(不止暗恋一个) 现在要从这n个人中找出一个最大集合,满足这个集合中的任意2个人,都没有暗恋这种关系. 输出集合的元素个数. 刚开 ...

随机推荐

  1. HDU 1506 Largest Rectangle in a Histogram

    这个问题姑且也叫做最大子矩阵吧 给一个树状图,求一个最大面积的子矩阵 思路是这样的,对于每个单位矩阵,求出左边连续不比它低的矩阵的下标,放在l数组里 同样,再求出右边连续的不比它低的矩阵的下标 这样, ...

  2. BZOJ 1984 月下“毛景树”

    我觉得我要把BZOJ上的链剖写完了吧.... #include<iostream> #include<cstdio> #include<cstring> #incl ...

  3. MSSQL常用操作及方法总结

    1.在安装Sql或sp补丁的时候系统提示之前有挂起的安装操作,要求重启的解决办法: 到注册表中找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control ...

  4. JS判断手机访问页面,根据手机访问或者PC访问跳转

    当用户访问你网站时,如果是PC端访问,则不作处理,如果是手机或者平板访问,就跳转到自己定义的手机页面去,这个在做webapp的时候经常用到,把代码分享给大家,希望对大家有所帮助. 首先,你要在页面中引 ...

  5. 获取Request请求的路径信息

    从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...

  6. window 与ubuntu共享文件 hgfs下为空和不用每次挂载hgfs的方法

    解决hgfs为空的方法: sudo apt-get install open-vm-dkms sudo mount -t vmhgfs .host:/ /mnt/hgfs 解决每次都要挂载的方法: 1 ...

  7. 求相同号码一天内的上网流量——mapreduce

    上网数据 1363157985066 13726230503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 24 27 2481 246 ...

  8. 为什么要Cssreset

    1.CssReset是什么?由于html标签在各浏览器的默认样式解析出来的有所不同.例如<ul>标签在IE.firefox.chrome浏览器下默认边距是不一样的.各浏览器的默认样,会给我 ...

  9. Python【基础第三篇】

    set集合 s1=set() 集合特点: 访问速度快 没有重复项 collections系列(数据类型容器模块) 一.计数器(Counter) Counter是对字典类型的补充,用于追踪值的出现次数. ...

  10. pybombs 安装

    参考:https://github.com/gnuradio/pybombs 先装:pip 然后: pip install PyBOMBS 更新源: pybombs recipes add gr-re ...