Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21265 | Accepted: 9401 |
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
网络流建图:源点——>食物——>牛1——>牛2——>饮料——>汇点
为了不让一头牛吃好多次,每头牛都拆成牛1和牛2两个点,连一条流量为1的边。
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
const int INF=1<<29;
int src=0,des=500,e,head[5005],dis[5005];
struct Edge{
int v,cap,next;
Edge(){}
Edge(int vv,int ca,int ne):v(vv),cap(ca),next(ne){}
}edge[5005];
void addedge(int u,int v,int cap){
edge[e]=Edge(v,cap,head[u]);
head[u]=e++;
edge[e]=Edge(u,0,head[v]);
head[v]=e++;
}
void init(){
e=0;
memset(head,-1,sizeof(head));
}
int bfs(){
memset(dis,0,sizeof(dis));
dis[src]=1;
queue<int> q;
q.push(src);
while (q.size()){
int x=q.front();q.pop();
for (int i=head[x];i!=-1;i=edge[i].next){
int v=edge[i].v;
if (edge[i].cap && dis[v]==0){
dis[v]=dis[x]+1;
if (v==des) return 1;
q.push(v);
}
}
}
return 0;
}
int dfs(int s,int f){
if (s==des) return f;
int tmp,cost=0;
for (int i=head[s];i!=-1;i=edge[i].next){
int v=edge[i].v;
if (edge[i].cap && dis[s]==dis[v]-1){
tmp=dfs(v,min(f-cost,edge[i].cap));
if (tmp>0){
edge[i].cap-=tmp;
edge[i^1].cap+=tmp;
cost+=tmp;
if (f==cost) break;
}
else dis[v]=-1;
}
}
return cost;
}
int Dinic(){
int ans=0;
while (bfs()) ans+=dfs(src,INF);
return ans;
}
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
init();
for (int i=1;i<=a;i++){
int x,y;
scanf("%d%d",&x,&y);
for (int j=0;j<x;j++){
int k;
scanf("%d",&k);
addedge(k,i+100,1);
}
addedge(i+100,i+200,1);
for (int j=0;j<y;j++){
int k;
scanf("%d",&k);
addedge(i+200,k+300,1);
}
}
for (int i=1;i<=b;i++) addedge(0,i,1);
for (int i=1;i<=c;i++) addedge(i+300,500,1);
printf("%d\n",Dinic());
}