天天看点

【POJ 2771】Guardian of Decency【题目】【分析】【代码】

【题目】

传送门

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

【分析】

题目大意:(多组数据)有 n n n 个人,每个人都有 4 4 4 个属性,分别是:身高、性别、喜欢的音乐、喜欢的运动。如果两个人能成为情侣,那么他们的身高差 ≤ 40 ≤40 ≤40,性别不同,喜欢的音乐相同,且喜欢的运动不同。现在要在这 n n n 个人中选出一些不能互相成为情侣的人,求最大的人数。

其实,这道题的话,直接算应该是不好算的,我们不如反着想。

就是说,对于能够成为情侣的两个人,我们对他们连边,那么最后求一个最大点独立集就行了。

而最大点独立集 = = = 所有顶点数 − - − 最小顶点覆盖(也就是最大匹配)。

那么直接建完图跑一边匈牙利算法就行了。

update(2019.10.21):这个好像叫做二分图最大团。

【代码】

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 505
using namespace std;
int n,a[N][N],match[N],height[N];
string gender[N],music[N],sport[N];
bool vis[N];
bool check(int i,int j)
{
	if(music[i]!=music[j])  return 0;
	if(sport[i]==sport[j])  return 0;
	if(gender[i]==gender[j])  return 0;
	if(abs(height[i]-height[j])>40)  return 0;
	return 1;
}
bool find(int x)
{
	int i;
	for(i=1;i<=n;++i)
	{
		if(a[x][i]&&!vis[i])
		{
			vis[i]=true;
			if(!match[i]||find(match[i]))
			{
				match[i]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=1;i<=n;++i)
		{
			scanf("%d",&height[i]);
			cin>>gender[i]>>music[i]>>sport[i];
		}
		memset(a,0,sizeof(a));
		memset(match,0,sizeof(match));
		for(i=1;i<=n;++i)
		  for(j=i+1;j<=n;++j)
		    if(check(i,j))
		      a[i][j]=a[j][i]=1;
		int ans=0;
		for(i=1;i<=n;++i)
		{
			memset(vis,false,sizeof(vis));
			bool flag=find(i);
			if(flag)  ans++;
		}
		printf("%d\n",n-ans/2);
	}
	return 0;
}
           

继续阅读