题目大意: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;
}