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: NF, 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 Fiintegers 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.
题解:题目给你N头牛,每头牛都有喜欢的饮料喝喜欢的食物,而每一种食物和饮料都只能给一头牛,让你求,最多可以分配多少头牛食物饮料;
由于每种饮料喝每种食物只能给一种牛,因此我们可以将牛这个节点拆分为一条容量为1的边,然后让牛喝饮料喝食物分别建立一条容量为1的边,这样就转化为
最大流的问题;
参考代码为:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define PI acos(-1)
#define EPS 1e-8
const int INF=0x3f3f3f3f;
const int maxn = ;
int n,m,k,s,t,x,y,z;
struct Edge {
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn], cur[maxn]; void Init()
{
memset(d,,sizeof d);
for(int i=;i<=n;i++) G[i].clear();
} void addedge(int from, int to, int cap)
{
edges.push_back((Edge){from, to, cap, });
edges.push_back((Edge){to, from, , });
int m = edges.size();
G[from].push_back(m-); G[to].push_back(m-);
} bool bfs()
{
memset(vis,,sizeof vis);
queue<int> q;
q.push(s);
d[s] = ; vis[s] = ;
while (!q.empty())
{
int x = q.front(); q.pop();
for(int i = ; i < G[x].size(); ++i)
{
Edge &e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int x,int a)
{
if(x == t || a == ) return a;
int flow = , f;
for(int &i = cur[x]; i < G[x].size(); ++i)
{
Edge &e = edges[G[x][i]];
if (d[e.to] == d[x] + && (f=dfs(e.to, min(a, e.cap-e.flow))) > )
{
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f; a -= f;
if (a == ) break;
}
}
return flow;
} int Maxflow(int s, int t)
{
int flow = ;
while (bfs())
{
memset(cur,,sizeof cur);
flow += dfs(s, INF);
}
return flow;
} int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
Init();
s=,t=*n+m+k+;
for(int i=;i<=m;i++) addedge(s,i,);
for(int i=;i<=n;i++) addedge(m+i,n+m+i,);
for(int i=*n+m+;i<=*n+m+k;i++) addedge(i,t,);
for(int i=;i<=n;i++)
{
scanf("%d%d",&x,&y);
for(int j=;j<=x;j++) scanf("%d",&z),addedge(z,m+i,);
for(int j=;j<=y;j++) scanf("%d",&z),addedge(m+n+i,z+m+*n,);
}
printf("%d\n",Maxflow(s,t));
}
return ;
}
 

POJ 3281 Dining(网络流-拆点)的更多相关文章

  1. poj 3281 Dining 网络流-最大流-建图的题

    题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...

  2. POJ - 3281 Dining(拆点+最大网络流)

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18230   Accepted: 8132 Descripti ...

  3. poj 3281 Dining【拆点网络流】

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11828   Accepted: 5437 Descripti ...

  4. POJ 3281 Dining 网络流最大流

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

  5. POJ 3281 Dining (网络流之最大流)

    题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给 ...

  6. POJ 3281 Dining[网络流]

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

  7. POJ 3281 Dining (网络流构图)

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

  8. POJ 3281 Dining (网络流)

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

  9. POJ 3281 Dining(最大流)

    POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...

随机推荐

  1. 深入理解计算机系统 第八章 异常控制流 Part1 第二遍

    第二遍读这本书,每周花两到三小时时间,能读多少读多少(这次看了第 500~507 页,共 8 页) 第一遍对应笔记链接 https://www.cnblogs.com/stone94/p/101651 ...

  2. (Codeforce)Correct Solution?

    One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and g ...

  3. 小程序 数字过千 以K显示

    先新建一个 wxs 文件 每一个 .wxs 文件和 <wxs> 标签都是一个单独的模块. 每个模块都有自己独立的作用域.即在一个模块里面定义的变量与函数,默认为私有的,对其他模块不可见. ...

  4. 资深架构师Sum的故事:正则!入门就是这样简单

    | 故事背景 职场如战场!Sum带领三个小队友用了两周,成功把代理功能给干出来了.如果说产品经理是最魔鬼的指挥官,那测试就是最魔鬼的教官.这两周,让Sum深深领略了什么是X市的日出. 不过话又说回来, ...

  5. nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)

    Phone List 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Given a list of phone numbers, determine if it i ...

  6. 使用POI导出EXCEL工具类并解决导出数据量大的问题

    POI导出工具类 工作中常常会遇到一些图表需要导出的功能,在这里自己写了一个工具类方便以后使用(使用POI实现). 项目依赖 <dependency> <groupId>org ...

  7. JavaScript中解析JSON --- json.js 、 json2.js 以及 json3.js的使用区别

    JSON官方(http://www.json.org/)提供了一个json.js,json.js是JSON官方提供的在JavaScript中解析JSON的js包,json.js.json2.js.js ...

  8. 【MongoDB】2019年MongoDB中文社区广州大会,干货满满的分享活动

    1 介绍 MongoDB中文社区(mongoing.com)是大中华区获得官方认可的中文社区,11月23日下午,在广州举办了线下用户大会,带给大家一手干货和实践. 2 大会议程 大会组织者对时间的把控 ...

  9. Python-Re正则表达式库

    来源:中国MOOC北京理工大学Python教学团队 链接:https://www.icourse163.org/learn/BIT-1001870001#/learn/content?type=det ...

  10. Spring Boot整合Elasticsearch启动报错

    如果你遇见下面的错误,很可能是你的springboot和es版本关系不对应 ERROR 14600 --- [ main] .d.e.r.s.AbstractElasticsearchReposito ...