void-man

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

给出一些成为恋爱对象的条件,再给出一些人的信息,问你最多能从中挑出多少个童鞋,没有恋爱关系?

首先把信息读入后,循环一次建图,看谁有恋爱关系,然后求出二分匹配,由于恋爱是双向并且两个集合相同,所以找出的二分匹配必定是偶数

并且匹配的线段中有A-B and B-A 的双向匹配,所以题目要求的是最大独立集合,所以答案就是节电个数n-最大匹配/2

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
#define MAX 505
using namespace std;
typedef struct NODE{
	int h;
	char sex,mus[20],hob[20];
}NODE;
NODE p[MAX];
int used[MAX],match[MAX];
int map[MAX][MAX];
int Augement(int n,int x)
{
	int i ;
	for(i=1; i<=n; i++)
		if( map[x][i] && !used[i] )
		{
			used[i] = 1;
			if( !match[i] || Augement(n,match[i]) )
			{
				match[i] = x;
				return 	1;
			}
		}
	return 0;
}
int Hungary(int n)
{
	int i,sum = 0;
	memset(match,0,sizeof(match));
	for(i=1; i<=n; i++)
	{
		memset(used,0,sizeof(used));
		if( Augement(n,i) )
			sum++;
	}
	return sum;
}
int love(int i,int k)
{
	if( abs(p[i].h - p[k].h) > 40 )
		return 0;
	if( p[i].sex == p[k].sex )
		return 0;
	if( strcmp(p[i].mus,p[k].mus) )
		return 0;
	if( strcmp(p[i].hob,p[k].hob) == 0 )
		return 0;
	return 1;
}
int main()
{
	int ncases;
	int n,i,k;
	scanf("%d",&ncases);
	while( ncases-- )
	{
		memset(map,0,sizeof(map));
		scanf("%d",&n);
		for(i=1; i<=n; i++)
			scanf("%d %c %s %s",&p[i].h,&p[i].sex,p[i].mus,p[i].hob);
		for(i=1; i<=n; i++)
			for(k=1; k<=n; k++)
				if( love(i,k) )
					map[i][k] = 1;
		int ans = Hungary(n);
		printf("%d\n",n-ans/2);
	}
return 0;
}
posted on 2011-05-16 00:02  void-man  阅读(164)  评论(0)    收藏  举报