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 种饮料,给出每个牛想吃的菜和饮料,问最多多少头牛不饿肚子
思路:
将牛拆成两个点。建立一个超级源点 s,s 与每种菜之间连边,菜与牛之间连边,牛与自己的分点连边,牛的分点和饮料连边,饮料与超级汇点连边,边权都是1。为什么拆点:因为每头牛只能吃一份菜,喝一份饮料。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1005;
const int M = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int head[N], tot, n, f, d, s, t;
struct node {
int u, v, w;
};
struct Edge {
int from, to, next, cap, flow;
}edge[M];
void init() {
tot = 2;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w, int rw = 0) {
edge[tot].from = u;
edge[tot].to = v;
edge[tot].cap = w;
edge[tot].flow = 0;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].from = v;
edge[tot].to = u;
edge[tot].cap = rw;
edge[tot].flow = 0;
edge[tot].next = head[v];
head[v] = tot++;
}
int Q[N];
int dep[N], cur[N], sta[N]; ///数组cur记录点u之前循环到了哪一条边
bool bfs(int s, int t, int n) {
int fron = 0, tail = 0;
memset(dep, -1, sizeof(dep[0]) * (n + 1));
dep[s] = 0;
Q[tail++] = s;
while(fron < tail) {
int u = Q[fron++];
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[tail++] = 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() {
int a, b, k;
while(~scanf("%d%d%d", &n, &f, &d)) {
init();
s = 0;
t = 2 * n + f + d + 1;
for(int i = 1; i <= n; ++i) {
scanf("%d%d", &a, &b);
for(int j = 1; j <= a; ++j) {
scanf("%d", &k);
addedge(k + 2 * n, i, 1);
}
for(int j = 1; j <= b; ++j) {
scanf("%d", &k);
addedge(i + n, 2 * n + f + k, 1);
}
}
for(int i = 1; i <= n; ++i)
addedge(i, i + n, 1);
for(int i = 1; i <= f; ++i)
addedge(s, 2 * n + i, 1);
for(int i = 1; i <= d; ++i)
addedge(2 * n + f + i, t, 1);
printf("%d\n", dinic(s, t, t + 1));
}
return 0;
}