【poj3281】Dining 最大流

来源:互联网 发布:电脑上解压缩软件 编辑:程序博客网 时间:2024/05/17 22:43

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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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

USACO 2007 Open Gold


题意:n头奶牛,f种食物,d种饮料。每头奶牛只会吃一种食物和一种饮料,每种饮料和食物都只会被一头奶牛吃。每头奶牛都有自己喜欢的食物和饮料列表,问最多能满足几头奶牛。

这次不是分成左部、右部了,因为有三种东西。做法是把奶牛拆点,中间连流量为1的边(限制流量,防止一头奶牛吃一堆),然后其中一个点连它喜欢的食物,另一个连它喜欢的饮料,然后跑一边最大流…

#include<cstring>#include<cstdio>#include<iostream>#include<queue>#include<algorithm>using namespace std;const int INF = 1000000010;const int SZ = 1000010;int head[SZ],nxt[SZ],tot = 1;struct edge{    int t,d;}l[SZ];void build(int f,int t,int d){    l[++ tot].t = t;    l[tot].d = d;    nxt[tot] = head[f];    head[f] = tot;}void insert(int f,int t,int d){    build(f,t,d); build(t,f,0);}int deep[SZ];queue<int> q;bool bfs(int s,int e){    memset(deep,0,sizeof(deep));    while(q.size()) q.pop();    deep[s] = 1;    q.push(s);    while(q.size())    {        int u = q.front(); q.pop();        for(int i = head[u];i;i = nxt[i])        {            int v = l[i].t;            if(!deep[v] && l[i].d)            {                deep[v] = deep[u] + 1;                q.push(v);                if(v == e) return true;            }        }    }    return false;}int dfs(int u,int flow,int e){    if(u == e || flow == 0) return flow;    int rest = flow;    for(int i =  head[u];i;i = nxt[i])    {        int v = l[i].t;        if(deep[v] == deep[u] + 1 && l[i].d)        {            int f = dfs(v,min(rest,l[i].d),e);            if(f > 0)            {                l[i].d -= f;                l[i ^ 1].d += f;                rest -= f;                if(rest == 0) break;            }            else deep[v] = 0;        }    }    return flow - rest;}int dinic(int s,int e){    int ans = 0;    while(bfs(s,e))        ans += dfs(s,INF,e);    return ans;}int n,f,d;int getnode(int i,int d){    if(d == 1) return i;    if(d == 2) return i + f;    if(d == 3) return i + f + n;    if(d == 4) return i + f + n + n;}int main(){    scanf("%d%d%d",&n,&f,&d);    int s = n * 2 + f + d + 1;    int e = n * 2 + f + d + 2;    for(int i = 1;i <= f;i ++)        insert(s,getnode(i,1),1);    for(int i = 1;i <= d;i ++)        insert(getnode(i,4),e,1);    for(int i = 1;i <= n;i ++)    {        int fi,di;        scanf("%d%d",&fi,&di);        for(int j = 1;j <= fi;j ++)        {            int x;            scanf("%d",&x);            insert(getnode(x,1),getnode(i,2),1);        }        for(int j = 1;j <= di;j ++)        {            int x;            scanf("%d",&x);            insert(getnode(i,3),getnode(x,4),1);        }        insert(getnode(i,2),getnode(i,3),1);    }    printf("%d\n",dinic(s,e));    return 0;}
0 0