\(\color{#0066ff}{ 题目描述 }\)

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

$\color{#0066ff}{ 输入格式 } $

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.

\(\color{#0066ff}{输出格式}\)

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

\(\color{#0066ff}{输入样例}\)

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

\(\color{#0066ff}{输出样例}\)

3

\(\color{#0066ff}{数据范围与提示}\)

1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100

\(\color{#0066ff}{ 题解 }\)

最大流

图从左至右依次为s,食物,牛,牛,饮料,t

s向所有食物连容量为1的边,所有饮料向t连容量为1的边

每头牛之间连容量为1的边

然后食物与左边的牛,右边的牛与饮料,按照输入连容量为1的边

这样保证了题目的两个条件

因为可能会有一头牛同时喜欢多种食物和饮料,而我们只能算一次,一头牛最多有1的流量!

#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 1e4;
struct node {
int to, dis;
node *nxt, *rev;
node(int to = 0, int dis = 0, node *nxt = NULL, node *rev = NULL)
: to(to), dis(dis), nxt(nxt), rev(rev) {}
void *operator new(size_t) {
static node *S = NULL, *T = NULL;
return (S == T) && (T = (S = new node[1024]) + 1024), S++;
}
}*head[maxn], *cur[maxn];
int dep[maxn];
int n, s, t, na, nb;
void add(int from, int to, int dis) {
head[from] = new node(to, dis, head[from], NULL);
}
void link(int from, int to, int dis) {
add(from, to, dis), add(to, from, 0);
(head[from]->rev = head[to])->rev = head[from];
}
bool bfs() {
for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
std::queue<int> q;
q.push(s);
dep[s] = 1;
while(!q.empty()) {
int tp = q.front(); q.pop();
for(node *i = head[tp]; i; i = i->nxt)
if(!dep[i->to] && i->dis)
dep[i->to] = dep[tp] + 1, q.push(i->to);
}
return dep[t];
}
int dfs(int x, int change) {
if(x == t || !change) return change;
int flow = 0, ls;
for(node *i = cur[x]; i; i = i->nxt) {
cur[x] = i;
if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->dis)))) {
change -= ls;
flow += ls;
i->dis -= ls;
i->rev->dis += ls;
if(!change) break;
}
}
return flow;
}
int dinic() {
int flow = 0;
while(bfs()) flow += dfs(s, 0x7fffffff);
return flow;
}
//na n n nb
int main() {
n = in(), na = in(), nb = in();
s = 0, t = n + n + na + nb + 1;
for(int i = 1; i <= na; i++) link(s, i, 1);
for(int i = 1; i <= nb; i++) link(n + na + n + i, t, 1);
for(int i = 1; i <= n; i++) link(na + i, na + n + i, 1);
for(int i = 1; i <= n; i++) {
int ka = in(), kb = in();
while(ka --> 0) link(in(), na + i, 1);
while(kb --> 0) link(na + n + i, n + n + na + in(), 1);
}
printf("%d\n", dinic());
return 0;
}

P2891 [USACO07OPEN]吃饭Dining 最大流的更多相关文章

  1. P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)

    题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...

  2. P2891 [USACO07OPEN]吃饭Dining

    漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891 题目描述 Cows are such finicky eaters. Each cow ha ...

  3. 洛谷 P2891 [USACO07OPEN]吃饭Dining

    裸的最大流. #include <cstdio> #include <cstring> #include <queue> const int MAXN = 4e3 ...

  4. 洛谷P2891 [USACO07OPEN]吃饭Dining

    题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...

  5. 「洛谷P2891」[USACO07OPEN]吃饭Dining 解题报告

    P2891 [USACO07OPEN]吃饭Dining 题目描述 Cows are such finicky eaters. Each cow has a preference for certain ...

  6. [Luogu P2891/POJ 3281/USACO07OPEN ]吃饭Dining

    传送门:https://www.luogu.org/problemnew/show/P2891 题面 \ Solution 网络流 先引用一句真理:网络流最重要的就是建模 今天这道题让我深有体会 首先 ...

  7. bzoj1711[USACO07OPEN]吃饭Dining

    题意 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮 ...

  8. [USACO07OPEN]吃饭Dining

    嘟嘟嘟 这应该是网络流入门题之一了,跟教辅的组成这道题很像. 把每一只牛看成书,然后对牛拆点,因为每一只牛只要一份,食物和饮料分别看成练习册和答案. #include<cstdio> #i ...

  9. BZOJ 1711 吃饭dining/Luogu P1402 酒店之王 拆点+最大流流匹配

    题意: (吃饭dining)有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享 ...

随机推荐

  1. List转Datable(需区分对象充当List成员和数组充当List成员两种情况)

    对象充当List成员时: /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name ...

  2. The connection to adb is down and a sever error has occured的解决

    1. 打开任务管理器,关掉豌豆夹等手机助手 2. 打开命令行,切换到adb所在目录,如:C:\Users\Jubincn\Downloads\adt-bundle-windows-x86_64-201 ...

  3. leetcode443

    使用两个数组分别记录字符和对应的数字,然后清除原来的vector,重新向里面添加元素.注意判断1个字符时,不将'1'加入vector. int compress(vector<char>& ...

  4. Microsoft Office Visio 2010如何创建UML 用例图

    转自:https://blog.csdn.net/mmoooodd/article/details/10513059 1..在Microsoft Office2010中打开Microsoft Visi ...

  5. WebView三个方法区别(解决乱码问题)

    最近使用WebView加载中文网页的时候出现乱码问题,网上整理下基本解决方法: 其实我发现这不管是在线还是离线显示都可以使用LoadUrl方法!联网时好像是默认utf-8,离线读取本地时需要设置默认编 ...

  6. 离散对数的求解(bsgs)

    bsgs算法 主要用来解决${A^x} = B(\bmod C)$(c是质数),都是整数,已知A.B.C求x. 例:poj 2417 Discrete Logging 具体步骤如下: 先把$x = i ...

  7. 深入理解asp.net中的 __doPostBack函数

    前段时间做一个.net网站的时候,用到了模拟前端按钮刷新updatePanel进行局部刷新的时候,遇见了这个问题,当时没顾上记下来,查看网上资料,记下来留着以后查看. 很早以前,当我刚接触asp.NE ...

  8. Flow Layout

    --------------siwuxie095                             将根面板 contentPane 的布局切换为 Flow Layout     Flow La ...

  9. C++标准库vector以及迭代器

    今天看C++的书,出现了一个新的概念,容器vector以及容器迭代器. vector是同一种对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存. ...

  10. ZXing开发详解

    博客转载自:https://blog.csdn.net/skillcollege/article/details/38852183 什么是Z*? 在Android平台做过二维码相关模块的肯定都熟知ZX ...