Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11639 | Accepted: 5344 |
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 theDi 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 <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan
#define clr(s,x) memset(s,x,sizeof(s))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) (x&(-x))
#define PB push_back
#define For(i,a,b) for(int i=a;i<b;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef vector<int> vint;
typedef vector<string> vstring;
void RI (int& x){
x = 0;
char c = getchar ();
while (c == ' '||c == '\n') c = getchar ();
bool flag = 1;
if (c == '-'){
flag = 0;
c = getchar ();
}
while (c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar ();
}
if (!flag) x = -x;
}
void RII (int& x, int& y){RI (x), RI (y);}
void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}
const double PI = acos(-1.0);
const int maxn = 6e2 + 10;
const int maxm = 4e4 + 10;
const LL mod = 1e9 + 7;
const double eps = 1e-9;
const int INF = 0x7fffffff;
/************************************END DEFINE*********************************************/
struct Side{
int to,next,c;
}side[maxm];
int top,node[maxn];
void add_side(int u,int v,int c,int rc){
side[top]=(Side){v,node[u],c};
node[u]=top++;
side[top]=(Side){u,node[v],rc};
node[v]=top++;
}
int start,end,cnt,dis[maxn],gap[maxn];
int get_flow(int u,int flow){
if(u==end)return flow;
int ans=0;
for(int i=node[u];i!=-1;i=side[i].next){
int v=side[i].to,c=side[i].c;
if(dis[u]>dis[v]&&c){
int f=get_flow(v,min(flow-ans,c));
ans+=f;
side[i].c-=f;
side[i^1].c+=f;
if(ans==flow)return ans;
}
}
if(!(--gap[dis[u]]))dis[start]=cnt+2;
gap[++dis[u]]++;
return ans;
}
int main(){
int n,f,d;
while(~scanf("%d%d%d",&n,&f,&d)){
top=0;
memset(node,-1,sizeof(node));
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
clr(side,0);
start = 0;
end = 2*n+f+d+1;
FOR(i,1,f)add_side(start,i,1,0);
FOR(i,1,d)add_side(2*n+f+i,end,1,0);
FOR(i,1,n)add_side(f+i,f+i+n,1,0);
FOR(i,1,n){
int num_f,num_d;
RII(num_f,num_d);
while(num_f--){
int mid;
RI(mid);
add_side(mid,f+i,1,0);
}
while(num_d--){
int mid;
RI(mid);
add_side(i+f+n,2*n+f+mid,1,0);
}
}
int ans=0;
cnt=end+1;
gap[0]=cnt;
while(dis[start]<cnt){
ans+=get_flow(start,INF);
}
printf("%d\n",ans);
}
return 0;
}