Poj-2289 Jamie's Contact Groups 多重二分图匹配

题目链接

题目大意:n个联系人,分成m组。每个联系人有各自希望所在的分组号。要求对联系人分组,使得每个联系人只能存在一个分组中,且最大分组最小。


#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1005;
const int Maxn = 505;
const int Mod = 1000000007;
const double inf = 1<<30;
int n,m,limit;
int map[maxn][Maxn];
int vcy[maxn],cy[Maxn][maxn];
bool vis[maxn];
char str[20];
bool FindPath( int u )
{
	for( int i = 0; i < m; i ++ )
	{
		if( !vis[i] && map[u][i] )
		{
			vis[i] = true;
			if( vcy[i] < limit )
			{
				cy[i][vcy[i]++] = u;
				return true;
			}
			for( int j = 0; j < vcy[i]; j ++ )
			{
				if( FindPath( cy[i][j] ) )
				{
					cy[i][j] = u;
					return true;
				}
			}
		}
	}
	return false;
}
bool MaxMatch()
{
	memset( vcy,0,sizeof(vcy) );
	for( int i = 1; i <= n; i++ )
	{
		memset( vis,0,sizeof(vis) );
		if( !FindPath( i ) )
			return false;
	}
	return true;
}
int MulMatch()
{
	int ld = 0,rd = n;
	while( ld < rd )
	{
		limit = ( ld+rd ) >>1;
		if( MaxMatch() ){
			rd = limit;
		}
		else
			ld = limit + 1;
	}
	return ld;
}
int main()
{
	#ifndef ONLINE_JUDGE   
	freopen("data.txt","r",stdin);   
	#endif
	int a;
	char ch;
	while( scanf("%d%d\n",&n,&m) != EOF,(n||m) )
	{
		memset( map,0,sizeof(map) );
		for( int i = 1; i <= n; i ++ )
		{
			scanf("%s",str);
			ch = getchar();
			while( ch != '\n' )
			{
				scanf("%d",&a);
				map[i][a] = 1;
				ch = getchar();
			}
		}
		printf("%d\n",MulMatch());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值