sicily 1192 二分匹配

//求二分图最大独立集,点之间无边
//先求最大匹配,剩下的点之间不可能有边,再加上匹配数(每对匹配只取一个人)即可
//如果剩下的点中,男的与匹配点中女的有边,就取该匹配的男点
//不可能出现不在匹配中的男女两点同时与同一匹配边有边,因为这样会导致增广
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;


struct Person
{
	int high;
	char music[101];
	char sport[101];
	int nxt[501],m;
	bool get;
	int _in;
}B[501],G[501];

bool couple( int i, int j )
{
	if( abs( B[i].high - G[j].high ) > 40 )           return false;
	if( strcmp( B[i].music, G[j].music ) != 0 ) return false;
	if( strcmp( B[i].sport, G[j].sport ) == 0 ) return false;
	return true;
}
int n, t;
int s[501];
int high;
char str[2];
bool _vis[501];
int ymatch[501];
int x = 0, y = 0;
bool find( int xx )
{
	for( int i = 0;i < B[xx].m;i++ )
	{
		int y = B[xx].nxt[i];
		if( _vis[y] ) continue;
		_vis[y] = true;

		if( ymatch[y] == -1 || find( ymatch[y] ) )
		{
			ymatch[y] = xx;
			return true;
		}
	}
	return false;
}
int Pair( )
{
	int result = 0;
	memset( ymatch,-1,sizeof(ymatch) );
	for( int i = 0;i < x;i++ )
	{
		memset( _vis,false,sizeof(_vis) );
		if( find( i ) ) result++;
	}
	return result;
}

int main()
{
	scanf( "%d",&t );
	while( t-- )
	{
		scanf( "%d",&n );
		x = y = 0;
		for( int i = 0;i < n;i++ )
		{
			s[i] = i;
			B[i].m = 0;
			scanf( "%d%s",&high,str );
			if( str[0] == 'M' )
			{
				B[x].high = high;
				scanf( "%s%s",B[x].music,B[x].sport );
				x++;
			}
			else
			{
				G[y].high = high;
				scanf( "%s%s",G[y].music,G[y].sport );
				y++;
			}
		}
		for( int i = 0;i < x;i++ )
		{
			for( int j = 0;j < y;j++ )
			{
				if( couple( i,j ) )
				{
					B[i].nxt[B[i].m++] = j;
				}
			}
		}
		printf( "%d\n",n - Pair( ) );
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值