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.
 
 
 
 
牛吃草问题,这个牛是一个点,有因为每一头牛只能用一次,所以要进行拆点。
这个图很好建的,我就不说了。
 
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
int n, f, d;
struct node
{
int from, to, cap, flow;
node(int from = , int to = , int cap = , int flow = ) :from(from), to(to), cap(cap), flow(flow) {}
};
vector<node>e;
vector<int>G[maxn];
int level[maxn], iter[maxn];
void add(int u, int v, int w)
{
e.push_back(node(u, v, w, ));
e.push_back(node(v, u, , ));
int m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
} void bfs(int s)//这个是为了构建层次网络,也就是level的构建
{
memset(level, -, sizeof(level));
queue<int>que;
que.push(s);
level[s] = ;
while (!que.empty())
{
int u = que.front(); que.pop();
for (int i = ; i < G[u].size(); i++)
{
node &now = e[G[u][i]];
if (now.cap > now.flow&&level[now.to] < )//只有这个没有满并且没有被访问过才可以被访问
{
level[now.to] = level[u] + ;
que.push(now.to);
}
}
}
} int dfs(int u, int v, int f)
{
if (u == v) return f;
for (int &i = iter[u]; i < G[u].size(); i++)
{
node &now = e[G[u][i]];
if (now.cap > now.flow&&level[now.to] > level[u])
{
int d = dfs(now.to, v, min(f, now.cap - now.flow));
if (d > )
{
now.flow += d;
e[G[u][i] ^ ].flow -= d;
return d;
}
}
}
return ;
} int Maxflow(int s, int t)
{
int flow = ;
while ()
{
bfs(s);
if (level[t] < ) return flow;
memset(iter, , sizeof(iter));
int f;
while ((f = dfs(s, t, inf) > )) flow += f;
}
}
void init()
{
for (int i = ; i <= n + ; i++) G[i].clear();
e.clear();
} int main()
{
while (cin >> n >> f >> d)
{
init();
int s = , t = f + * n + d + ;
for (int i = ; i <= f; i++) add(s, i, );
for (int i = ; i <= n; i++)
{
int a, b;
cin >> a >> b;
while (a--)//与牛i相连
{
int x;
cin >> x;
add(x, f + i, );
}
add(f + i, f + n + i, );
while (b--)
{
int x;
cin >> x;
add(f + n + i, f + * n + x, );
}
}
for (int i = ; i <= d; i++) add(f + * n + i, t, );
int ans = Maxflow(s, t);
cout << ans << endl;
}
return ;
}
 
 

最新文章

  1. 在一个SQL Server表中的多个列找出最大值
  2. 笔记整理之BCP
  3. web工作流
  4. H5小游戏的坑点小结
  5. windows 并发与同步 学习笔记
  6. git统计报告
  7. SVG中的'defs' and 'use'-可复用的图元定义
  8. OC KVC总结
  9. String、StringBuffer、StringBuilder之间区别
  10. CUDA零内存拷贝 疑问考证
  11. border样式?
  12. R︱并行计算以及提高运算效率的方式(parallel包、clusterExport函数、SupR包简介)
  13. mysql常用基础操作语法(十二)~~常用数值函数【命令行模式】
  14. 【关于Java移位操作符&amp;按位操作符】
  15. iOS 中的特殊字面量表示方法
  16. (27)session(设置值、取值、修改、删除)
  17. BFC(块状格式化上下文)
  18. UVa 10905 孩子们的游戏
  19. 内置装饰器一:@classmethod、@staticmathod
  20. Servlet 工程 web.xml 中的 servlet 和 servlet-mapping 标签 《转载》

热门文章

  1. 玩转SVG线条动画
  2. GeoGebra小制作
  3. pickle\json,configparser,hashlib模块
  4. Python下将一般对象打印成Json
  5. ASE课程总结 by 林建平
  6. Beta-release 目标
  7. 5. history
  8. Mac剪切板中的PNG保存到文件swift
  9. 关于synergy的问题
  10. CSS躬行记(7)——合成