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
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> #include<math.h> usingnamespace std; constint MAX = 666666; constint Ni = 555; structEdge { int u, v, c; int next; }edge[20 * Ni]; int n, m; int edn;//边数 int p[Ni];//父亲 int d[Ni]; int sp, tp;//原点,汇点 voidaddedge(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++; } intbfs() { 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; } intdfs(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; } intdinic(int sp, int tp) { int total = 0, t; while (bfs()) { while (t = dfs(sp, MAX)) total += t; } return total; } intmain() { 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)); return0; }