Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18230 | Accepted: 8132 |
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<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cstdlib> #include<string.h> #include<set> #include<vector> #include<queue> #include<stack> #include<map> #include<cmath> typedef long long ll; typedef unsigned long long LL; using namespace std; const double PI=acos(-1.0); const double eps=0.0000000001; const int INF=1e9; const int N=1000000+100; int vis[1000][1000]; int dis[N]; int head[N]; int a[N],b[N]; int n,f,d; int tot; struct node{ int to,next,flow,c; }edge[N<<1]; void init(){ memset(head,-1,sizeof(head)); tot=0; } void add(int u,int v,int c){ edge[tot].to=v; edge[tot].flow=c; edge[tot].next=head[u]; head[u]=tot++; edge[tot].to=u; edge[tot].flow=0; edge[tot].next=head[v]; head[v]=tot++; } int BFS(int s,int t){ queue<int>q; memset(dis,-1,sizeof(dis)); dis[s]=0; q.push(s); while(!q.empty()){ int x=q.front(); q.pop(); if(x==t)return 1; for(int i=head[x];i!=-1;i=edge[i].next){ int v=edge[i].to; if(edge[i].flow&&dis[v]==-1){ dis[v]=dis[x]+1; q.push(v); } } } if(dis[t]==-1)return 0; return 1; } int DFS(int s,int flow){ if(s==2*n+d+f+1)return flow; int ans=0; for(int i=head[s];i!=-1;i=edge[i].next){ int v=edge[i].to; if(edge[i].flow&&dis[v]==dis[s]+1){ int f=DFS(v,min(flow-ans,edge[i].flow)); edge[i].flow-=f; edge[i^1].flow+=f; ans+=f; if(ans==flow)return ans; } } return ans; } int Dinc(int s,int t){ int flow=0; while(BFS(s,t)){ //cout<<1<<endl; flow+=DFS(s,INF); //cout<<flow<<endl; } return flow; } int main(){ while(scanf("%d%d%d",&n,&f,&d)!=EOF){ init(); int s=0; for(int i=1;i<=f;i++){ add(s,2*n+i,1); } for(int i=1;i<=d;i++){ add(2*n+f+i,2*n+f+d+1,1); } for(int i=1;i<=n;i++){ add(i,i+n,1); } int D,F; for(int i=1;i<=n;i++){ scanf("%d%d",&D,&F); for(int j=1;j<=D;j++){ scanf("%d",&a[j]); add(2*n+a[j],i,1); } for(int j=1;j<=F;j++){ scanf("%d",&b[j]); add(n+i,2*n+f+b[j],1); } } cout<<Dinc(s,2*n+f+d+1)<<endl; } }