POJ 1274 The Perfect Stall
The Perfect StallTime Limit: 2000/1000 ms (Java/Other) Memory Limit: 20000/10000 K (Java/Other) Total Submission (s): 8 Accepted Submission (s ): 6 Problem Description Farmer John completed his new barn just last week, complete with all the latest milking technology. unfortunately, due to engineering problems, all the stallin the new barn are different. for the first week, Farmer John randomly assigned cows to stils, but it quickly became clear that any given cow was only willing to produce milk in certain stils. for the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stils. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stils that is possible.
Input The input parameter des several cases. for each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200 ). N is the number of cows that Farmer John has and M is the number of stils in the new barn. each of the following N lines corresponds to a single cow. the first integer (Si) on the line is the number of stallthat the cow is willing to produce milk in (0 <= Si <= M ). the subsequent Si integers on that line are the stils in which that cow is willing to produce milk. the stall numbers will be integers in the range (1 .. m), and no stall will be listed twice for a given cow.
Output For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.
Sample Input
5 5 2 5 3 3 3 4 2 1 5 3 1 2 5 2
Sample Output
4
Network stream or binary match.
There are nheaded cows, m stalls, and each booth can only accommodate one cow. Each cow has its own booth that is willing to produce milk.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace std; # define MAXN 44444 # define MAXM 999999 # define inf 1 <30 struct Edge {int v, cap, next;} edge [MAXM]; int n, m, vs, vt, NE, NV; int head [MAXN]; void Insert (int u, int v, int cap) {edge [NE]. v = v; edge [NE]. cap = cap; edge [NE]. next = head [u]; head [u] = NE ++; edge [NE]. v = u; edge [NE]. cap = 0; edge [NE]. next = head [v]; head [v] = NE ++;} Int level [MAXN]; int gap [MAXN]; void bfs (int vt) {memset (level,-1, sizeof (level); memset (gap, 0, sizeof (gap); level [vt] = 0; gap [level [vt] ++; queue <int> que; que. push (vt); while (! Que. empty () {int u = que. front (); que. pop (); for (int I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. v; if (level [v]! =-1) continue; level [v] = level [u] + 1; gap [level [v] ++; que. push (v) ;}}int pre [MAXN]; int cur [MAXN]; // the start point of the parameter. int SAP (int vs, int vt) {bfs (vt ); memset (pre,-1, sizeof (pre); memcpy (cur, head, sizeof (head); int u = pre [vs] = vs, flow = 0, aug = inf; gap [0] = NV; while (level [vs] <nv ). cap-= "aug;" aug = "(aug =-1? Edge [I]. cap: min (aug, edge [I]. cap); "bool =" "flag =" false; "I =" edge [I]. next) "int =" "j =" 0; j <u; j ++) "minlevel =" NV; "ne =" 0; "nv =" n + m + 1; // The total number of sides that may pass through. "pre =" "return =" "u =" pre [v]; "v =" = vt) "vs =" 0; // start point "vt =" n + m + 1; // end point "> <p> </nv)> </int> </queue> </algorithm> </cstring> </cstdio> </iostream>