Poj 1274 the perfect stall solution report

Source: Internet
Author: User
Tags one more line

Classification: Graph Theory, bipartite graph, maximum matching, Hungary Algorithm

Author: acshiryu time: original question: http://poj.org/problem? Id = 1274 materials: matrix67's blog; nocow Hungary algorithm; Bipartite Graph Matching Courseware

The perfect stall

Time limit:1000 ms Memory limit:10000 K
Total submissions:11659 Accepted:5395

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 52 2 53 2 3 42 1 53 1 2 51 2 

Sample output

4

The question is to find the maximum matching of the Bipartite Graph, which is a basic question. The Hungary algorithm can be used directly to solve the problem. I did not understand the information about the Hungary algorithm for a day yesterday, those explanations are too abstract and directly expressed in text. They do not even have a single graph. Now, they only have some knowledge about the algorithm. To put it bluntly, you need to find a path from the bipartite graph so that the starting and ending points of the path have not been matched, in addition, the line through which the path passes is a line that is not matched, one that has already been matched, and the other that does not match the other that appears in turn. After such a path is found, it is clear that there is one more line to be matched than the one already matched in the path. Therefore, modify the matching graph and remove all matching lines from the path, change the unmatched connections to match, so that the number of matches is one more than the original one. Continue to perform the above operations until such a path is not found. (Uploaded from matrix67 Daniel's blog)

The first time I did this, I didn't understand it at the beginning. After reading the nocow code, I ran it again on the CPU. After knowing it, I directly set the template and gave this question to AC.

Reference code:

1 # include <iostream>
2 # include <cstdlib>
3 # include <cstdio>
4 # include <cstring>
5 # include <algorithm>
6 # include <cmath>
7 using namespace STD;
8 bool map [201] [201], vis [201]; // map indicates whether edge exists between two points. vis indicates whether edge exists after each operation.
9 int link [201]; // link stores the serial number of another vertex connected to the Change Point.
10 int M, N;
11 bool find (int K)
12 {// find a match for K. If yes, record the match and return true; otherwise, false is returned.
13 int I, J;
14 For (I = 1; I <= N; I ++)
15 {// traverse all nodes to find the vertex that has not been accessed and is associated with I
16 if (Map [k] [I] = true &&! Vis [I])
17 {
18 vis [I] = true; // record the change point to be accessed
19 if (link [I] = 0 | find (link [I])
20 {// If the vertex does not match other vertices, or another vertex can match the vertex J that matches the vertex, that is, the augmented path exists.
21 link [I] = K; // match I with K
22 return true;
23}
24}
25}
26 return false;
27}
28 int main ()
29 {
30 While (CIN> m> N)
31 {// initialization, accept data input
32 memset (MAP, false, sizeof (MAP ));
33 memset (link, 0, sizeof (Link ));
34 int I, J;
35 For (I = 1; I <= m; I ++)
36 {
37 int S;
38 CIN> S;
39 For (j = 0; j <s; j ++)
40 {
41 int K;
42 CIN> K;
43 map [I] [k] = true;
44}
45}
46
47 int ans = 0; // maximum number of matches
48 for (I = 1; I <= m; I ++)
49 {
50 memset (VIS, false, sizeof (VIS); // all data is initially 0, indicating that the data has not been tested
51 if (find (I) // If a match is found for I
52 ans ++;
53}
54 cout <ans <Endl;
55}
56 return 0;
57}

  

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.