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.
在农场里,FJ给好多大牛做了好多吃的和喝的,但是大牛比较挑,只要其中的几种,别的都不要,因为每种都只有一样,所以现在要求最多能让多少大牛吃喝都满意。
这个题主要是建边,建完边就是一个裸的dinic了。下面说一下正确的建边方式:
因为在题意中指出,每个牛只能吃一种它喜欢的 dish 和 drink ,所以在建边的时候要控制牛的流量。这里采用拆点的方法,将原来的点拆开并用一条用来限制流量的边连接。如果不限制流量,按照下面的方式建边的话
这样只能保证每道菜和每个饮料被用过一次,而不能保证每个大牛只吃过一次,也就是大牛没有限流。
#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define foodSTAR 0
#define cowSTAR 105
#define cowFinish 210
#define drinkSTAR 315
#define starNode 0
#define finishNode 328
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return
#define mem(a,b) memset(a,b,sizeof(a));
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
struct Edge{
int to;
int cap;
int rev; //edge index of reverse edge in adjacent vertex to
};
int N,F,D;
int level[440];
vector<Edge> adj[440];
int dinic();
void bfs();
int dfs(int star,int finish,int f);
int main(){
ios::sync_with_stdio(false);
while(cin>>N>>F>>D){
int foodLen,drinkLen;
rep(c,0,N){
int cow=c+cowSTAR;
cin>>foodLen>>drinkLen;
int food,drink;
rep(i,0,foodLen){
cin>>food;
food+=foodSTAR;
adj[food].push_back((Edge){cow,1,adj[cow].size()});
adj[cow].push_back((Edge){food,0,adj[food].size()-1});
}
adj[cow].push_back((Edge){c+cowFinish,1,adj[c+cowFinish].size()});
adj[c+cowFinish].push_back((Edge){cow,0,adj[cow].size()-1});
cow=c+cowFinish;
rep(i,0,drinkLen){
cin>>drink;
drink+=drinkSTAR;
adj[cow].push_back((Edge){drink,1,adj[drink].size()});
adj[drink].push_back((Edge){cow,0,adj[cow].size()-1});
}
}
rep(i,1,F+1){
adj[starNode].push_back((Edge){i,1,adj[i].size()});
adj[i].push_back((Edge){starNode,-2,adj[starNode].size()-1});
}
rep(i,1+drinkSTAR,1+D+drinkSTAR){
adj[i].push_back((Edge){finishNode,1,adj[finishNode].size()});
adj[finishNode].push_back((Edge){i,0,adj[i].size()-1});
}
cout<<dinic()<<endl;
rep(i,0,440)
adj[i].clear();
}
re 0;
}
int dinic(){
int d;
int sum=0;
while(true){
bfs();
if(level[finishNode]<0)
break;
while(d=dfs(starNode,finishNode,INF),d>0){
sum+=d;
}
}
re sum;
}
void bfs(){
mem(level,-1);
level[starNode]=0;
queue<int> Q;
Q.push(starNode);
while(!Q.empty()){
int now=Q.front();
Q.pop();
int dep=level[now];
rep(i,0,adj[now].size()){
Edge &e=adj[now][i];
int u=e.to;
if(level[u]<0 && e.cap>0){
level[u]=dep+1;
Q.push(u);
}
}
}
}
int dfs(int star,int finish,int f){
if(star==finish)
return f;
rep(i,0,adj[star].size()){
Edge &e=adj[star][i];
if(e.cap>0 && level[e.to]==level[star]+1){
int d=dfs(e.to,finish,min(f,e.cap));
if(d>0){
e.cap-=d;
adj[e.to][e.rev].cap+=d;
return d;
}
}
}
return -1;
}