B - Dining
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 Fiintegers 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.
思路:这种比较基础的最大流建图方式如果没有接触过还是有点难想的,我也是看了别人的博客才想出如何建图:
我们首先需要一个超级源点和一个超级汇点,从超级源点到超级汇点的最大流就是答案。所以我们先把超级源点连接上f个食物,权值为1,然后把每个食物连到可以吃它的牛上,权值为1,再把牛连到它可以喝的饮料上,权值也为1.这时候如果某个流经过食物到奶牛再到食物再到源点,则答案会增加一。这就是基本的建图思路。
但这个建图有个问题,就是有可能造成多个食物流到一头牛身上,而题目规定了一头牛只能吃一份食物喝一份饮料,所以此处需要将牛进行拆点,在两个点之间连一条权值为1的路径。这就保证了这条限制。
AC代码
//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp make_pair
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e3 + 50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//typedef vector<ll> vec;
//typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
int n,m,t,f,d;
struct Edge{
int to, cap, flow, next;
}edge[maxn*maxn];
int tol;
int head[maxn];
void init(){
tol = 2;
ms(head, -1);
}
void add_edge(int u, int v, int w, int rw = 0){
edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
edge[tol].next = head[u]; head[u] = tol++;
edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
edge[tol].next = head[v]; head[v] = tol++;
}
int Q[maxn];
int dep[maxn], sta[maxn], cur[maxn];
bool bfs(int s, int t, int n){
int ft = 0, tl = 0;
ms(dep, -1);
dep[s] = 0;
Q[tl++] = s;
while(ft < tl){
int u = Q[ft++];
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && dep[v] == -1){
dep[v] = dep[u]+1;
if(v==t)return true;
Q[tl++] = v;
}
}
}
return false;
}
int dinic(int s, int t, int n){
int maxflow = 0;
while(bfs(s,t,n)){
for(int i = 0; i < n; i++) cur[i] = head[i];
int u = s, tail = 0;
while(cur[s] != -1){
if(u==t){
int tp = INF;
for(int i = tail-1; i >= 0; i--)
tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
maxflow += tp;
for(int i = tail-1; i >= 0; i--){
edge[sta[i]].flow += tp;
edge[sta[i]^1].flow -= tp;
if(edge[sta[i]].cap-edge[sta[i]].flow == 0)
tail = i;
}
u = edge[sta[tail]^1].to;
}
else if(cur[u]!=-1 && edge[cur[u]].cap>edge[cur[u]].flow && dep[u]+1 == dep[edge[cur[u]].to]){
sta[tail++] = cur[u];
u = edge[cur[u]].to;
}
else{
while(u!=s&&cur[u]==-1)
u = edge[sta[--tail]^1].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
}
int main()
{
init();
sddd(n,f,d);
int fn, dn, fi, di;
rep(i, 1, n){
cin >> fn >> dn;
rep(j, 1, fn){
cin >> fi;
add_edge(fi, f+i, 1);
}
rep(j, 1, dn){
cin >> di;
add_edge(f+n+i, f+2*n+di, 1);
}
}
rep(i, 1, f){
add_edge(0, i, 1);
}
rep(i, 1, n){
add_edge(f+i, f+n+i, 1);
}
rep(i, 1, d){
add_edge(f+2*n+i, f+2*n+d+1, 1);
}
cout << dinic(0, f+2*n+d+1, f+2*n+d+2);
return 0;
}