Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11866 | Accepted: 5454 |
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
//最大流
//把食物放在左边,牛放在中间,饮料放在右边,然后建图。特别注意要每一头牛只能占用一份,所以应该牛i指向牛i,容量为1,由于没考虑到这种情况一直WA.
//0为源点,1至f为食物点,因为每头牛被拆成两头,牛i指向牛i,所以f + 1至f + 2n为牛,f + 2n + 1至f + 2n + d为饮料点,f + 2n + d + 1为汇点
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 500;
struct edge
{
int to, cap, rev;
};
vector <edge> G[N];
int level[N];
int iter[N];
void add_edge(int from, int to, int cap)
{
edge e;
e.to = to, e.cap = cap, e.rev = G[to].size();
G[from].push_back(e);
e.to = from, e.cap = 0, e.rev = G[from].size() - 1;
G[to].push_back(e);
}
void bfs(int s)
{
memset(level, -1, sizeof(level));
queue <int> que;
level[s] = 0;
que.push(s);
while(! que.empty())
{
int v = que.front(); que.pop();
for(int i = 0; i < G[v].size(); i++)
{
edge &e = G[v][i];
if(e.cap > 0 && level[e.to] < 0)
{
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
int dfs(int v, int t, int f)
{
if(v == t) return f;
for(int &i = iter[v]; i < G[v].size(); i++)
{
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to])
{
int d = dfs(e.to, t, min(f, e.cap));
if(d > 0)
{
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t)
{
int flow = 0;
while(true)
{
bfs(s);
if(level[t] < 0) return flow;
memset(iter, 0, sizeof(iter));
int f;
while((f = dfs(s, t, INF)) > 0) flow += f;
}
}
int main()
{
int n, f, d, a, b, tmp;
while(~ scanf("%d%d%d", &n, &f, &d))
{
for(int i = 1; i <= f; i++) //连接源点和食物点
add_edge(0, i, 1);
for(int i = 1; i <= d; i++) //连接饮料点和汇点
add_edge(n * 2 + f + i, n * 2 + f + d + 1, 1);
for(int i = 1; i <= n; i++) //连接牛i和牛i
add_edge(f + i, f + n + i, 1);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &a, &b);
for(int j = 0; j < a; j++) //连接食物点和牛i
{
scanf("%d", &tmp);
add_edge(tmp, f + i, 1);
}
for(int j = 0; j < b; j++) //连接牛i和饮料点
{
scanf("%d", &tmp);
add_edge(f + n + i, f + n * 2 + tmp, 1);
}
}
printf("%d\n", max_flow(0, n * 2 + f + d + 1)); //最大流即是结果
for(int i = 0; i < N; i++)
G[i].clear();
}
return 0;
}