Jamie's Contact Groups
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 7026 | Accepted: 2323 |
Description
Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The contact list has become so long that it often takes a long time for her to browse through the whole list to find a friend's number. As Jamie's best friend and a programming genius, you suggest that she group the contact list and minimize the size of the largest group, so that it will be easier for her to search for a friend's number among the groups. Jamie takes your advice and gives you her entire contact list containing her friends' names, the number of groups she wishes to have and what groups every friend could belong to. Your task is to write a program that takes the list and organizes it into groups such that each friend appears in only one of those groups and the size of the largest group is minimized.
Input
There will be at most 20 test cases. Ease case starts with a line containing two integers N and M. where N is the length of the contact list and M is the number of groups. N lines then follow. Each line contains a friend's name and the groups the friend could belong to. You can assume N is no more than 1000 and M is no more than 500. The names will contain alphabet letters only and will be no longer than 15 characters. No two friends have the same name. The group label is an integer between 0 and M - 1. After the last test case, there is a single line `0 0' that terminates the input.
Output
For each test case, output a line containing a single integer, the size of the largest contact group.
Sample Input
3 2 John 0 1 Rose 1 Mary 1 5 4 ACM 1 2 3 ICPC 0 1 Asian 0 2 3 Regional 1 2 ShangHai 0 2 0 0
Sample Output
2 2
Source
Shanghai 2004
本题显而易见,是二分答案加上二分图多重匹配,
需要注意的是读入
本题显而易见,是二分答案加上二分图多重匹配,
需要注意的是读入
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=1010;
int linker[maxn][maxn];
int g[maxn][maxn];
int num[maxn];
int used[maxn];
int uN,vN;
bool dfs(int u){
for(int v=1;v<=vN;v++){
if(g[u][v]&&!used[v]){
used[v]=1;
if(linker[v][0]<num[v]){
linker[v][++linker[v][0]]=u;
return true;
}
for(int i=1;i<=num[v];i++)
if(dfs(linker[v][i])){
linker[v][i]=u;
return true;
}
}
}
return false;
}
int hungary(){
int res=0;
mem0(linker);
for(int u=1;u<=uN;u++){
mem0(used);
if(dfs(u))
res++;
}
return res;
}
int main(){
int n,m;
int u;
char s[100];
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)
break;
mem0(g);
for(int i=1;i<=n;i++){
scanf("%s",s);
while(true){ //注意数字可能是连续的多个字符组成的
scanf("%d",&u);
g[i][u+1]=1;
if(getchar()=='\n')
break;
}
}
uN=n;
vN=m;
int high=1010,low=0;
while(high-low>=0){
int mid=(low+high)>>1;
for(int i=1;i<=m;i++)
num[i]=mid;
int ans=hungary();
if(ans>=n)
high=mid-1;
else
low=mid+1;
}
printf("%d\n",low);
}
return 0;
}