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

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
这是一对多的匹配,就是增广路改一改,改成num<limit时可以增广就行
如果group的匹配数小于当前要找的答案,那么就直接加入group的匹配集里,否则在匹配集里找可以增广的点修改
需要注意的是used数组,如果a->groupx->a就会无限调用,所以设置了一个used数组每次都清空,有点懒
#include <iostream>
#include <cstring>
#include <vector>
#include <sstream>
#include <string>
using namespace std; const int maxn=;
int m,n;
int num[maxn],cmp[maxn][maxn],used[maxn],tot;
vector<int > G[maxn]; void printg(){
for(int i=;i<n;i++){
for(int j=;j<G[i].size();j++){
int t=G[i][j];
cout<<"G["<<i<<"]["<<j<<"]:"<<t<<" ";
}
cout<<endl;
}
} bool fnd(int s,int limit){
for(int i=;i<G[s].size();i++){
int t=G[s][i];
if(used[t])continue;
used[t]=true;
if(num[t]<limit){cmp[t][num[t]++]=s;return true;}
for(int j=;j<num[t];j++){
if(fnd(cmp[t][j],limit)){
cmp[t][j]=s;
return true;
}
}
}
return false;
} bool hungry(int limit){
tot=;
memset(num,,sizeof(num));
for(int i=;i<n;i++){
memset(used,,sizeof(used));
if(fnd(i,limit))tot++;
}
if(tot==n)return true;
return false;
} int getans(int s,int e){
int mid=(s+e)/;
if(hungry(mid)){
return s==mid?mid:getans(s,mid);
}
return s==mid?e:getans(mid,e);
} string buff,rbuff;
int main(){
cin.tie();
ios::sync_with_stdio(false);
stringstream ss;
while(cin>>n>>m&&(n||m)){
getline(cin,rbuff);
for(int i=;i<n;i++){
G[i].clear();
getline(cin,rbuff);
ss.clear();
ss<<rbuff;
ss>>buff;
int t;
while(ss>>t){
t+=n;
G[i].push_back(t);
}
}
//printg();
if(m==)cout<<<<endl;
else {
int ans=getans(n/m,n+);
cout<<ans<<endl;
}
}
return ;
}

POJ 2289 Jamie's Contact Groups 二分图多重匹配 难度:1的更多相关文章

  1. POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】

    Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

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

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

  3. POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 6 ...

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

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

  5. 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 ...

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

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

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

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

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

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

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

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

随机推荐

  1. startActivityForResult( )用法

    一.与startActivity( )的不同之处 1, startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivity( ). 2, start ...

  2. JavaScript常用操作,常用类

    算术运算符 重点关注 算数,赋值,逻辑运算符,三目运算符 <!DOCTYPE html> <html> <head> <meta charset=" ...

  3. 记录Java中对url中的参数进行编码

    Code: import java.net.URLEncoder; import java.util.HashMap; import java.util.Iterator; import java.u ...

  4. npm教程_脚手架原理以及bootstrap引入

    格式:vue init <templateName> <ProjectName> 例子:vue init webpack vue02 运行上面的命令后,脚手架帮忙按照webpa ...

  5. ros 编译包含脚本文件以及launch文件

    目录结构如下: 修改CMakeLists.txt文件 install(PROGRAMS scripts/initial_pos.py DESTINATION ${CATKIN_PACKAGE_BIN_ ...

  6. jekins 插件离线安装

    官网插件地址:http://updates.jenkins-ci.org/download/plugins/ 系统管理->插件管理->高级 选择一个下载好的插件,然后点击上传即可 然后就会 ...

  7. Couldn't find an AF_INET address for

    vim ~/.bashrc #输入内容 export ROS_HOSTNAME=你的hostname export ROS_MASTER_URI=http://(目标主机的IP):11311 expo ...

  8. jQuery双击编辑td数据

    html <td class="remark" style="width: 200px;"> {$vo.remark} </td> js ...

  9. 【Robot Framework 项目实战 02】SeleniumLibrary Web UI 自动化

    前言 SeleniumLibrary 是针对 Robot Framework 开发的 Selenium 库.它也 Robot Framework 下面最流程的库之一.主要用于编写 Web UI 自动化 ...

  10. Codeforces 834D - The Bakery(dp+线段树)

    834D - The Bakery 思路:dp[i][j]表示到第j个数为止分成i段的最大总和值. dp[i][j]=max{dp[i-1][x]+c(x+1,j)(i-1≤x≤j-1)},c(x+1 ...