Hdu2063 roller coaster (Hungarian algorithm-maximum binary matching)

Source: Internet
Author: User

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 Description
RPG girls went to the playground with everyone 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?
 

Input
The first line of the 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
For each group of data, an integer is output, indicating the maximum number of combinations that can be used on a roller coaster.
 

Sample Input
6 3 3
1 1
1 2
1 3
2 1
2 3
3 1
0
 

Sample Output
3

Code thinking: At first, I didn't understand this algorithm, but I couldn't understand it. I finally figured it out by myself using the test data.
[Cpp]
# 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 matching)
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; // This match indicates that the boy has matched
If (pre [I] =-1 | find (pre [I]) // This boy does not have a girl or this girl continues to look for other boys-> OK
{
Pre [I] = cur; // boys [I] belong to girls 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; // match
}
Sum = 0;
For (I = 1; I <= n; I ++) // girls match boys
{
Memset (flag, 0, sizeof (flag); // re-mark 0 each time
Sum + = find (I );
}
Printf ("% d \ n", sum );
}
Return 0;
}

 


 


Author: xiongheqiang

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.