POJ 3281 Dining
Dining
64-bit integer IO format: %lld Java class name: Main
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
Source
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 500; 18 struct arc{ 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = -1){ 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 arc e[200000]; 27 int head[maxn],d[maxn],cur[maxn]; 28 int tot,N,F,D,s,t; 29 void add(int u,int v,int flow){ 30 e[tot] = arc(v,flow,head[u]); 31 head[u] = tot++; 32 e[tot] = arc(u,0,head[v]); 33 head[v] = tot++; 34 } 35 bool bfs(){ 36 memset(d,-1,sizeof(d)); 37 queue<int>q; 38 d[s] = 1; 39 q.push(s); 40 while(!q.empty()){ 41 int u = q.front(); 42 q.pop(); 43 for(int i = head[u]; ~i; i = e[i].next){ 44 if(e[i].flow && d[e[i].to] == -1){ 45 d[e[i].to] = d[u] + 1; 46 q.push(e[i].to); 47 } 48 } 49 } 50 return d[t] > -1; 51 } 52 int dfs(int u,int low){ 53 if(u == t) return low; 54 int tmp = 0,a; 55 for(int &i = cur[u]; ~i; i = e[i].next){ 56 if(e[i].flow && d[e[i].to] == d[u] + 1 &&(a=dfs(e[i].to,min(low,e[i].flow)))){ 57 e[i].flow -= a; 58 e[i^1].flow += a; 59 tmp += a; 60 low -= a; 61 if(!low) break; 62 } 63 } 64 if(!tmp) d[u] = -1; 65 return tmp; 66 } 67 int dinic(){ 68 int ans = 0; 69 while(bfs()){ 70 memcpy(cur,head,sizeof(head)); 71 ans += dfs(s,INF); 72 } 73 return ans; 74 } 75 int main() { 76 while(~scanf("%d %d %d",&N,&F,&D)){ 77 memset(head,-1,sizeof(head)); 78 s = tot = 0; 79 int n = F + D,a,b,c; 80 t = n + (N<<1|1); 81 for(int i = 1; i <= N; ++i){ 82 scanf("%d %d",&a,&b); 83 while(a--){ 84 scanf("%d",&c); 85 add(c,n+(i<<1)-1,1); 86 } 87 add(n+(i<<1)-1,n+(i<<1),1); 88 while(b--){ 89 scanf("%d",&c); 90 add(n+(i<<1),F+c,1); 91 } 92 } 93 for(int i = 1; i <= F; ++i) add(s,i,1); 94 for(int i = 1; i <= D; ++i) add(F+i,t,1); 95 printf("%d\n",dinic()); 96 } 97 return 0; 98 }