描述
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
输入
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.
输出
For each test case in the input there should be one line with an integer giving the maximum number of eligible pupils.
样例输入
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
样例输出
3
7
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
using namespace std;
int map[505][505],vis[505],n,pre[505];
struct data
{
int h;
char sex;
char music[110],sport[110];
}f[505];
bool ok(int i,int j)
{
if(abs(f[i].h-f[j].h)>40)
return 1;
if(f[i].sex==f[j].sex)
return 1;
if(strcmp(f[i].music,f[j].music)!=0)
return 1;
if(strcmp(f[i].sport,f[j].sport)==0)
return 1;
return 0;
}
int find(int i)
{
for(int j=1;j<=n;j++)
if(!vis[j]&&map[i][j])
{
vis[j]=1;
if(pre[j]==0||find(pre[j]))
{
pre[j]=i;
return 1;
}
}
return 0;
}
int main()
{
int t,i,j,ans;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d",&n);
memset(map,0,sizeof(map));
memset(pre,0,sizeof(pre));
for(i=1;i<=n;i++)
scanf("%d %c%s%s",&f[i].h,&f[i].sex,f[i].music,f[i].sport);
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(ok(i,j)==0)
map[i][j]=map[j][i]=1;
}
}
for(i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
ans+=find(i);
}
printf("%d\n",n-ans/2);
}
}