#include <stdio.h>
#include <string.h>
#include <stdlib.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define N 510
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct Stu
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int height;
char sex[5];
char music[110];
char sport[110];
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Stu p[N];
bool map[N][N];
int match[N];
bool visite[N];
int id[N];
int n,m,t;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
bool isok( Stu const& a, Stu const& b )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return abs( a.height- b.height )<= 40 && strcmp(a.music,b.music )== 0 && strcmp(a.sport,b.sport )!= 0 ;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int abs( int const& a )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
return a> 0? a: -a;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void build()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
n= 0, m= 0;
memset( map, false, sizeof(map) );
memset( match, -1, sizeof(match) );
for( int i= 0; i< t; ++i )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
bool dfs( int p )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
for( int i= 0; i< m; ++i )
if( !visite[i] && map[p][i] )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
visite[i]= true;
int t= match[i];
match[i]= p;
if( t== -1 || dfs(t) ) return true;
match[i]= t;
}
return false;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int Match()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int ret= 0;
for( int i= 0; i< n; ++i )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
memset( visite, false, sizeof(visite) );
if( dfs(i) ) ret++;
}
return ret;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int test;
scanf("%d", &test );
while( test-- )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
posted on 2008-11-05 12:05
Darren 阅读(280)
评论(1) 编辑 收藏 引用