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
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<deque>
using namespace std;
#define maxn 550
#define INF 99999999
int Map[maxn][maxn];
int prev[maxn];
bool vis[maxn];
void EK(int x,int n)
{
int sum=0;
int temp;
int v,u,i;
while(1)
{
memset(prev,0,sizeof(prev));
memset(vis,false,sizeof(vis));
deque<int>Q;
prev[x]=0;
vis[x]=true;
Q.push_back(x);
while(!Q.empty())
{
u=Q.front();
Q.pop_front();
for(v=1;v<=n;v++)
{
if(!vis[v]&&Map[u][v])
{
vis[v]=true;
prev[v]=u;
if(v==n)
{
Q.clear();
break;
}
else
Q.push_back(v);
}
}
}
if(!vis[n])
break;
temp=INF;
i=n;
// printf("%d\n",prev[i]);
while(prev[i])
{
if(temp>Map[prev[i]][i])
temp=Map[prev[i]][i];
//printf("%d\n",prev[i]);
i=prev[i];
}
i=n;
while(prev[i])
{
//printf("1\n");
Map[prev[i]][i]-=temp;
Map[i][prev[i]]+=temp;
i=prev[i];
}
// printf("%d\n",temp);
sum+=temp;
}
printf("%d\n",sum);
}
int main()
{
int N,F,D;
int i;
int i1,i2;
int t;
int n1,n2;
int x;
while(scanf("%d%d%d",&N,&F,&D)!=EOF)
{
t=2*N+F+D+1;
memset(Map,0,sizeof(Map));
for(i=1;i<=N;i++)
{
scanf("%d%d",&n1,&n2);
for(i1=1;i1<=n1;i1++)
{
scanf("%d",&x);
Map[2*N+x][i]=1;
}
for(i2=1;i2<=n2;i2++)
{
scanf("%d",&x);
Map[N+i][2*N+F+x]=1;
}
}
for(i=2*N+1;i<=2*N+F;i++)
Map[t][i]=1;
for(i=2*N+F+1;i<=2*N+F+D;i++)
Map[i][t+1]=1;
for(i=1;i<=N;i++)
Map[i][i+N]=1;
/* for(i=1;i<=t+1;i++)
{
for(int j=1;j<=t+1;j++)
if(Map[i][j])
printf("%d %d\n",i,j);
}*/
EK(t,t+1);
}
return 0;
}
1代表流向一个和接受一个