Poj 2771 guardian of decency (binary matching, maximum independent set)

Source: Internet
Author: User
Guardian of decency
Time limit:3000 Ms   Memory limit:65536 K
Total submissions:4071   Accepted:1702

Description

Frank N. stein is a very conservative high-school teacher. he wants to take some of his students on an excursion, but he is afraid that some of them might become couples. while you can never exclude this possibility, he has made some rules that he thinks indicates a low probability two persons will become a couple:
    • Their height differs by more than 40 cm.
    • They are of the same sex.
    • Their preferred music style is different.
    • Their favorite sport is the same (they are likely to be fans of different teams and that wowould result in fighting ).

So, for any two persons that he brings on the excursion, they must satisfy at least one of the requirements above. help him find the maximum number of persons he can take, given their vital information.

Input

The first line of the input consists of an integer T ≤ 100 giving the number of test cases. the first line of each test case consists of an integer n ≤ 500 giving the number of pupils. next there will be one line for each pupil consisting of four space-separated Data items:
    • An integer h giving the height in cm;
    • A character 'F' for female or 'M' for male;
    • A string describing the preferred music style;
    • A string with the name of the favorite sport.

No string in the input will contain in more than 100 characters, nor will any string contain any whitespace.

Output

For each test case in the input there shocould be one line with an integer giving the maximum number of eligible pupils.

Sample Input

 
2435 M Classicism upper M baroque skiing43 M baroque chess30 F baroque upper M romance lower f baroque upper M baroque ping-pong51 M Classicism upper M Classicism paintball35 M baroque ping-pong39 F romance ping-pong110 M romance paintball

Sample output

 
37

Source

Northwestern Europe 2005 binary match. It mainly refers to the graph creation method. The question requirement is to select one of the four conditions that can be met between two or two. We should build the image in turn. One side for a person who does not meet the four conditions. It is converted to the problem of finding the largest independent set.

Maximum Independent Set of a Bipartite Graph=Number of vertices-Bipartite Graph maximum matching

Independent Set: a set of independent vertices in the graph.

 

# Include <stdio. h> # Include <Iostream> # Include < String . H> # Include <Algorithm> # Include <Math. h> Using   Namespace STD;  /*  **************************************** * ******************************** // Bipartite Graph Matching (hungaryAlgorithm) // Initialization: G [] [] division of vertices on both sides // establish G [I] [J] to indicate the directed edge of I-> J, it is the matching on the left to the right. If no edge is connected, the value 0 is initialized. // UN indicates the number of vertices on the left to match, and Vn indicates the number of vertices on the right to match. // call: res = Hungary (); Maximum number of matched outputs // advantage: Suitable for dense graphs, DFS find augmented paths, simple and easy to understand // time complexity: O (VE) //************************************** ************************************  */  //  Vertex number starting from 0  Const   Int Maxn = 510  ;  Int UN, vn; //  Number of U and V  Int  G [maxn] [maxn];  Int  Linker [maxn];  Bool  Used [maxn];  Bool DFS ( Int U) //  Find the augmented path from the left  {  Int  V;  For (V =0 ; V <VN; V ++) //  The vertex number starts from 0 and needs to be modified to start from 1.        If (G [u] [v] &! Used [v]) {used [v] = True  ;  If (Linker [v] =- 1 | DFS (linker [v]) {  //  Find zengguang Road, reverse Linker [v] = U; Return   True  ;}}  Return   False ; //  Do not forget this sentence.  }  Int  Hungary (){  Int Res = 0  ;  Int  U; memset (linker, - 1 ,Sizeof  (Linker ));  For (U = 0 ; U <UN; U ++ ) {Memset (used,  0 , Sizeof  (Used ));  If (DFS (u) RES ++ ;}  Return  Res ;}  //  **************************************** **************************************/ Struct  Node {  Int H; //  Height      Char Str1 [ 4 ]; //  Gender      Char Str2 [ 110 ]; //  Favorite Music Style      Char Str3 [ 110 ];//  Favorite sports  } Node [maxn];  Int  Main (){  Int  T;  Int  N; scanf (  "  % D  " ,& T );  While (T -- ) {Scanf (  " % D  " ,& N); UN = Vn = N; memset (G,  0 , Sizeof  (G ));  For ( Int I = 0 ; I <n; I ++ ) Scanf (  "  % D % S % s  " , & Node [I]. H, & node [I]. str1, & node [I]. str2 ,&Node [I]. str3 );  For ( Int I = 0 ; I <n; I ++ )  For ( Int J = 0 ; J <n; j ++ ){  If (ABS (node [I]. H-node [J]. h) <= 40 & Node [I]. str1 [ 0 ]! = Node [J]. str1 [ 0 ] & Strcmp (node [I]. str2, node [J]. str2) = 0 & Strcmp (node [I]. str3, node [J]. str3 )! = 0  ) {G [I] [J] = G [J] [I] = 1  ;} Printf (  "  % D \ n  " , N-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.