Poj (1, 1274) the perfect stall

Source: Internet
Author: User

// Note that in the bipartite graph, M corresponds to the right, cowshed, and N corresponds to the left, and ox.
// Initialize the mark once when searching for a match for each ox.

There are nheaded cows and M cowshed, and each of them is only willing to enter the cowshed it is willing to enter. Maybe each ox only wishes to enter a shed, maybe one shed only supports one ox. Now we have given nheaded ox and M cowshed, and given the number of Cowshed that each ox is willing to enter. Please design a solution to match the largest number of cowshed, and output the maximum matching value. Idea: in fact, this question is to find the maximum matching of a bipartite graph. Bipartite Graph: points in a graph can be divided into two point sets. The points at both ends of each edge in the graph belong to different point sets, and the graph is a bipartite graph. The general definition of matching: matching is a subset of all edges in a bipartite graph, in which any two edges have no common points. Maximum matching: in this question, the matching with the largest number of edges belongs to two different sets, and the edge set we seek is the correspondence between the ox and the canopy, therefore, the question is converted to the biggest edge set (maximum matching) of a bipartite graph. 1. Search for an unmatched vertex x on the left, and search all vertices on the right connected to it. 2. If an unmatched vertex is found on the right, the algorithm ends (matched successfully). 3. If a matched Vertex a is found on the right side (Set B to A, and mark it as B-> ), then, let's look at the Point B on the left that matches a. Find the point that is not matched in the point (right) of the edge that is connected to B Starting from point B. If the point C is found on the right, if no match is found, change the original match (B-> A) to (B-> C), and match X with a (x-> A). The algorithm ends (successful match) 4. if the match fails, a link on the left is found. If the match succeeds, a match is added based on the original number of matches. If the match fails, the number of matches remains unchanged. Therefore, after all the left-side links have been found once (that is, all the left-side links have been found once, that is, the number of matches cannot be increased), the number of matches is the maximum match. It can be solved using DFS or BFs, and the implementation of DFS is relatively simple.

 

# Include "stdio. H"
# Include "string. H"
Int map [1000] [1000];
Int link [1000], Mark [1000];
Int n, m;
Int find (int K)
{
Int I;
For (I = 1; I <= m; I ++)
{
If (MARK [I] = 0 & map [k] [I])
{
Mark [I] = 1;
If (link [I] =-1 | find (link [I])
{
Link [I] = K;
Return 1;
}
}
}
Return 0;
}
Int main ()
{
Int I, K, h;
While (scanf ("% d", & N, & M )! = EOF)
{
Memset (MAP, 0, sizeof (MAP ));
Memset (link,-1, sizeof (Link ));

For (I = 1; I <= N; I ++)
{
Scanf ("% d", & K );
While (k --)
{
Scanf ("% d", & H );
Map [I] [H] = 1;
}
}
Int sum = 0;
For (I = 1; I <= N; I ++)
{
Memset (mark, 0, sizeof (Mark ));
Sum + = find (I );
}
Printf ("% d \ n", sum );
}
Return 0;
}

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.