Jamie's Contact Groups

Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 题目大意:给你n个人可能的分组,给你m个组。问你将n个人分到组中,且每个人只能分到一个组内,最大组(组内人数最多)的最小值是多少。 解题思路:多重匹配。这是一对多的情况。很明显是多重匹配,但是匹配次数却没有,而是让求的结果。那我们就枚举匹配次数。如果该匹配次数满足条件,记录答案,同时向小逼近,如果不满足,就向大逼近。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int maxn = 1010;
int Map[maxn][maxn];
//linker[v][j]表示第j个与Y部的v匹配的是x部的谁
int linker[maxn][maxn], used[maxn];
int mid;
bool dfs(int u,int rn){
for(int v = 1; v <= rn; v++){
if(used[v] || !Map[u][v]){
continue;
}
used[v] = 1;
if(linker[v][0] < mid){ //枚举的匹配次数
linker[v][++linker[v][0]] = u;
return true;
}else{
for(int j = 1; j <= linker[v][0]; j++){
if(dfs(linker[v][j],rn)){
linker[v][j] = u;
return true;
}
}
}
}
return false;
}
bool Hungary(int ln,int rn){
int ret = 0;
for(int i = 0; i <= rn; i++){
linker[i][0] = 0;
}
for(int i = 1; i <= ln; i++){
memset(used,0,sizeof(used));
if(dfs(i,rn)){
ret++;
}
}
if(ln == ret){
return true;
}
return false;
}
int main(){
int n,m;
char str[5000];
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
memset(Map,0,sizeof(Map));
getchar();
for(int i = 1; i <= n; i++){
gets(str);
int len = strlen(str);
int v = 0, flag = 1;
for(int j = 0; j <= len; j++){
if(str[j] >= '0'&& str[j] <= '9'){
v = v*10 + str[j]-'0';
flag = 0;
}else{
if(flag) continue;
else{
Map[i][v+1] = 1; v = 0;
}
}
}
}
int l = 1, r = n;
while(l < r){
mid = (l+r)/2;
if(Hungary(n,m)){
r = mid;
}else{
l = mid + 1;
}
}
printf("%d\n",r);
}
return 0;
}

  


最新文章

  1. arm_GPIO_简单编程例题
  2. 关于CSV文件 Excel打开乱码问题的解决方案
  3. 在React 组件中使用Echarts
  4. JS跨域解决iframe高度自适应(IE8/Firefox/Chrome适用)
  5. Jmeter调试工具---Debug Sampler
  6. java:访问权限-protected实例
  7. Windows7 64位机上Emgu CV2.4.2安装与配置
  8. 记录创建企业Wiki的几个开源项目
  9. Labeling Balls(拓扑排序wa)
  10. spring-线程池(2)
  11. phpExcel读取excel文件数据
  12. Redux源码分析之compose
  13. Java 强引用 软引用 弱引用 虚引用详解
  14. 线上服务器上安装的VNCServer不能正常工作
  15. Tensorflow简单CNN实现
  16. 浅析对spring中IOC的理解
  17. 【Linux】理解分区
  18. vue-cli 上手
  19. 哪个先执行:@PostConstruct和@Bean的initMethod?
  20. Rplot

热门文章

  1. 对XML文档进行修改
  2. web安全-接入层注入
  3. dede地图显示最新文章的解决方法
  4. pytorch 0.4.1安装问题
  5. JScrollPane (滚动面板)使用心得
  6. maven 子父工程。。。
  7. js 的 一些操作。。。
  8. Nginx静态服务,域名解析
  9. SQL注入工具sqlmap的注入过程记录
  10. Linux字符设备驱动--Led设备驱动