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
Line 1: Three space-separated integers: N, F, and D
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
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
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
One way to satisfy three cows is:
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.
题目:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料。每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?
思路:建图,建立一个超级源点,超级汇点,源点连上食物,食物连上牛,牛连上饮料,(牛应该分成牛A1,B1,两个点,这样连成一条线,线的权值为一,否则一头牛可能不止会分配到一种饮料和食物)计算最大流。
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
int mpp[410][410],p[410],liu[410][410],path[410];
int n,f,d,sum;
void init()
{
int x,y,m;
memset(mpp,0,sizeof(mpp));
memset(liu,0,sizeof(liu));
for(int i=1; i<=n; i++)
{
scanf("%d%d",&x,&y);
for(int j=1; j<=x; j++)
{
scanf("%d",&m);
mpp[m+200][i]=1;
mpp[0][m+200]=1;
}
mpp[i][i+100]=1;
for(int j=1; j<=y; j++)
{
scanf("%d",&m);
mpp[i+100][m+300]=1;
mpp[m+300][401]=1;
}
}
}
void inten()
{
queue<int>q;
while(1)
{
memset(path,-1,sizeof(-1));
memset(p,0,sizeof(p));
p[0]=inf;
q.push(0);
while(!q.empty())
{
int ans=q.front();
q.pop();
for(int i=0; i<=401; i++)
{
if(!p[i]&&liu[ans][i]<mpp[ans][i])
{
p[i]=min(p[ans],mpp[ans][i]-liu[ans][i]);
path[i]=ans;
q.push(i);
}
}
}
if(!p[401])
break;
sum+=p[401];
int tem=401;
while(path[tem]!=-1)
{
int v=path[tem];
liu[v][tem]+=p[401];
liu[tem][v]-=p[401];
tem=v;
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&f,&d))
{
sum=0;
init();
inten();
printf("%d\n",sum);
}
}