HDU 3829 cat vs dog (Maximum Independent Set for binary matching)

Source: Internet
Author: User
Cat vs dog

Time Limit: 2000/1000 MS (Java/others) memory limit: 125536/65536 K (Java/Others)
Total submission (s): 1770 accepted submission (s): 600

Problem descriptionthe zoo have n cats and m dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. so the administrator wants to know which animals he shoshould remove to make maximum number of happy children.

 

Inputthe input file contains multiple test cases, for each case, the first line contains three integers n <= 100, m <= 100 and P <= 500.
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)

 

Outputfor each case, output a single INTEGER: the maximum number of happy children.

 

Sample input1 1 2 C1 D1 D1 C1 1 2 4 C1 D1 C1 D1 C1 D2 D2 C1

 

Sample output1 3

Hint

Case 2: Remove D1 and D2, that makes child 1, 2, 3 happy.

 

Source2011 multi-university training contest 1-host by hnu

 

Recommendxubiao At first glance, I want to use binary matching. However, considering that many children may like the same thing, it seems that binary matching is not feasible... Then, we thought about the maximum weight matching... The result still does not work. Finally, we changed our mindset and matched our children by two points. It is to treat a child as a vertex. Build an edge between conflicting children. In this way, it is converted to the largest independent set.
 /*  HDU 3829 calculates the Maximum Independent Set  */  # Include <Stdio. h> # Include <Iostream> # Include <Algorithm> # Include < String . H> # Include <Vector> Using  Namespace  STD;  //  **************************************** ********  Const   Int Maxn = 1505 ; //  This value is greater than the number on both sides, because there is a linker  Int  Linker [maxn];  Bool  Used [maxn]; vector < Int > Map [maxn]; Int  UN;  Bool DFS ( Int  U ){  For ( Int I = 0 ; I <map [u]. Size (); I ++ ){  If (! Used [map [u] [I]) {used [map [u] [I] = True  ;  If (Linker [map [u] [I] =-1 | DFS (linker [map [u] [I]) {linker [map [u] [I] = U;  Return   True  ;}}}  Return   False  ;}  Int  Hungary (){  Int  U;  Int Res = 0 ; Memset (linker, - 1 , Sizeof  (Linker ));  For (U = 0 ; U <UN; U ++ ) {Memset (used,  False , Sizeof  (Used ));  If (DFS (u) RES ++ ;}  Return  Res ;} //  **************************************** *************  Char Like [maxn] [ 5  ];  Char Dislike [maxn] [ 5  ];  Int  Main (){  Int  N, m, p;  While (Scanf ( "  % D " , & N, & M, & P )! = EOF ){  For ( Int I = 0 ; I <maxn; I ++ ) Map [I]. Clear ();  For ( Int I = 0 ; I <p; I ++ ) {Scanf (  "  % S % s  " , & Like [I], &Dislike [I]);} Un = P;  For ( Int I = 0 ; I <p; I ++ )  For ( Int J = I + 1 ; J <p; j ++ )  If (Strcmp (like [I], dislike [J]) = 0 | Strcmp (like [J], dislike [I]) = 0 ) {Map [I]. push_back (j); map [J]. push_back (I);} printf (  "  % D \ n  " , P-Hungary ()/ 2  );}  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.