Dining
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
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 preparedD (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
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
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
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
有n头牛,f种食物,d种饮料,每头牛有它喜欢的食物和饮料,每种食物,饮料都只能被一头牛吃,问最多多少牛能吃到喜欢的食物和饮料。
源点连食物,食物连牛,牛连牛,牛连饮料,饮料连汇点,权值都为1,超级源点汇点权值无限。然后跑一边网络流就行。牛连牛确保一头牛只能提供1的流量。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = 500;
const int INF = 2147483647;
struct edge{
int to,cap,rev;
edge(int a=0,int b=0,int c=0) :to(a),cap(b),rev(c){}
};
vector<edge> G[MAXN];
bool used[MAXN];
int iter[MAXN];
int level[MAXN];
void Add(int f,int t,int c)
{
G[f].push_back(edge{t,c,G[t].size()} );
G[t].push_back(edge{f,0,G[f].size()-1} );
}
void init()
{
for(int i = 0;i < MAXN;i++){
G[i].clear();
}
return ;
}
int dfs1(int v, int t, int f)
{
if(v == t) return f;
used[v] = true;
for(int i = 0;i < G[v].size();i++){
edge &e = G[v][i];
if(!used[e.to]&&e.cap>0) {
int d=dfs1(e.to,t,min(f,e.cap));
if(d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int MAX1(int s,int t)
{
int f = 0;
while(true){
memset(used,0,sizeof(used));
int tempf = dfs1(s,t,INF);
if(tempf == 0) return f;
f += tempf;
}
}
void bfs(int s)
{
memset(level,-1,sizeof(level));
queue<int> que;
que.push(s);
level[s] = 0;
while(!que.empty()){
int v = que.front();
que.pop();
for(int i = 0;i < G[v].size();i++){
edge &e = G[v][i];
if(e.cap > 0 && level[e.to] == -1){
level[e.to] = level[v]+1;
que.push(e.to);
}
}
}
}
int dfs2(int v,int t,int f)
{
if(v == t) return f;
for(int &i = iter[v];i < G[v].size();i++){
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to]){
int d = dfs2(e.to,t,min(f,e.cap));
if(d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int MAX2(int s,int t)
{
int f = 0;
while(true){
bfs(s);
if(level[t] < 0) return f;
memset(iter,0,sizeof(iter));
int tempf;
while(tempf = dfs2(s,t,INF) > 0) f += tempf;
}
}
int main()
{
int nnum,snum,ynum;
while(scanf("%d%d%d",&nnum,&snum,&ynum) != EOF){
init();
for(int i = 1;i <= snum;i++){
Add(0,i,1);
}
for(int i = snum+nnum+1;i <= snum+2*nnum;i++){
Add(i-nnum,i,1);
}
for(int i = 2*nnum+snum+1;i <= 2*nnum+snum+ynum;i++){
Add(i,2*nnum+snum+ynum+1,1);
}
for(int i = 1;i <= nnum;i++){
int c1,c2;
scanf("%d%d",&c1,&c2);
int a;
for(int j = 0;j < c1;j++){
scanf("%d",&a);
Add(a,snum+i,1);
}
for(int j = 0;j < c2;j++){
scanf("%d",&a);
Add(snum+nnum+i,snum+2*nnum+a,1);
}
}
int ans = MAX1(0,2*nnum+snum+ynum+1);
printf("%d\n",ans);
}
return 0;
}