Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared
D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤
N
≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).

Input

Line 1: Three space-separated integers: N,
F, and D

Lines 2.. N+1: Each line i starts with a two integers Fi and
Di, the number of dishes that cow i likes and the number of drinks that cow
i likes. The next Fi integers denote the dishes that cow
i will eat, and the Di integers following that denote the drinks that cow
i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

Sample Output

3

Hint

One way to satisfy three cows is:

Cow 1: no meal

Cow 2: Food #2, Drink #2

Cow 3: Food #1, Drink #1

Cow 4: Food #3, Drink #3

The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

Source

USACO 2007 Open Gold



以往一般都是左边一个点集表示供应并与源相连,

右边一个点集表示需求并与汇相连。现在不同了,供应有两种资源,需求仍只有

一个群体,怎么办?其实只要仔细思考一下最大流的建模原理,此题的构图也不

是那么难想。最大流的正确性依赖于它的每一条 s-t 流都与一种实际方案一一对

应。那么此题也需要用 s-t 流将一头牛和它喜欢的食物和饮料“串”起来,而食

物和饮料之间没有直接的关系,自然就想到把牛放在中间,两边是食物和饮料,

由 s, t 将它们串起来构成一种分配方案。至此建模的方法也就很明显了:每种食

物 i 作为一个点并连边(s, i, 1),每种饮料 j 作为一个点并连边(j, t, 1),将每头牛 k

拆成两个点 k’, k’’并连边(k’, k’’, 1), (i, k’, 1), (k’’, j, 1),其中 i, j 均是牛 k 喜欢的食物

或饮料。求一次最大流即为结果。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define MAXN 400+10
#define MAXM 5000000
#define INF 10000000+100
int n,f,d,top;
int vis[MAXN],dis[MAXN],cur[MAXN],head[MAXN];
struct node
{
int u,v,cap,flow,next;
}edge[MAXM];
void init()
{
top=0;
memset(head,-1,sizeof(head));
}
void add(int a,int b,int c)
{ node E1={a,b,c,0,head[a]};
edge[top]=E1;
head[a]=top++;
node E2={b,a,0,0,head[b]};
edge[top]=E2;
head[b]=top++;
}
void getmap()
{
int j;
for(int i=1;i<=n;i++)
{
int fnum,dnum;
scanf("%d%d",&fnum,&dnum);
while(fnum--)
{
scanf("%d",&j);
add(2*n+j,i,1);
}
while(dnum--)
{
scanf("%d",&j);
add(n+i,2*n+f+j,1);
}
add(i,n+i,1);
}
for(int i=1;i<=f;i++)
add(0,2*n+i,1);
for(int i=1;i<=d;i++)
add(2*n+f+i,2*n+d+f+1,1);
}
bool bfs(int s,int e)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
while(!q.empty()) q.pop();
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(E.cap>E.flow&&!vis[E.v])
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+1;
if(E.v==e) return true;
q.push(E.v);
}
}
}
return false;
}
int dfs(int x,int a,int e)
{
if(x==e||a==0)
return a;
int flow=0,f;
for(int &i=cur[x];i!=-1;i=edge[i].next)
{
node &E=edge[i];
if(dis[x]+1==dis[E.v]&&(f=dfs(E.v,min(E.cap-E.flow,a),e))>0)
{
E.flow+=f;
edge[i^1].flow-=f;
a-=f;
flow+=f;
if(a==0) break;
}
}
return flow;
}
int MAXflow(int s,int e)
{
int flow=0;
while(bfs(s,e))
{
memcpy(cur,head,sizeof(head));
flow+=dfs(s,INF,e);
}
return flow;
}
int main()
{
while(scanf("%d%d%d",&n,&f,&d)!=EOF)
{
init();
getmap();
printf("%d\n",MAXflow(0,2*n+d+f+1));
}
return 0;
}

最新文章

  1. python模块(json和pickle模块)
  2. iOS 宏条件编译
  3. 服务器Ubuntu16.04下连接锐捷
  4. Hibernate中易错地方的总结
  5. webbroswer 后台注入脚本 的方法
  6. hdu2845(dp)
  7. hdu 5636 搜索 BestCoder Round #74 (div.2)
  8. BZOJ_2795_[Poi2012]A Horrible Poem_hash+暴力
  9. 『Python进阶』专题汇总
  10. Codeforces Avito Code Challenge 2018 D. Bookshelves
  11. Apache Shiro 快速入门教程,shiro 基础教程
  12. Python模块——configparser
  13. Linux dmidecode 命令
  14. 《mysql必知必会》学习_第八章_20180730_欢
  15. Coursera台大机器学习技法课程笔记15-Matrix Factorization
  16. spark shell学习笔记
  17. easyui-datagrid个人实例
  18. 《C++ Primer (V4)》读书笔记
  19. 多线程系列 - 基础篇01 - 线程基本概念 &amp; 线程优先级 &amp; 守护线程 60%
  20. fastjson的deserializer的主要优化算法

热门文章

  1. Docker学习总结(4)——Docker镜像与容器命令
  2. hadoop-09-安装资源上传
  3. Java数据结构-线性表之单链表LinkedList
  4. hdoj--1869--六度分离(floyd)
  5. [JZOJ4024] [佛山市选2015] 石子游戏 解题报告
  6. JMeter使用碰到的问题
  7. 配置ssh免密码登录的原理
  8. java9新特性-18-统一的JVM日志系统
  9. AD域导入导出命令
  10. CDH5.14操作指南