UVALive - 3415Guardian of Decency(二分图最大独立点集)

    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.

题意:一个极度封建的老师要带同学们出去玩,但是他怕在途中同学之间发生恋情,所以只带任意两个学生满足下列要求之一的去.
1》身高差 > 40
2>性别相同
3》爱好不同的音乐

4>爱好同类型的运动

思路:我们可以把两个可能发生恋情的人之间连一条边,酱求一个最大独立点集就阔以了,正好可以分为男女两列,边只会出现在男女之间,注意建图一定要分两列,千万不要混在一起

最大独立点集+最小点覆盖 = n,道理很清楚,去掉最小点覆盖的所有点,图中一条边都没有惹,可不就最大独立点集了嘛.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

struct node
{
	int h;
	char x;
	string music;
	string kind;
} sm[520],sf[520];

int n,p,q;
int g[520],vis[520];
int mp[520][520];

int find(int x)
{
	for(int i = 1;i<= q;i++)
	{
		if(mp[x][i]&&!vis[i])
		{
			vis[i] = 1;
			
			if(!g[i]||find(g[i]))
			{
				g[i] = x;
				return 1;
			}
		}
	}
	
	return 0;
}

void init()
{
	p = q = 0;
	mem(mp,0);
	mem(g,0);
}

int main()
{
	int t;
	cin>>t;
	
	while(t--)
	{
		init();
		scanf("%d",&n);
		for(int i = 1;i<= n;i++)
		{
			node st;
			cin>>st.h>>st.x>>st.music>>st.kind;
			if(st.x == 'M')
				sm[++p] = st;
			else
				sf[++q] = st;
		}
		
		for(int i = 1;i<= p;i++)
		{
			for(int j = 1;j<= q;j++)
			{
				if(abs(sm[i].h-sf[j].h)<= 40&&sm[i].music.compare(sf[j].music) == 0&&sm[i].kind.compare(sf[j].kind) != 0)
					mp[i][j] = 1;
			}
		}
		
		int sum = 0;
		for(int i = 1;i<= p;i++)
		{
			mem(vis,0);
			if(find(i))
				sum++;
		}
		
		printf("%d\n",n-sum);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nka_kun/article/details/79917285