漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891

题目描述

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).

有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1: 复制

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
输出样例#1: 复制

3

说明

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.

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std; const int N=1e4+;
const int M=3e4+;
const int INF=0x7fffffff; int n,F,D,S,T;
int head[N],num_edge;
struct Edge
{
int v,flow,nxt;
}edge[M<<]; inline int read()
{
char c=getchar();int num=,f=;
for(;!isdigit(c);c=getchar())
f=c=='-'?-:f;
for(;isdigit(c);c=getchar())
num=num*+c-'';
return num*f;
} inline void add_edge(int u,int v,int flow)
{
edge[++num_edge].v=v;
edge[num_edge].flow=flow;
edge[num_edge].nxt=head[u];
head[u]=num_edge;
} int dep[N];
inline bool bfs()
{
memset(dep,,sizeof(dep));
queue<int> que;
que.push(S),dep[S]=;
int now,v;
while(!que.empty())
{
now=que.front(),que.pop();
for(int i=head[now];i;i=edge[i].nxt)
{
if(edge[i].flow)
{
v=edge[i].v;
if(dep[v])
continue;
dep[v]=dep[now]+;
if(v==T)
return ;
que.push(v);
}
}
}
return ;
} int dfs(int now,int flow)
{
if(now==T)
return flow;
int outflow=,tmp,v;
for(int i=head[now];i;i=edge[i].nxt)
{
if(edge[i].flow)
{
v=edge[i].v;
if(dep[v]!=dep[now]+)
continue;
tmp=dfs(v,min(flow,edge[i].flow));
if(tmp)
{
outflow+=tmp;
flow-=tmp;
edge[i].flow-=tmp;
edge[i^].flow+=tmp;
if(!flow)
return outflow;
}
}
}
dep[now]=;
return outflow;
} int main()
{
num_edge=;
n=read(),F=read(),D=read();
T=F+D+n*+;
for(int i=;i<=n;++i)
{
add_edge(i+F,i+F+n,);
add_edge(i+F+n,i+F,);
}
for(int i=;i<=F;++i)
{
add_edge(S,i,);
add_edge(i,S,);
}
for(int i=;i<=D;++i)
{
add_edge(i+F+*n,T,);
add_edge(T,i+F+n*,);
}
for(int i=,a,b,c;i<=n;++i)
{
b=read(),c=read();
while(b--)
{
a=read();
add_edge(a,i+F,);
add_edge(i+F,a,);
}
while(c--)
{
a=read();
add_edge(i+F+n,a+F+n*,);
add_edge(a+F+n*,i+F+n,);
}
}
int flow=;
while(bfs())
flow+=dfs(S,INF);
printf("%d",flow);
return ;
}

最新文章

  1. 继续上篇抢QQ口令红包,抢那招抢不了的红包技巧
  2. Swift&amp;Node 使用Alamofire进行Post
  3. [ActionScript 3.0] AS3实现滤镜叠加效果
  4. oracle 迁移到 mysql(结构和数据)
  5. 恒天云IaaS基础设施标准
  6. Android_menu_optionMenu
  7. VS2003与Win7的兼容性问题
  8. KVM下windows虚拟机使用virtio驱动
  9. flask-sqlalchemy relationship
  10. ROS机器人导航仿真(kinetic版本)
  11. Git从入门到差不多会用
  12. 《DOM Scripting》学习笔记-——第四章 案列分析 JS美术馆(点击链接到图片)
  13. Linux文件权限与属性详解 之 su &amp; sudo
  14. F - Rescue 优先队列bfs
  15. oracle 中的sql 语句
  16. jenkins 没有maven选项,怎么办
  17. scribe日志系统安装笔记
  18. 使用unity2017.3 vuforia7摄像头放大的问题
  19. 关于C#数据类型自己的理解
  20. 如何深度优化MySQL内核

热门文章

  1. PowerBuilder学习笔记之导入Excel数据
  2. PowerBuilder学习笔记之2PowerScript语言(二)
  3. C#压缩和解压文件
  4. vue复制textarea文本域内容到粘贴板
  5. 30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用
  6. 【常用技巧】js开发的一些技巧
  7. java判定数据(对象)类型
  8. 七年总结常用 Git 命令清单
  9. MVC-路由(Route)
  10. SQL SERVER-Job中Operators搬迁脚本