您的位置:首页 > 其它

hdu 1669 Jamie's Contact Groups(二分+二分图最大多重匹配)

2016-07-28 10:18 274 查看


Jamie's Contact Groups

Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 551    Accepted Submission(s): 189


Problem 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

2004 Asia Regional Shanghai

题意:有n个人和m个地方,每个人可以属于多个地方,问怎么样选择每个人去哪个地方才可以得到人数最多的地方人最少

思路:要求人数最多的地方人最少,明显的要用二分法,枚举人数最多的人数即可。然后每次都多重匹配,满足就往下移动,不满足就往上

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1100
#define M 550
int ma
[M];
int n,m;
int line[M]
,vis[M],num[M],cnt[M];
int can(int t)
{
for(int i=0; i<m; i++)
{
if(!vis[i]&&ma[t][i])
{
vis[i]=1;
if(cnt[i]<num[i])
{
line[i][cnt[i]++]=t;
return 1;
}
else
{
for(int j=0; j<num[i]; j++)
{
if(can(line[i][j]))
{
line[i][j]=t;
return 1;
}
}
}
}
}
return 0;
}
int main()
{
char op[20],s[100000];
while(~scanf("%d %d",&n,&m)&&(n+m))
{
memset(ma,0,sizeof(ma));
for(int i=1; i<=n; i++)
{
scanf("%s ",op);
gets(s);
int id=0,len=strlen(s);
for(int j=0; j<len; j++)
{
if(s[j]>='0'&&s[j]<='9')
{
id=id*10+s[j]-'0';
if(j==len-1) ma[i][id]=1;
}
else if(j>0&&s[j-1]>='0'&&s[j-1]<='9')
{
ma[i][id]=1;
id=0;
}
}
}
int l=0,r=2000,last;
while(l<=r)
{
int mid=(l+r)>>1;
for(int i=0; i<m; i++)
num[i]=mid;
memset(cnt,0,sizeof(cnt));
int ans=0;
for(int i=1; i<=n; i++)
{
memset(vis,0,sizeof(vis));
if(can(i))  ans++;
else break;
}
if(ans<n) l=mid+1;
else last=mid,r=mid-1;
}
printf("%d\n",last);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: