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.

 

还是一次做网络流

最大流模板题

建图时   注意拆点   注意边不要建多了

建个图 Dinic模板一套就好了

#include<queue>
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int  inf=1e9+7;
const int maxn=2e4+10;
const int maxm=5e4+10;
struct Edge
{
    int v,f,nxt;
};
int n,src,sink;
int g[maxn+10];
int nume;
Edge e[maxm*2+10];
void addedge(int u,int v,int c)
{
    e[++nume].v=v;
    e[nume].f=c;
    e[nume].nxt=g[u];
    g[u]=nume;
    e[++nume].v=u;
    e[nume].f=0;
    e[nume].nxt=g[v];
    g[v]=nume;
}

queue<int>que;
bool vis[maxn+10];
int dist[maxn+10];
int N,F,D;
void bfs()
{
    memset(dist,0,sizeof(dist));
    while(!que.empty())que.pop();
    vis[src]=true;
    que.push(src);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=g[u]; i; i=e[i].nxt)
        {
            if(e[i].f&&!vis[e[i].v])
            {
                que.push(e[i].v);
                dist[e[i].v]=dist[u]+1;
                vis[e[i].v]=true;
            }
        }
    }
}
int dfs(int u,int delta)
{
    if(u==sink)
        return delta;
    else
    {
        int ret=0;
        for(int i=g[u]; delta&&i; i=e[i].nxt)
        {
            if(e[i].f&&dist[e[i].v]==dist[u]+1)
            {
                int dd=dfs(e[i].v,min(e[i].f,delta));
                e[i].f-=dd;
                e[i^1].f+=dd;
                delta-=dd;
                ret+=dd;
            }
        }
        return ret;
    }
}
int maxflow()
{
    int ret=0;
    while(1)
    {
        memset(vis,0,sizeof(vis));
        bfs();
        if(!vis[sink])return ret;
        ret+=dfs(src,inf);
    }
}
void init()
{
    memset(g,0,sizeof(g));
    nume=1;
    scanf("%d%d%d",&N,&F,&D);
    src=N*2+F+D+1;
    sink=N*2+F+D+2;
    int x;
    for(int i=1; i<=F; i++)
    {
        x=i+2*N;
        addedge(src,x,1);
    }
    for(int i=1;i<=D;i++)
    {
        x=i+N*2+F;
        addedge(x,sink,1);
    }
    for(int i=1; i<=N; i++)
    {
        int d,f;
        scanf("%d%d",&f,&d);
        while(f--)
        {
            scanf("%d",&x);
            x=x+2*N;
            //addedge(x,src,1);
            //addedge(i,x,1);
            addedge(x,i,1);
        }
        while(d--)
        {
            scanf("%d",&x);
            x=x+N*2+F;
            //addedge(x,i+N,1);
            addedge(i+N,x,1);
            //addedge(x,sink,1);
            //addedge(sink,x,1);
        }
        addedge(i,i+N,1);
    }
    printf("%d\n",maxflow());
}
int main()
{
    init();
    return 0;
}