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).

题目大意:

有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)

输入格式
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.

输出格式
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

输入输出样例
输入
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

输出
3

时隔许久又碰到网络流了,不过挺简单一道题。。。(王队(wch)肯定觉得简单!!!!)

这道题有两个限制:

  1. 每种食物只能给一头牛
  2. 没头牛最多一种食物和饮料

对于第一个,我们建立一个超级源点指向食物的流量为1,且让饮料指向超级汇点的流量为1即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int N=210;
int n,f,d,s,t;
int tot=1,head[N<<4],nex[N<<4],w[N<<4],to[N<<4];
struct node{
	int v,e;
}p[N<<4];
void add(int a,int b,int c){
	w[++tot]=c; to[tot]=b; nex[tot]=head[a]; head[a]=tot;
}
bool bfs(){
	int vis[N<<4]={0};	queue<int> q;
	q.push(s);	vis[s]=1;
	while(q.size()){
		int u=q.front();	q.pop();
		for(int i=head[u];i;i=nex[i]){
			if(w[i]&&!vis[to[i]]){
				p[to[i]].v=u; p[to[i]].e=i;
				vis[to[i]]=1;	q.push(to[i]);
				if(to[i]==t)	return true;
			}
		}
	}
	return false;
}
int EK(){
	int res=0;
	while(bfs()){
		int mi=0x3f3f3f3f;
		for(int i=t;i!=s;i=p[i].v)	mi=min(mi,w[p[i].e]);
		for(int i=t;i!=s;i=p[i].v){
			w[p[i].e]-=mi;	w[p[i].e^1]+=mi;
		}
		res+=mi;
	}
	return res;
}
int main(){
	cin>>n>>f>>d;	s=9*N,t=9*N+1;
	for(int i=1;i<=f;i++)	add(s,i+3*N,1),add(i+3*N,s,0);
	for(int i=1;i<=d;i++)	add(i+6*N,t,1),add(t,i+6*N,0);
	for(int i=1;i<=n;i++){
		int a,b,x;	cin>>a>>b;
		while(a--){
			cin>>x;
			add(x+3*N,2*i-1,1); add(2*i-1,x+3*N,0);	
		}
		add(2*i-1,2*i,1);	add(2*i,2*i-1,0);
		while(b--){
			cin>>x;	add(2*i,x+6*N,1); add(x+6*N,2*i,0);		
		}
	}
	cout<<EK()<<endl;
	return 0;
}