UVALive - 3415 (最大点独立集----最大二分匹配)

UVALive - 3415 (最大点独立集----最大二分匹配)

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个学生中选出一些学生,满足任意两个学生至少要满足下面的四条中的一条。
1,身高相差大于40cm
2,性别相同
3,最喜欢的音乐不同类型
4,最喜欢的体育比赛相同类型
输出可以挑选的最多学生人数。

解题思路

最大点独立集。
只要这四个条件都不满足的两个人 ,一定不能同时出现
两个集合 分别都是 1–n
所以我们把这四个条件都不满足的人连边,然后跑匈牙利算法找最大匹配
但是在匹配的过程中,有一半是重复的,加入 1 和 4不能同时去吧。你给1 配对的时候配上了4 给4配对的时候也配上了1 。
最大匹配/2 就是不能去的人数
n-最大匹配/2 就是最多能去多少。

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
const int maxn = 501;
int n;
struct node{
	int height;
	char sex;
	string music;
	string sport;
}a[maxn];
int mp[maxn][maxn];
int girl[maxn];
int used[maxn];
void init(){
	memset(mp,0,sizeof(mp)); 
	memset(girl,0,sizeof(girl));
}
bool judge(int i,int j){
	if(abs(a[i].height-a[j].height)>40) return false;
	if(a[i].sex==a[j].sex ) return false;
	if(a[i].music != a[j].music) return false;
	if(a[i].sport == a[j].sport) return false;
	return true;
}
int find(int x){
	for(int i=1;i<=n;i++){
		if(mp[x][i]&&used[i]==0){
			used[i] = 1;
			if(girl[i]==0||find(girl[i])){
				girl[i] = x;
				return true;
			}
		}
	}
	return false;
}
int solve(){
	int res = 0;
	memset(girl,0,sizeof(girl));
	for(int i=1;i<=n;i++){
		memset(used,0,sizeof(used));
		if(find(i)){
			res++;
		}
	}
	return res;
}
int main()
{
	int t;
	cin>>t;
	while(t--){
		scanf("%d",&n);
		init();
		for(int i=1;i<=n;i++){
			cin>>a[i].height>>a[i].sex>>a[i].music>>a[i].sport;
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j) continue;
				if(judge(i,j)){
					mp[i][j] = 1;
				}
			}
		}
		int ans = solve();
		ans = n-ans/2;
		cout<<ans<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值