poj 2771 Guardian of Decency(最大独立集)

Guardian of Decency
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5397   Accepted: 2268

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

solution:

此题为求最大点独立集=n-最大匹配,因此在符合条件的时候连一条边,然后用匈牙利算法跑一遍即得答案。

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 550;
int uN, vN;  //u,v数目
int g[MAXN][MAXN];//编号是0~n-1的 
int linker[MAXN];
bool used[MAXN];
struct node
{
	char sex[5];
	char music[101];
	char sport[101];
	int high;
}man[MAXN],woman[MAXN],now;
bool ok(node a, node b)
{
	if (abs(a.high - b.high)>40)
		return 0;
	if (strcmp(a.music, b.music) != 0)
		return 0;
	if (strcmp(a.sport, b.sport) == 0)
		return 0;
	return 1;
}
bool dfs(int u)
{
	int v;
	for (v = 0; v<vN; v++)
		if (g[u][v] && !used[v])
		{
		used[v] = true;
		if (linker[v] == -1 || dfs(linker[v]))
		{
			linker[v] = u;
			return true;
		}
		}
	return false;
}
int hungary()
{
	int res = 0;
	int u;
	memset(linker, -1, sizeof(linker));
	for (u = 0; u<uN; u++)
	{
		memset(used, 0, sizeof(used));
		if (dfs(u))  res++;
	}
	return res;
}
int main()
{
	int t,n;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		uN = vN = 0;
		memset(g, 0, sizeof(g));
		for (int i = 0; i < n; i++)
		{
			scanf("%d%s%s%s", &now.high, now.sex, now.music, now.sport);
			if (now.sex[0] == 'M')
				man[uN++] = now;
			else woman[vN++] = now;
		}
		for (int i = 0; i < uN; i++)
			for (int j = 0; j < vN; j++)
				if (ok(man[i], woman[j]))g[i][j] = 1;
		printf("%d\n", n - hungary());
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_22522375/article/details/51524723