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
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
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.
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; int n,f,d,b,c,x,n1,n2; struct Edge{ int to,Next,flow; }edge[100010]; int first[17000],size1; int dl[17100],deep[17100]; void add_edge(int x,int y,int z){ size1++; edge[size1].to=y; edge[size1].flow=z; edge[size1].Next=first[x]; first[x]=size1; } bool bfs(int S,int T) { int head=0,tail=1; memset(deep,0,sizeof(deep)); deep[S]=1,dl[1]=S; while(head!=tail){ int v=dl[++head]; for(int u=first[v];u;u=edge[u].Next){ if(!deep[edge[u].to]&&edge[u].flow>0){ deep[edge[u].to]=deep[v]+1; dl[++tail]=edge[u].to; } } } return deep[T]; } int dfs(int now,int aim,int flow){ if(now==aim)return flow; int F=0; for(int u=first[now];u&&flow;u=edge[u].Next){ if(deep[edge[u].to]==deep[now]+1&&edge[u].flow){ int ret=dfs(edge[u].to,aim,min(flow,edge[u].flow)); F+=ret;flow-=ret;edge[u].flow-=ret,edge[u^1].flow+=ret; } } if(!F)deep[now]=-2; return F; } int maxflow(int S,int T){ int ret=0; while(bfs(S,T)){ ret+=dfs(S,T,(1<<30)); } return ret; } int S,T; int main() { while(scanf("%d%d%d",&n,&f,&d)!=EOF) { memset(first,0,sizeof(first)); S=0,T=f+n*2+d+1; size1=1; for(int i=1;i<=n;i++) { scanf("%d%d",&n1,&n2); for(int k=1;k<=n1;k++) { scanf("%d",&x); add_edge(x,f+i*2-1,1); add_edge(f+i*2-1,x,0); } add_edge(f+i*2-1,f+i*2,1); add_edge(f+i*2,f+i*2-1,0); for(int k=1;k<=n2;k++) { scanf("%d",&x); add_edge(f+i*2,f+n*2+x,1); add_edge(f+n*2+x,f+i*2,0); } } for(int i=1;i<=f;i++) { add_edge(S,i,1); add_edge(i,S,0); } for(int i=1;i<=d;i++) { add_edge(f+n*2+i,T,1); add_edge(T,f+n*2+i,0); } printf("%d ",maxflow(S,T)); } return 0; }