Poj 1247 The Perfect Stall bare binary match, but you can use The maximum flow

Source: Internet
Author: User

The Perfect Stall
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 16396 Accepted: 7502

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 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
Sample Output

4 Source

USACO 40
The question is very simple, that is, the bare binary match, but I used the largest flow of water.

/******** PRO: POJ 1274TIT: The Perfect StallDAT: 2013-08-16-13.40AUT: UKeanEMA: huyocan@163.com ****/# include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <queue> # define INF 1e9using namespace std; queue <int> que; // The int s, t queues to be used for extensive search; // Source and Sink int flow [505] [505]; // residual traffic int p [505]; // The parent node array int a [505] of the wide search record path; // minimum residual volume on the path int cap [505] [505]; // capacity network int ans; // maximum stream int read () {int n, m; if (! (Cin> n> m) return 0; s = 0; t = m + n + 1; // 1-> n is cattle n + 1-> n + m is cattle memset (cap, 0, sizeof (cap); for (int I = 1; I <= n; I ++) {cap [s] [I] = 1; int si; cin> si; for (int j = 0; j <si; j ++) {int temp; cin >>temp; cap [I] [temp + n] = 1 ;}} for (int I = n + 1; I <= n + m; I ++) cap [I] [t] = 1; return 1;} int deal () // The augmented path algorithm is not described in detail, for more information, see my first blog on network stream // http://blog.csdn.net/hikean/article/details/9918093 {memset (flow, 0, sizeof (flow); ans = 0; while (1) {memset (a, 0, sizeof (); A [s] = INF; que. push (s); while (! Que. empty () {int u = que. front (); que. pop (); for (int v = 0; v <= t + 1; v ++) if (! A [v] & cap [u] [v]-flow [u] [v]> 0) {p [v] = u; que. push (v); a [v] = min (a [u], cap [u] [v]-flow [u] [v]); // minimum residual traffic on the path} if (a [t] = 0) break; for (int u = t; u! = S; u = p [u]) {flow [p [u] [u] + = a [t]; flow [u] [p [u]-= a [t];} ans + = a [t];} cout <ans <endl; return ans ;} int main () {while (read () deal (); return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.