二分图最大独立集——POJ 2771

对应POJ题目:点击打开链接


Guardian of Decency
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

题意:满足以下条件之一的两个人就不能结婚:

1:身高相差40cm(不包括)

2:性别相同

3:音乐品味不同

4:体育爱好相同

有N个人,问最多有多少人,使他们任意两人之间都不能结婚。

思路:明显就是图的最大独立集了,想办法化为二分图,即是按性别划分,男一边,女一边,然后能结婚的就从男那边连一条线到女那边。那同一边的肯定不会有连线,满足二分图。故ans = N -最大匹配。


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#define ms(x,y) memset(x,y,sizeof(x))
#define ABS(x) ((x)>0?(x):-(x))
const int MAXN=500+10;
const int INF=1<<30;
using namespace std;
int bn,gn;
int link[MAXN];
int vis[MAXN];
int net[MAXN][MAXN];

struct People
{
	int h;
	char sex[3],music[105],sport[105];
}boy[MAXN],girl[MAXN];

int dfs(int u)
{
	for(int v=0; v<gn; v++){
		if(!vis[v] && net[u][v]){
			vis[v] = 1;
			if(link[v] == -1 || dfs(link[v])){
				link[v] = u;
				return 1;
			}
		}
	}
	return 0;
}

int MaxMatch()
{
	int res = 0;
	ms(link, -1);
	for(int u=0; u<bn; u++){
		ms(vis, 0);
		if(dfs(u)) res++;
	}
	return res;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	int T;
	scanf("%d", &T);
	while(T--)
	{
		ms(net,0);
		bn = 0;
		gn = 0;
		int n;
		scanf("%d", &n);
		People p;
		for(int i=0; i<n; i++){
			scanf("%d%s%s%s", &p.h, p.sex, p.music, p.sport);
			if(p.sex[0] == 'F') boy[bn++] = p;
			else girl[gn++] = p;
		}
		for(int i=0; i<bn; i++){
			for(int j=0; j<gn; j++){
				if(ABS(boy[i].h - girl[j].h) <= 40 && !strcmp(boy[i].music, girl[j].music) && strcmp(boy[i].sport,girl[j].sport))
		 		net[i][j] = 1;
			}
		}
		printf("%d\n", n - MaxMatch());
	}
	return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值