Dining

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 21578 Accepted: 9545

Description

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

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.

Source

USACO 2007 Open Gold

看完题就知道是一道最大流,稍微思考一下就知道怎么建图了:这道题的难点就是每种饮料,每种食物和每头牛只能进行一次匹配。而这是许多最大流问题共同的套路,我们可以将每头牛拆成一个入口和出口,从入口向出口连一条容量为111的边,然后建一个源点sss和一个汇点ttt,从sss向每种食物(饮料)建一条容量为111的边,从每种饮料(食品)向ttt建一条容量为111的边,最后对于每种食品(饮料),向匹配的牛的入口建容量为111的边,对于每头牛的出口,向可以与之匹配的饮料(食品)连一条容量为111的边,最后从sss到ttt跑最大流即可

代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#define N 500
#define M 15000
using namespace std;
inline long long read(){
	long long ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
	return ans;
}
int n,f,d,s,t,dis[N],first[N],cnt=-1,ans=0;
struct Node{int v,next,c;}e[M];
inline void add(int u,int v,int c){
	e[++cnt].v=v;
	e[cnt].c=c;
	e[cnt].next=first[u];
	first[u]=cnt;
}
inline bool bfs(){
	queue<int>q;
	q.push(s);
	memset(dis,-1,sizeof(dis));
	dis[s]=0;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		for(int i=first[x];i!=-1;i=e[i].next){
			int v=e[i].v;
			if(e[i].c<=0||dis[v]!=-1)continue;
			dis[v]=dis[x]+1;
			if(v==t)return true;
			q.push(v);
		}
	}
	return false;
}
inline int dfs(int x,int f){
	if(!f||x==t)return f;
	int flow=f;
	for(int i=first[x];i!=-1;i=e[i].next){
		int v=e[i].v;
		if(flow&&e[i].c>0&&dis[v]==dis[x]+1){
			int tmp=dfs(v,min(flow,e[i].c));
			if(tmp==0)dis[v]=-1;
			flow-=tmp;
			e[i].c-=tmp;
			e[i^1].c+=tmp;
		}
	}
	return f-flow;
}
int main(){
	memset(first,-1,sizeof(first));
	n=read(),f=read(),d=read();
	s=0,t=n+n+f+d+1;
	for(int i=1;i<=f;++i)add(s,i,1),add(i,s,0);
	for(int i=1;i<=d;++i)add(i+f+n+n,t,1),add(t,i+f+n+n,0);
	for(int i=1;i<=n;++i){
		int cntf=read(),cntd=read();
		for(int j=1;j<=cntf;++j){
			int v=read();
			add(v,i+f,1),add(i+f,v,0);
		}
		for(int j=1;j<=cntd;++j){
			int v=read();
			add(i+f+n,v+f+n+n,1),add(v+f+n+n,i+f+n,0);
		}
		add(i+f,i+f+n,1),add(i+f+n,i+f,0);
	}
	while(bfs())ans+=dfs(s,0x3f3f3f3f);
	printf("%d",ans);
	return 0;
}

2018.06.27 POJ3281 Dining(最大流)的更多相关文章

  1. POJ3281 Dining —— 最大流 + 拆点

    题目链接:https://vjudge.net/problem/POJ-3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

  2. 2018.06.27"Shortest" pair of paths(费用流)

    "Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...

  3. POJ3281 Dining 最大流

    题意:有f种菜,d种饮品,每个牛有喜欢的一些菜和饮品,每种菜只能被选一次,饮品一样,问最多能使多少头牛享受自己喜欢的饮品和菜 分析:建边的时候,把牛拆成两个点,出和入 1,源点向每种菜流量为1 2,每 ...

  4. [poj3281]Dining(最大流+拆点)

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

  5. poj3281 Dining 最大流(奇妙的构图)

    我是按照图论500题的文档来刷题的,看了这题怎么也不觉得这是最大流的题目.这应该是题目做得太少的缘故. 什么是最大流问题?最大流有什么特点? 最大流的特点我觉得有一下几点: 1.只有一个起点.一个终点 ...

  6. 2018.06.27 NOIP模拟 节目(支配树+可持久化线段树)

    题目背景 SOURCE:NOIP2015-GDZSJNZX(难) 题目描述 学校一年一度的学生艺术节开始啦!在这次的艺术节上总共有 N 个节目,并且总共也有 N 个舞台供大家表演.其中第 i 个节目的 ...

  7. 【VSCode】Windows下VSCode编译调试c/c++【更新 2018.03.27】

    --------– 2018.03.27 更新--------- 便携版已更新,点此获取便携版 已知BUG:中文目录无法正常调试 用于cpptools 0.15.0插件的配置文件更新 新的launch ...

  8. Insider Dev Tour(2018.06.28)

    时间:2018.06.28地点:北京金茂万丽酒店

  9. 【2018.06.26NOIP模拟】T3节目parade 【支配树】*

    [2018.06.26NOIP模拟]T3节目parade 题目描述 学校一年一度的学生艺术节开始啦!在这次的艺术节上总共有 N 个节目,并且总共也有 N 个舞台供大家表演.其中第 i 个节目的表演时间 ...

随机推荐

  1. Bitmap byte[] InputStream Drawable 互转

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...

  2. Codeforces Beta Round #18 (Div. 2 Only)

    Codeforces Beta Round #18 (Div. 2 Only) http://codeforces.com/contest/18 A 暴力 #include<bits/stdc+ ...

  3. 【python-sql】sql操作时遇到的坑

    针对python的sql语句的封装以及调用: python的db操作类: # -*- coding: utf-8 -*- # coding=utf-8 import MySQLdb class Dat ...

  4. Wannafly挑战赛14 C.可达性(tarjan缩点)

    题目描述 给出一个 0 ≤ N ≤ 105 点数.0 ≤ M ≤ 105 边数的有向图, 输出一个尽可能小的点集,使得从这些点出发能够到达任意一点,如果有多个这样的集合,输出这些集合升序排序后字典序最 ...

  5. sqlserver编号

    select ROW_NUMBER() OVER (ORDER BY 字段 DESC) AS rid,* from 表名

  6. 在 JavaScript 中 ["1","2","3"].map(parseInt) 为何返回不是 [1,2,3] 却是 [1,NaN,NaN]?

    这个问题我是希望有很多人可以一起交流的: 我在 http://blog.csdn.net/justjavac/article/details/19473199#t0 上看到了比较详细的解释, 但是具体 ...

  7. [z]根据awr报告查看最慢的sql语句

    http://blog.itpub.net/12679300/viewspace-2125679/ 当系统运行了一段时间后,系统基本也趋于稳定了,SQL调优也变了DBA的一个主要的工作内容,很多时候都 ...

  8. mysql主备配置

    目录 mysql主备2 一.master配置:2 1. 修改配置文件 2 2. 登录添加账号并赋权限 2 3. 查看master信息 2 二.slave配置:2 1. 修改配置文件 2 2. 重启登录 ...

  9. c# 把对象加入队列,对象为全局变量,对象改变队列值也跟着改变

    若程序把对象加入队列,对象为全局变量,对象改变队列值也跟着改变,如下: filecontent  为两个字段的属性值. class FileContent { // public string Htt ...

  10. android apk 反编译过程

    一.准备必要的工具 apktool (资源文件获取) dex2jar(源码文件获取) jd-gui  (源码查看) 以上三个文件的下载地址为:https://download.csdn.net/dow ...