Guardian of Decency

 

Guardian of Decency

Grade: 10 / Discount: 0.8

Time Limit:2000MS Memory Limit:65536K

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 favourite sport is the same (they are likely to be fans of different teams and that would 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 favourite sport.


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

Output

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

Sample Input

2

4

35 M classicism programming

0 M baroque skiing

43 M baroque chess

30 F baroque soccer

8

27 M romance programming

194 F baroque programming

67 M baroque ping-pong

51 M classicism programming

80 M classicism Paintball

35 M baroque ping-pong

39 F romance ping-pong

110 M romance Paintball

Sample Output

3

7

Source

Northwestern Europe 2005

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define  N 510

struct  Stu
{
    int   height;
    char  sex[5];
    char  music[110];
    char  sport[110];
};

Stu    p[N];
bool   map[N][N];
int    match[N];
bool   visite[N];
int    id[N];
int    n,m,t;

bool isok( Stu const& a, Stu const& b )
{
    return  abs( a.height- b.height )<= 40 && strcmp(a.music,b.music )== 0 && strcmp(a.sport,b.sport )!= 0 ;
}

int  abs( int const& a )
{
    return a> 0? a: -a;
}

void build()
{
    n= 0, m= 0;
    memset( map, false, sizeof(map) );
    memset( match, -1, sizeof(match) );
    
    for( int i= 0; i< t; ++i )
    {
        if( p[i].sex[0]== 'M' )  id[i]= n++;
        else                     id[i]= m++;
    }
    
    for( int i= 0; i< t; ++i )
        for( int j= 0; j< t; ++j )
        if( p[i].sex[0]== 'M' && p[j].sex[0]== 'F'  &&  isok( p[i], p[j] ) )
            map[ id[i] ][ id[j] ]= true;
}

bool dfs( int p )
{
    for( int i= 0; i< m; ++i )
    if( !visite[i] && map[p][i] )
    {
        visite[i]= true;
        
        int t= match[i];
        match[i]= p;
        
        if( t== -1 || dfs(t) )  return true;
        match[i]= t;
    }
    
    return false;
}

int Match()
{
    int ret= 0;
    
    for( int i= 0; i< n; ++i )
    {
        memset( visite, false, sizeof(visite) );
        
        if( dfs(i) )  ret++;
    }
    
    return ret;
}

int main()
{
    int test;
    scanf("%d", &test );
    
    while( test-- )
    {
        scanf("%d", &t );
        
        for( int i= 0; i< t; ++i )
            scanf("%d %s %s %s", &p[i].height, p[i].sex, p[i].music, p[i].sport );
            
        build();
        printf("%d\n", t- Match() );
    }
    
    return 0;
}


 

你可能感兴趣的:(String,Integer,input,character,each,output)