题意:有n个联系人,有m个列表,一个联系人只属于一个列表,一个列表可以有多个联系人,是联系人最多的列表里人数尽量少,输出。
解法:联系人为左集合,列表为右集合,二分查找,匹配的人数。
/*
----------------------------------
Love is more than a word.
It says so much.
When I see these four letters,
I almost feel your touch.
This is only happened since
I fell in love with you.
Why this word does this,
I haven't got a clue.
To My Goddess
CY
----------------------------------
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 1000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define REP(i,a,b) for(i=a;i<=b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define clehead(a) rep(i,maxn)a[i]=-1
/*
The time of story :
** while(1)
{
once upon a time,
there was a mountain,
on top of which there was a temple,
in which there was an old monk and a little monk.
Old monk was telling stories inside the temple.
What was he talking about?
** }
ÎûÎû
(*^__^*)
*/
#define sci(a) scanf("%d",&a)
#define scd(a) scanf("%lf",&a)
#define pri(a) printf("%d",a)
#define prie(a) printf("%d\n",a)
#define prd(a) printf("%lf",a)
#define prde(a) printf("%lf\n",a)
#define pre printf("\n")
#define LL(x) x<<1
#define RR(x) x<<|1
#define pb push_back
#define mod 90001
#define PI 3.141592657
const ull INF = 1LL << 61;
const int inf = int(1e5)+10;
const double eps=1e-5;
using namespace std;
struct node
{
int u,v,w;
int next;
};
bool cmp(int a,int b){
return a>b;
}
int bmap[maxn][maxn];
bool bmask[maxn];
int nx,ny;
int vcy[maxn];
int cy[maxn][maxn];
int limit;
int left,right;
bool findpath(int u)
{
int i,j;
rep(i,ny)if(bmap[u][i]&&!bmask[i])
{
bmask[i]=1;
if(vcy[i]<limit){
cy[i][vcy[i]++]=u;
return true;
}
rep(j,vcy[i])if(findpath(cy[i][j]))
{
cy[i][j]=u;
return true;
}
}
return false;
}
bool mulmatch()
{
cle(vcy);
int i,j,k;
rep(i,nx)
{
cle(bmask);
if(!findpath(i))return false;
}
return true;
}
char str[2005];
int main()
{
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>nx>>ny)
{
if(nx+ny==0)break;
getchar();
int i,j,k;
cle(bmap);
rep(j,nx){
gets(str);
rep(i,strlen(str))
{
if(str[i]>='0'&&str[i]<='9')
{
int v=0;
while(str[i]>='0'&&str[i]<='9')
{
v*=10;
v+=str[i]-'0';
i++;
}
bmap[j][v]=true;
}
}
}
int l=0;
int r=nx;
while(l<r)
{
limit=(l+r)>>1;
if(mulmatch())r=limit;
else l=limit+1;
}
cout<<r<<endl;
}
return 0;
}