Guardian of decencygrade: 10/discount: 0.8
Time Limit: 2000 ms memory limit: 65536 K
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
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 510struct 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;}