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;
}

  


POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】的更多相关文章

  1. Poj 2289 Jamie's Contact Groups (二分+二分图多重匹配)

    题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...

  2. POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)

    POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...

  3. poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】

    题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K ...

  4. POJ 2289 Jamie's Contact Groups 二分图多重匹配 难度:1

    Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: ...

  5. POJ 2289 Jamie's Contact Groups(多重匹配+二分)

    题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是 ...

  6. POJ 2289 Jamie's Contact Groups 【二分】+【多重匹配】(模板题)

    <题目链接> 题目大意: 有n个人,每个人都有一个或者几个能够归属的分类,将这些人分类到他们能够归属的分类中后,使所含人数最多的分类值最小,求出该分类的所含人数值. 解题分析: 看到求最大 ...

  7. POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment

    这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...

  8. POJ 2289 Jamie's Contact Groups (二分+最大流)

    题目大意: 有n个人,可以分成m个组,现在给出你每个人可以去的组的编号,求分成的m组中人数最多的组最少可以有多少人. 算法讨论: 首先喷一下这题的输入,太恶心了. 然后说算法:最多的最少,二分的字眼. ...

  9. POJ - 2289 Jamie's Contact Groups (二分图多重匹配)

    题意:N个人,M个团体.每个人有属于自己的一些团体编号.将每个人分配到自己属于的团体中,问这个人数最多的团体其人数最小值是多少. 分析:一个一对多的二分图匹配,且是最大值最小化问题.二分图的多重匹配建 ...

随机推荐

  1. Websocket,ProtoBuffer,Hightlight,JSON 等,最近遇到的一些知识点小结

    websocket websocket 支持但不仅限于web场景,是一种封装好的socket通信协议,可以用来做C-S通信. 可以通过设置 binaryType 来指定通信的数据流格式,从而达到简洁高 ...

  2. Linux配置国内的Yum源

    因为Linux默认的yum源是国外的源,所以会有卡顿,缓慢的情况.而国内的Yum源相对速度较快,现在也比较成熟,所以给Linux更换国内Yum源是一个很好的选择. 1.  备份(备份之前需要yum i ...

  3. Windows Server 2016 IIS10安装URLRewrite 2.0组件方法

    1,打开Regedit> HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ InetStp2,编辑“MajorVersion”并以十进制设置数值数据值为93 ...

  4. 剑指offer —— 替换空格

    1.问题:请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 2.思路:可能首先想到的应该就是 ...

  5. 数据结构19: BF算法(普通模式匹配算法)

    判断两个串之间是否存在主串与子串的关系,这个过程称为串的模式匹配. 在串的模式匹配过程,子串 T 通常被叫做“模式串”. 普通的模式匹配(“BF”算法) 判断两个串是否存在子串与主串的关系,最直接的算 ...

  6. linux的理解

    1.用户组 因为linux 是多人多任务系统 所有可能有很多人在主机人作业.比如 有A B C D 4个人 在linux主机上作业, A B C 3个人 在做同一个项目 建了一个文件夹这个文件只能A ...

  7. Infiniband交换机的FabricMonitor加载数据hang

    刚刚帮客户将Exadata中Infiniband交换机的固件版本从2.1.3-1 升级到2.2.7-1,但升级后发现Infiniband交换机的FabricMonitor功能无法使用,具体如下图所示. ...

  8. [Node.jS]shelljs

    shelljs : https://www.npmjs.org/package/shelljs 要给可以替代Unix下shell脚本的库. require('shelljs/global'); if ...

  9. java中\r ,\n,\t等的含义

    \t 相当于tab,缩进,制表符\n NewLine 换行 \f 换页符 \r 回车 \" 转义 “\\ 转义 \

  10. apache htaccess 一个 例子

    <Files ~ "^.(htaccess|htpasswd)$"> deny from all </Files> DirectoryIndex index ...