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

确保每个f和d都只能经过一次,将牛拆点,分别建f到牛,牛到d的容量为1的路,设置一个总起点0和总汇点2*n+d+f+1,跑一遍网络流

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("\n")
using namespace std;
typedef long long ll; int n,f,d;
const int MAXN = ;
const int MAXM = ;
struct Edge
{
int to,next,cap,flow;
}edge[MAXM];
set <pair<int,int> > st;
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; void init()
{
tol = ;
memset(head,-,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=) {
edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
edge[tol].flow = ;head[u] = tol++;
edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
edge[tol].flow = ;head[v]=tol++;
} int sap(int start,int end,int N) {
memset(gap,,sizeof(gap));
memset(dep,,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -;
gap[] = N;
int ans = ;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
for(int i = pre[u];i != -; i = pre[edge[i^].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i = pre[u];i != -; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -;i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+ == dep[u])
{
flag = true;
cur[u] = pre[v] = i; break;
}
}
if(flag)
{
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -;i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u] = Min+;
gap[dep[u]]++;
if(u != start) u = edge[pre[u]^].to;
}
return ans;
}
int main()
{
init();
int fi, di, x;
cin >> n >> f >> d;
for(int i=;i<=f;i++) addedge(, *n+i, );
for(int i=;i<=d;i++) addedge(*n+f+i, *n+f+d+, );
for(int i=;i<=n;i++) addedge(i, n+i, );
for(int i=;i<=n;i++)
{
scanf("%d%d",&fi,&di);
for(int j=;j<fi;j++)
{
scanf("%d",&x);
addedge(*n+x, i, );
}
for(int j=;j<di;j++)
{
scanf("%d",&x);
addedge(i+n,*n+f+x,);
}
}
cout << sap(, *n+f+d+, *n+f+d+) << endl;
}

最新文章

  1. Uva 524 Prime Ring
  2. node-webkit教程(15)当图片加载失败的时候
  3. Oracle时间函数numtoyminterval()
  4. Supervisor的一些基础使用
  5. Promise 异步执行的同步操作
  6. Spring contextConfigLocation默认加载文件的位置
  7. readelf相关命令
  8. 浏览器控制台console的使用
  9. 【Intellij Idea】设置JDK
  10. 一个非常好用的图片切割工具(c# winform开发) 附源码
  11. R语言学习 第九篇:plyr包
  12. Cordova Upload Images using File Transfer Plugin and .Net core WebAPI
  13. mssqlserver on linux - Linux下尝鲜MSSQL-SERVER【微软大法棒棒哒】
  14. 关闭win10一切
  15. 213. House Robber II 首尾相同的偷窃问题
  16. DevExpress的提示框
  17. mybatis使用时遇到的一些问题------模糊查询、处理大于号小于号、相关函数替换空值
  18. mongo长连接
  19. Hadoop生态圈-Hbase的API常见操作
  20. Codeforces Round #277.5 (Div. 2)部分题解

热门文章

  1. Tensorflow 0.8.0 安装配置方法
  2. 0919MYSQL中取当前周/月/季/年的第一天与最后一天
  3. ASP.NET--IIS的Http请求流程
  4. [bzoj2002][Hnoi2010]Bounce弹飞绵羊_LCT
  5. Apache反向代理结合Tomcat集群来实现负载均衡(一)、概念理解
  6. poj 3267 The Cow Lexicon (动态规划)
  7. 【cl】控制台执行Java程序
  8. [转]详细解读TrueSkill 排名系统
  9. 排序系列 之 冒泡排序及其改进算法 —— Java实现
  10. jquery的this和$(this)