Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18739 | Accepted: 8344 |
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.
每只牛都有它想吃的食物和水,但现在每种食物和水都只有一个。为了让更多的牛吃好(得到想要的食物和水),输出最多有多少只牛能吃好。
思路:最大流套路。有一个食物源点,有一个水的汇点。
食物1 牛1-----牛1 水1
源点s 食物2 牛2-----牛2 水2 终点
食物3 牛3-----牛3 水3
食物4 牛4-----牛4 水4
牛和牛之间有一条路为1,控制每只牛最多拥有一种食物。
AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<map>
#define MIN(a,b) a>b?b:a
#define inf 0x3f3f3f3f
#define N 500
using namespace std;
struct node
{
int y,w,nex;
} e[30000];
int head[N],cnt,S,T,vs;
int nn[N][2],ff[N],dd[N];
void add(int x,int y,int w)
{
e[cnt].y=y;
e[cnt].w=w;
e[cnt].nex=head[x];
head[x]=cnt++;
e[cnt].y=x;
e[cnt].w=0;
e[cnt].nex=head[y];
head[y]=cnt++;
}
int gap[N],dis[N];
int dfs(int s,int aug)
{
if(s==T) return aug;
int flow=0,mi=vs-1;
for(int i=head[s]; i!=-1; i=e[i].nex)
if(e[i].w)
{
if(dis[s]==dis[e[i].y]+1)
{
int t=dfs(e[i].y,MIN(aug-flow,e[i].w));
e[i].w-=t;
e[i^1].w+=t;
flow+=t;
if(dis[S]>=vs) return flow;
if(aug==flow) return flow;
}
mi=MIN(mi,dis[e[i].y]);
}
if(!flow)
{
if(!(--gap[dis[s]])) dis[S]=vs;
++gap[dis[s]=mi+1];
}
return flow;
}
int slove()
{
gap[0]=vs;
int ans=0;
while(dis[S]<vs)
ans+=dfs(S,inf);
return ans;
}
int main()
{
int n,f,d,l,r,s;
while(~scanf("%d%d%d",&n,&f,&d))
{
cnt=0,S=0,T=1,vs=2;
memset(e,0,sizeof e);
memset(gap,0,sizeof gap);
memset(dis,0,sizeof dis);
memset(nn,0,sizeof nn);
memset(ff,0,sizeof ff);
memset(dd,0,sizeof dd);
memset(head,-1,sizeof head);
for(int i=1; i<=n; i++)
{
nn[i][0]=vs++;
nn[i][1]=vs++;
scanf("%d%d",&l,&r);
while(l--)
{
scanf("%d",&s);
if(!ff[s]) ff[s]=vs++;
add(ff[s],nn[i][0],1);
}
while(r--)
{
scanf("%d",&s);
if(!dd[s]) dd[s]=vs++;
add(nn[i][1],dd[s],1);
}
}
for(int i=1; i<=f; i++)
if(ff[i])
add(S,ff[i],1);
for(int i=1; i<=d; i++)
if(dd[i])
add(dd[i],T,1);
for(int i=1; i<=n; i++)
add(nn[i][0],nn[i][1],1);
printf("%d\n",slove());
}
}