poj 2289 Jamie's Contact Groups(二分+多重匹配)

来源:互联网 发布:淘宝海报艺术字 编辑:程序博客网 时间:2024/04/29 11:30
Jamie's Contact Groups
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 5987 Accepted: 1888

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 2John 0 1Rose 1Mary 15 4ACM 1 2 3ICPC 0 1Asian 0 2 3Regional 1 2ShangHai 0 20 0

Sample Output

22
 
题意:有n个人的姓名,现在要将这n个姓名分组,给出每个姓名能分在哪些组。求如何分组使最多姓名的组里面的姓名数量最少。
思路:二分每组能分得的最大数目,然后用二分图的多重匹配。
 
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <stack>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)using namespace std;const int maxn=1005;const int maxm=505;bool map[maxn][maxm];bool vis[maxm];int match[maxm][maxn],cnt[maxm];int n,m;bool find(int x,int lim){    for(int i=0; i<m; i++)        if(!vis[i]&&map[x][i])        {            vis[i]=true;            if(cnt[i]<lim)            {                match[i][cnt[i]++]=x;                return true;            }            for(int j=0;j<cnt[i];j++)            if(find(match[i][j],lim))            {                match[i][j]=x;                return true;            }        }    return false;}int MMG(int lim){    int ans=0;    memset(cnt,0,sizeof(cnt));    for(int i=0; i<n; i++)    {        memset(vis,false,sizeof(vis));        if(find(i,lim)) ans++;    }    return ans;}int main(){    int k;    char s[20],ch;    while(scanf("%d%d",&n,&m),n||m)    {        memset(map,false,sizeof(map));        for(int i=0;i<n;i++)        {            scanf("%s",s);            while((ch=getchar())==' ')            {                scanf("%d",&k);                map[i][k]=true;            }        }        int low=0,high=1000;        int mid;        while(low<high)        {            mid=(low+high)>>1;            int t=MMG(mid);            //cout<<low<<" "<<high<<" "<<mid<<endl;            //cout<<t<<endl;            if(t==n) high=mid;            else low=mid+1;        }        printf("%d\n",low);    }    return 0;}

原创粉丝点击