Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20014 | Accepted: 8898 |
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 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 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
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 201000 using namespace std; queue<int>que; int n,f,d; int tot=1,ans,src,decc; int lev[MAXN],cur[MAXN]; int to[MAXN],cap[MAXN],net[MAXN],head[MAXN]; void add(int u,int v,int w){ to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot; to[++tot]=u;net[tot]=head[v];cap[tot]=0;head[v]=tot; } bool bfs(){ for(int i=src;i<=decc;i++){ cur[i]=head[i]; lev[i]=-1; } while(!que.empty()) que.pop(); que.push(src);lev[src]=0; while(!que.empty()){ int now=que.front(); que.pop(); for(int i=head[now];i;i=net[i]) if(lev[to[i]]==-1&&cap[i]>0){ lev[to[i]]=lev[now]+1; que.push(to[i]); if(to[i]==decc) return true; } } return false; } int dinic(int now,int flow){ if(now==decc) return flow; int delate,rest=0; for(int & i=cur[now];i;i=net[i]) if(lev[to[i]]==lev[now]+1&&cap[i]>0){ delate=dinic(to[i],min(cap[i],flow-rest)); if(delate){ rest+=delate; cap[i]-=delate; cap[i^1]+=delate; if(rest==flow) break; } } if(rest!=flow) lev[now]=-1; return rest; } int main(){ scanf("%d%d%d",&n,&f,&d); src=0;decc=n*2+d+f+1; for(int i=1;i<=f;i++) add(0,2*n+i,1); for(int i=1;i<=d;i++) add(2*n+f+i,decc,1); for(int i=1;i<=n;i++){ int ff,dd; scanf("%d%d",&ff,&dd); add(i,n+i,1); for(int j=1;j<=ff;j++){ int x; scanf("%d",&x); add(2*n+x,i,1); } for(int j=1;j<=dd;j++){ int x; scanf("%d",&x); add(n+i,2*n+f+x,1); } } while(bfs()) ans+=dinic(src,0x7f7f7f7f); cout<<ans; }