题目描述

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. 一种创新的 Hybird App 技术开发模式

    Hybrid这个词,在App开发领域,相信大家都不陌生.Hybrid App是指介于web-app.native-app这两者之间的app,它虽然看上去是一个Native App,但只有一个UI We ...

  2. C#程序配置读写例子 - 开源研究系列文章

    今天讲讲关于C#的配置文件读写的例子. 对于应用程序的配置文件,以前都是用的ini文件进行读写的,这个与现在的json类似,都是键值对应的,这次介绍的是基于XML的序列化和反序列化的读写例子.对于in ...

  3. 《SQL与数据库基础》16. 锁

    目录 锁 全局锁 表级锁 表锁 元数据锁 意向锁 行级锁 行锁 间隙锁 临键锁 本文以 MySQL 为例 锁 锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(CPU. ...

  4. Python连接Neo4j工具比较 Neo4j Driver、py2neo

    Python有许多可以连接Neo4j的库和工具,以下是一些常用的: Neo4j Driver for Python 这是官方提供的Python驱动程序,它使用Cypher查询语言与Neo4j数据库进行 ...

  5. CodeForces 1367D Task On The Board

    题意 给一个字符串\(t\),和一个长度为\(m\)的数组\(b[]\),要求构造一个字符串\(s\),\(s\)中的字符都出现在\(t\)中,对于\(s[i]\)而言,对于任意\(j\),如果有\( ...

  6. E-GraphSAGE: A Graph Neural Network based Intrusion Detection System 笔记

    E-GraphSAGE: A Graph Neural Network based Intrusion Detection System 目录 E-GraphSAGE: A Graph Neural ...

  7. NebulaGraph实战:3-信息抽取构建知识图谱

      自动信息抽取发展了几十年,虽然模型很多,但是泛化能力很难用满意来形容,直到LLM的诞生.虽然最终信息抽取质量部分还是需要专家审核,但是已经极大的提高了信息抽取的效率.因为传统方法需要大量时间来完成 ...

  8. 第五周单元测验题英语教学与互联网 mooc

    第五周单元测验题 返回 本次得分为:16.00/20.00, 本次测试的提交时间为:2020-08-30, 如果你认为本次测试成绩不理想,你可以选择 再做一次 . 1 单选(2分) 从评价的主体来看, ...

  9. 【BUU刷题日记】--第二周

    [BUU刷题日记]--第二周 一.[WUSTCTF2020]朴实无华 1 目录爆破 使用dirsearch扫描发现没有结果,因为如果dirsearch请求过快则会导致超出服务器最大请求,扫描不出本来可 ...

  10. Vue源码学习(十三):nextTick()方法

    好家伙,nextTick, (...这玩意,不太常用) 1.什么是nextTick 在Vue中,nextTick是一个用于异步执行回调函数的方法. 它在Vue更新DOM后被调用,以确保在下一次DOM更 ...