Dining
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21934 | Accepted: 9733 |
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.
Source
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;
const int inf = 0x7fffffff;
const int mn = 125;
int edge;
int iter[4 * mn]; /// 当前弧优化
struct node
{
int to, val, fan;
};
vector<node> G[4 * mn];
void addedge(int u, int v, int w)
{
node e;
e.to=v,e.val=w,e.fan=G[v].size(); G[u].push_back(e);
e.to=u;e.val=0;e.fan=G[u].size()-1;G[v].push_back(e);
}
int lv[4 * mn];
void bfs(int s)
{
memset(lv, 0, sizeof lv);
lv[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = 0; i < G[t].size(); i++)
{
node &e = G[t][i];
if (e.val > 0 && !lv[e.to])
{
lv[e.to] = lv[t] + 1;
q.push(e.to);
}
}
}
}
int dfs(int s, int t, int f)
{
if (s == t)
return f;
for (int &i = iter[s]; i < G[s].size(); i++)
{
node &e = G[s][i];
if (e.val > 0 && lv[s] < lv[e.to])
{
int d = dfs(e.to, t, min(f, e.val));
if (d > 0)
{
e.val -= d;
G[e.to][e.fan].val += d;
return d;
}
}
}
return 0;
}
int main()
{
int n, f, d;
scanf("%d %d %d", &n, &f, &d);
for (int i = 1; i <= n; i++) /// 牛拆点
addedge(f + i, f + n + i, 1);
for (int i = 1; i <= f; i++)
addedge(0, i, 1);
for (int i = 1; i <= d; i++)
addedge(f + 2 * n + i, f + 2 * n + d + 1, 1);
for (int i = 1; i <= n; i++)
{
int a, b;
scanf("%d %d", &a, &b);
while (a--)
{
int x;
scanf("%d", &x);
addedge(x, f + i, 1);
}
while (b--)
{
int x;
scanf("%d", &x);
addedge(f + n + i, f + 2 * n + x, 1);
}
}
int flow = 0;
while (1)
{
bfs(0);
if (!lv[f + 2 * n + d + 1])
break;
memset(iter, 0, sizeof iter);
int F;
while ((F = dfs(0, f + 2 * n + d + 1, inf)) > 0) // f = dfs(0,f + …) 重定义 括号注意
flow += F;
}
printf("%d\n", flow);
return 0;
}