Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14505 | Accepted: 6576 |
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
题目大意:
有n头牛,f种食物和d种饮品。
接下来n行,每行前两个元素表示这头牛喜欢多少种食物和多少种饮品,接下来那些元素,就表示这头牛喜欢的食物编号和喜欢的饮品编号。
已知每种食物只有一个,每种饮品也只有一个,问最多能够满足多少头牛。一头牛只有同时拿到了自己喜欢的某种事物和饮品的时候,才算做满足。
思路:
1、一开始真的是脑洞大开,又是匹配思路,又是最大流思路,碰碰的冒出来,只可惜最后还是因为自己分析了一番之后发现自己思路的建立方向和代码实现对于匹配的思路毫无进展,自己逼自己走上了绝路。
2、确定了用最大流来搞这个题之后呢,首先最重要的当然是建图啦,巴拉巴拉一顿建立,发现自己控制不了最关键的一个点:一头牛只有同时拿到了自己喜欢的某种事物和饮品的时候,才算做满足。
3、那么我们就要考虑如何处理这个问题。无论是源点接食物,食物接牛,牛接饮品,饮品接汇点,或者是食物接饮品,饮品接牛,牛接汇点(一开始我就是这么建图的),这样的建图方式,一律不可行。这个时候拆点的作用就显现出来了,我们将牛节点一分为2,然后令源点接食物,食物接牛1,牛1接牛2,牛2接饮品,饮品接汇点。这样,我们就能保证一头牛其左端到右端的流是1,也就保证了一头牛只拿一个食物,之后牛2接饮品,然后再通过饮品接上汇点,即又保证了一头牛只拿一种饮品,这样就能保证:
一头牛只有同时拿到了自己喜欢的某种事物和饮品的时候,才算做满足。的条件。
4、那么建图最终思路:a接b表示从a到b有一条有向变。
①源点接食物,权值为1,表示一种食物只能给一头牛提供。
②拆牛节点1分为2,并且令牛1接牛2,权值为1,保证之后牛2接饮品的时候,保证只拿了一个食物。
③食物接牛1,其权值为1。
④牛2接饮品,其权值为1,表示一头牛只拿一种饮品。
⑤饮品接汇点,权值为1,表示一种饮品只能给一头牛提供。
样例图:
5、建好图之后,跑一遍Dinic,输出最大流即可。
Ac代码:
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
int from;
int to;
int w;
int next;
}e[15151515];
int cur[151515];
int head[151515];
int n,f,d,ss,tt,cont;
int ff[150000];
int dd[150000];
int divv[150000];
void add(int from,int to,int w)
{
/*
if(w)
{
printf("%d %d\n",from,to);
}*/
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
int makedivv()
{
memset(divv,0,sizeof(divv));
queue<int >s;
s.push(ss);
divv[ss]=1;
while(!s.empty())
{
int u=s.front();
if(u==tt)return 1;
s.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&divv[v]==0)
{
divv[v]=divv[u]+1;
s.push(v);
}
}
}
return 0;
}
int Dfs(int u,int maxflow,int tt)
{
if(u==tt)return maxflow;
int ret=0;
for(int &i=cur[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&divv[v]==divv[u]+1)
{
int f=Dfs(v,min(maxflow-ret,w),tt);
e[i].w-=f;
e[i^1].w+=f;
ret+=f;
if(ret==maxflow)return ret;
}
}
return ret;
}
void Dinic()
{
int ans=0;
while(makedivv()==1)
{
memcpy(cur,head,sizeof(head));
ans+=Dfs(ss,INF,tt);
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d%d%d",&n,&f,&d))
{
ss=n*2+f+d+1;
tt=ss+1;
cont=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
add(i,i+n,1);
add(i+n,i,0);
}
for(int i=n*2+1;i<=n*2+f;i++)
{
add(ss,i,1);
add(i,ss,0);
}
for(int i=n*2+f+1;i<=n*2+f+d;i++)
{
add(i,tt,1);
add(tt,i,0);
}
for(int i=1;i<=n;i++)
{
int fff,ddd;
scanf("%d%d",&fff,&ddd);
for(int j=1;j<=fff;j++)
{
int tmp;
scanf("%d",&tmp);
add(tmp+2*n,i,1);
add(i,tmp+2*n,0);
}
for(int j=1;j<=ddd;j++)
{
int tmp;
scanf("%d",&tmp);
add(i+n,tmp+2*n+f,1);
add(tmp+2*n+f,i+n,0);
}
}
Dinic();
}
}
/*
2 3 3
0 1 1
1 0 1
*/