POJ 3281 Dining【網路流-拆點建圖】

NO IMAGE

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


題意:

有F 種食物和D 種飲料,每種食物或飲料只能供一頭牛享用,且每頭牛隻享用種食物和一種飲料。現在有N 頭牛,每頭牛都有自己喜歡的食物種類列表和飲料種類列表,問最多能使幾頭牛同時享用到自己喜歡的食物和飲料。 (1 <= F <= 100, 1 <= D <= 100, 1 <= N <= 100 )

拆點建圖:把牛拆成頭和尾,兩個點,中間容量為1,源點連線每個食物,容量為1,然後根據題意食物連線牛的頭,容量inf,牛尾連線飲料,容量inf,每個飲料連線匯點,容量1

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
using namespace std;
const int MAX = 666666;
const int Ni = 555;
struct Edge {
int u, v, c;
int next;
}edge[20 * Ni];
int n, m;
int edn;//邊數    
int p[Ni];//父親    
int d[Ni];
int sp, tp;//原點,匯點   
void addedge(int u, int v, int c)
{
edge[edn].u = u; edge[edn].v = v; edge[edn].c = c;
edge[edn].next = p[u]; p[u] = edn  ;
edge[edn].u = v; edge[edn].v = u; edge[edn].c = 0;
edge[edn].next = p[v]; p[v] = edn  ;
}
int bfs()
{
queue <int> q;
memset(d, -1, sizeof(d));
d[sp] = 0;
q.push(sp);
while (!q.empty())
{
int cur = q.front();
q.pop();
for (int i = p[cur]; i != -1; i = edge[i].next)
{
int u = edge[i].v;
if (d[u] == -1 && edge[i].c > 0)
{
d[u] = d[cur]   1;
q.push(u);
}
}
}
return d[tp] != -1;
}
int dfs(int a, int b)
{
int r = 0;
if (a == tp)return b;
for (int i = p[a]; i != -1 && r < b; i = edge[i].next)
{
int u = edge[i].v;
if (edge[i].c > 0 && d[u] == d[a]   1)
{
int x = min(edge[i].c, b - r);
x = dfs(u, x);
r  = x;
edge[i].c -= x;
edge[i ^ 1].c  = x;
}
}
if (!r)d[a] = -2;
return r;
}
int dinic(int sp, int tp)
{
int total = 0, t;
while (bfs())
{
while (t = dfs(sp, MAX))
total  = t;
}
return total;
}
int main()
{
int f, d, temp;
edn = 0;
memset(p, -1, sizeof(p));    //初始化
scanf("%d%d%d", &n, &f, &d);
sp = 0; tp = 444;
for (int i = 1; i <= f; i  )
{
addedge(sp, i, 1);
}
for (int i = 1; i <= d; i  )
addedge(i   111, tp, 1);
for (int i = 1; i <= n; i  )
{
scanf("%d%d", &f, &d);
addedge(i   222, i   333, 1);
for (int j = 1; j <= f; j  )
{
scanf("%d", &temp);
addedge(temp, i   222, 1);//food不加,牛加222  333  drink加111            
}
for (int j = 1; j <= d; j  )
{
scanf("%d", &temp);
addedge(i   333, temp   111, 1);
}
}
printf("%d\n", dinic(sp, tp));
return 0;
}