Dining
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23269 | Accepted: 10294 |
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
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 头牛,F 个食物,D 个饮料。N 头牛每头牛只喜欢几个食物和饮料。每个食物和饮料只能给一头牛。而且一头牛必须同时获得一个食物和一个饮料才能满足。问至多有多少头牛可以获得满足。
解析:
最大流。
为了保证食物和饮料都能匹配上牛,我们可以也只能把牛放中间,也就是串的方法是“食物-牛-饮料”。 注意为了保证一头牛只被算一次,要把牛拆点,中间连一条容量为 1 的边限制。而对于食物和饮料,分别从源点S想食物连容量为 1 的边,从饮料向汇点T连容量为 1 的边,就能保证只被算一次。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <queue>
using namespace std;
const int inf=1e9;
const int Maxn=505;
const int Maxm=100005;
int n,m,k,ans,s,t=500,size=1;
int first[Maxn],dep[Maxn],tmp[Maxn];
struct shu{int to,next,len;};
shu edge[Maxm];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') f=-1,c=getchar();
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void build(int x,int y,int z)
{
edge[++size].next=first[x];
first[x]=size;
edge[size].to=y,edge[size].len=z;
}
inline bool bfs()
{
memset(dep,0,sizeof(dep));
queue<int>q;q.push(s);dep[s]=1;
while(q.size())
{
int p=q.front();q.pop();
for(int u=first[p];u;u=edge[u].next)
{
int to=edge[u].to;
if(!dep[to]&&edge[u].len)
{
dep[to]=dep[p]+1,q.push(to);
if(to==t) return 1;
}
}
}
return 0;
}
inline int dinic(int p,int flow)
{
if(p==t) return flow;
int sum=0;
for(int &u=tmp[p];u&&sum<flow;u=edge[u].next)
{
int to=edge[u].to;
if(dep[to]==dep[p]+1&&edge[u].len)
{
int minn=dinic(to,min(flow-sum,edge[u].len));
if(!(flow-sum)){dep[to]=0;break;}
edge[u].len-=minn,edge[u^1].len+=minn,sum+=minn;
}
}
return sum;
}
int main()
{
n=get_int(),m=get_int(),k=get_int();
for(int i=1;i<=n;i++) build(i+100,i+300,1),build(i+300,i+100,0);
for(int i=1;i<=m;i++) build(s,i,1),build(i,s,0);
for(int i=1;i<=k;i++) build(i+200,t,1),build(t,i+200,0);
for(int i=1;i<=n;i++)
{
int s1=get_int(),s2=get_int();
for(int j=1;j<=s1;j++)
{
int x=get_int();
build(x,i+100,1),build(i+100,x,0);
}
for(int j=1;j<=s2;j++)
{
int x=get_int();
build(i+300,x+200,1),build(x+200,i+300,0);
}
}
while(bfs())
{
memcpy(tmp,first,sizeof(first));
ans+=dinic(s,inf);
}
cout<<ans;
return 0;
}