Hduoj ----- (1068) girls and boys (binary matching)

Source: Internet
Author: User
Girls and boys

Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 7577 accepted submission (s): 3472


Problem descriptionthe second year of the university somebody started a study on the romantic relations between the students. the relation "romantically involved" is defined between one girl and one boy. for the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been "romantically involved ". the result of the program is the number of students in such a set.

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

The number of students
The description of each student, in the following format
Student_identifier :( number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3...
Or
Student_identifier :( 0)

The student_identifier is an integer number between 0 and n-1, for N subjects.
For each given data set, the program shocould write to standard output a line containing the result.

 

Sample input70: (3) 4 5 61: (2) 4 62: (0) 3: (0) 4: (2) 0 15: (1) 06: (2) 0 0 130: (2) 1 21: (1) 02: (1) 0

 

Sample output52source southeastern Europe 2000 the question is roughly: there is a school where boys and girls should be matched, and then the meaning of male and male working, female and female playing Lala is ruled out, q: How many bad luck tickets do I have? In fact, it is a disguised form. The biggest match is to find the biggest match, and the remaining unlucky ones are the answer... code:
1 # include <cstring> 2 # include <cstdio> 3 # include <cstdlib> 4 using namespace STD; 5 const int maxn = 1005; 6 int N, A, B, C; 7 bool mat [maxn] [maxn]; 8 bool vis [maxn]; 9 int girl [maxn]; 10 bool check (int x) {11 for (INT I = 0; I <n; I ++) {12 if (MAT [x] [I] = 1 &&! Vis [I]) {13 vis [I] = 1; 14 if (GIRL [I] =-1 | check (GIRL [I]) {15 girl [I] = x; 16 return 1; 17} 18} 19} 20 return 0; 21} 22 int main () 23 {24 // freopen ("test. in "," r ", stdin); 25 while (scanf (" % d ", & N )! = EOF) {26 memset (MAT, 0, sizeof (MAT); 27 memset (girl,-1, sizeof (girl); 28 for (INT I = 0; I <n; I ++) {29 scanf ("% d: (% d)", & A, & B); 30 while (B --) {31 scanf ("% d", & C); 32 mat [a] [c] = 1; 33} 34} 35 int ans = 0; 36 For (Int J = 0; j <n; j ++) {37 memset (VIS, 0, sizeof (VIS); 38 If (check (j )) ans ++; 39} 40/* Through the maximum binary match, we get the maximum number of matches. However, because both boys and girls calculate 41, we have to divide it by two. In this way, the maximum number of matches is */42 printf ("% d \ n", N-ans/2); 43} 44 return 0; 45}
View code

 

 

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.