题目描述

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

输入格式

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

样例输入 #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.

看到这么小的数据,还有这种选来选去的东西,考虑网络流。

在建图中把牛放在中间,前面放食物,后面放饮料。

由源点向每个食物放一条流量为1的边,每个饮料向汇点放一条流量为1的边。然后把牛拆成两份,中间流量为1.牛和喜欢的饮料食物连边即可。跑一次最大流,每个牛,饮料,食物都会选一次。刚好就可以跑出答案。

#include<bits/stdc++.h>
using namespace std;
int idx=1,hd[200005],k,v[200005],q[200005],thd[200005],kt,l,r,n,f,d,x,y,z,ans,p,qq,s,t;
struct edge{
int v,nxt,f;
}e[2000005];
void add_edge(int u,int v)
{
e[++idx]=(edge){v,hd[u],1};
hd[u]=idx;
e[++idx]=(edge){u,hd[v],0};
hd[v]=idx;
}
int bfs()
{
memcpy(hd,thd,sizeof(hd));
memset(v,0,sizeof(v));
q[l=r=1]=0,v[0]=1;
while(l<=r)
{
for(int i=hd[q[l]];i;i=e[i].nxt)
{
if(e[i].f&&!v[e[i].v])
{
v[e[i].v]=v[q[l]]+1;
q[++r]=e[i].v;
}
}
++l;
}
return v[t];
}
int dfs(int x,int flow)
{
if(x==t)
return flow;
for(int&i=hd[x];i;i=e[i].nxt)
{
if(v[x]+1==v[e[i].v]&&min(flow,e[i].f))
{
if(kt=dfs(e[i].v,min(flow,e[i].f)))
{
e[i^1].f+=kt;
e[i].f-=kt;
return kt;
}
}
}
return 0;
}
int main()
{
scanf("%d%d%d",&n,&f,&d);
s=0,t=1+f+n+d+1;
for(int i=1;i<=f;i++){
add_edge(s,1+i);
}
for(int i=1;i<=d;i++){
add_edge(1+f+n+i,t);
}
for(int i=1;i<=n;i++){
add_edge(1+f+i,1+f+n+d+1+i);
}
for(int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
for(int j=1;j<=x;j++)
{
scanf("%d",&z);
add_edge(1+z,1+f+i);
}
for(int j=1;j<=y;j++)
{
scanf("%d",&z);
add_edge(1+f+n+d+1+i,1+f+n+z);
}
}
memcpy(thd,hd,sizeof(hd));
while(bfs())
while(k=dfs(0,2147483647))
ans+=k;
printf("%d",ans);
return 0;
}

[USACO2007OPENG] Dining G的更多相关文章

  1. 【题解】[USACO07OPEN]Dining G

    \(Link\) \(\text{Solution:}\) 这一题,我们要做到,食物和牛.牛和饮料均为一对一的关系.我们发现这个图不好建立. 经典技巧:将牛拆边,拆成入点和出点,并连容量为\(1\)的 ...

  2. bzoj5000+的洛谷题号

    前言 闲得没事把 bzoj5000+ 在 Luogu 上可找到的题面整理了一下-- 对于我,bzoj 连账号都没有,所以肯定是不清楚 bzoj 题目总数的--因此其实就是手动翻查. 工作量很大,基本不 ...

  3. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  4. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  5. poj3281 Dining

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14316   Accepted: 6491 Descripti ...

  6. POJ 3281 Dining

    Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and dri ...

  7. POJ 3281 Dining 网络流最大流

    B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...

  8. POJ 3281 Dining (网络流)

    POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...

  9. POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )

    题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...

  10. POJ 3281 Dining(网络流)

                                                                        Dining Time Limit: 2000MS   Memo ...

随机推荐

  1. VINS中旋转外参初始化

    VINS 中的旋转外参初始化 ​ 为了使这个两个传感器融合,我们首先需要做的事情是将两个传感器的数据对齐,除了时间上的对齐,还有空间上的对齐.空间上的对齐通俗的讲就是将一个传感器获取的数据统一到另一个 ...

  2. MIT 6.828 Lab实验记录 —— lab1 Booting PC

    实验参考信息 MIT 6.828 lab1 讲义地址 MIT 6.828 课程 Schedule MIT 6.828 lab 环境搭建参考 MIT 6.828 lab 工具guide Brennan' ...

  3. 【Azure Batch】在批处理的Task中如何让它执行多个CMD指令呢

    问题描述 根据Azure Batch的入门文档(使用 Azure 门户创建 Batch 帐户并运行作业 : https://docs.azure.cn/zh-cn/batch/quick-create ...

  4. 「Tricks」整体DP

    不太了解这个东西的具体定义是什么,总之应该是一个用数据结构维护 DP 状态的某几个维度的 trick 吧. 事实上你可以把这篇 post 理解为三个题的解集. 先直接来看 noi2020 - Dest ...

  5. Springboot简单功能示例-4 自定义加密进行登录验证

    springboot-sample 介绍 springboot简单示例 跳转到发行版 查看发行版说明 软件架构(当前发行版使用) springboot hutool-all 非常好的常用java工具库 ...

  6. MySQL系列之——MySQL体系结构、基础管理(用户、权限管理、连接管理、多种启动方式介绍、初始化配置、多实例的应用)

    文章目录 一 体系结构 1.1 C/S(客户端/服务端)模型介绍 1.2 实例介绍 1.3 mysqld程序运行原理 1.3.1 mysqld程序结构 1.3.2 一条SQL语句的执行过程 1.3.2 ...

  7. 在线问诊 Python、FastAPI、Neo4j — 问题咨询

    目录 查出节点 拼接节点属性 测试结果 问答演示 通过节点关系,找出对应的节点,获取节点属性值,并拼接成想要的结果. 接上节生成的CQL # 输入 question_class = {'args': ...

  8. K8S太庞大,这款PasteSpider绝对适合你!一款轻量级容器部署管理工具

    PasteSpider采用.netcore编写,运行于linux服务器的docker/podman里面,涉及的技术或者工具有podman/docker,registry,nginx,top,ssh,g ...

  9. 02-RAID技术 学习心得

    RAID 术语 扇区:是磁盘中最小的存储单元,向磁盘读写数据时是以扇区为最小单元进行存储 block:block,是由N个扇区组成一个块: 在磁盘相同偏移处横向逻辑分割,就形成了stripee: 一个 ...

  10. .NET高性能开发-位图索引(一)

    首先来假设这样一个业务场景,大家对于飞机票应该不陌生,大家在购买机票时,首先是选择您期望的起抵城市和时间,然后选择舱等(公务舱.经济舱),点击查询以后就会出现航班列表,随意的点击一个航班,可以发现有非 ...