Hdu2063 roller coaster (Hungarian algorithm-maximum binary matching)

Source: Internet
Author: User

Question connection: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2063

Question:

Roller coaster

Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 4934 accepted submission (s): 2147

Problem descriptionrpg girls went to the playground today and finally got on the coveted roller coaster. However, there are only two seats in each row of the roller coaster, and there is an unwritten rule, that is, every girl must find a boy as a partner and sit with her. However, every girl has her own ideas. For example, rabbit is only willing to be a partner with xhd or pqk. Grass is only willing to be a partner with linle or ll, princesssnow is willing to be a partner with the water prodigal son or pseudo cool. Considering the funding issue, boss Liu decided to let the partner find a ride on the roller coaster. Other people, hey, just stand and watch it. Smart acmer, can you help calculate how many combinations can ride on a roller coaster?

The first line of input data is a three integer K, M, N, indicating the number of possible combinations, the number of girls, and the number of boys. 0 <k <= 1000
1 <= N and M <= 500. In the next K rows, each row has two numbers, indicating that female AI is willing to be partner with boys' BJ. The last 0 end input.

Output outputs an integer for each group of data, indicating the maximum number of combinations that can be used on a roller coaster.

Sample Input

6 3 31 11 21 32 12 33 10
 

Sample output

3. Thinking about the code: I didn't understand the algorithm at first, but I couldn't understand it. I finally figured it out by myself using the test data.
# Include <iostream> # include <stdio. h> # include <memory. h> using namespace STD; const int n = 505; bool map [N] [N], flag [N]; int pre [N]; int n, m, num; // Hungarian algorithm (Binary match) int find (INT cur) // cur is the current girl {int I; for (I = 1; I <= m; I ++) // matched boy {If (Map [cur] [I] &! Flag [I]) // This boy is not matched {flag [I] = true; // in this match, if (pre [I] =-1 | find (pre [I]) // This boy does not have a girl or the girl continues to look for other boys-> OK {pre [I] = cur; // The Boy [I] belongs to the girl cur return 1 ;}}} return 0;} int main () {int I, girl, boy, sum; while (scanf ("% d", & num), num) {scanf ("% d", & N, & M); // n indicates the number of girls, and M indicates the number of boys. memset (MAP, false, sizeof (MAP )); memset (PRE,-1, sizeof (pre); for (I = 0; I <num; I ++) {scanf ("% d", & girl, & boy); map [girl] [Boy] = true; // can match} sum = 0; for (I = 1; I <= N; I ++) // match boys with girls {memset (flag, 0, sizeof (FLAG); // re-mark 0 sum + = find (I) each time );} 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.