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. Win32 API编程:使用CreateProcess创建新进程

    #include <windows.h> #include <tchar.h> #include <stdio.h> int main(int argc, char ...

  2. define和typedef

    #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查,不关含义是否正确照样带入,只有在编译已被展开的源程序时才会发现可能的错误并报错. 所以define后加分号,不然会把分号也会一 ...

  3. Git 基础 —— 常用命令

    Git 基础学习系列 Git 基础 -- 安装 配置 别名 对象 Git 基础 -- 常用命令 Git 基础 -- 常见使用场景 Git基础 -- Github 的使用 git init 创建 Git ...

  4. 如何修改ls命令列出来的目录颜色

    答:默认为蓝色,在黑色背景下无法看清楚,因此以以下方法修改; 1.往~/.bash_profile文件中添加以下内容: export LS_COLORS='no=00:fi=00:di=01;33:l ...

  5. 02_Flume1.6.0安装及单节点Agent实践

    Flume1.6.0的安装1.上传Flume-1.6.0-tar.gz到待部署的所有机器     以我的为例: /usr/local/src/ 2.解压得到flume文件夹      # tar -x ...

  6. bnu 51636 Squared Permutation 线段树

    Squared Permutation Time Limit: 6000ms Memory Limit: 262144KB 64-bit integer IO format: %lld      Ja ...

  7. spring boot打jar包发布

    artifactId 是即将打包的包的名称 version 是即将打包的版本号 packaging 是即将打包的格式,这里讲的是jar包 终端输入命令: mvn clean install 然后在ta ...

  8. Cglib方法实现动态代理

    除了使用JDK方式产生动态代理外,Java还给我们提供了另外一种产生动态代理的方法,那就是使用cglib. cglib是这样实现动态代理的: · ①.针对类来实现代理 · ②对指定目标类产生一个子类 ...

  9. Android手机摄像头编程入门

    本讲内容:Android手机摄像头编程入门智能手机中的摄像头和普通手机中的摄像头最大的区别在于,智能机上的摄像头可以由程序员写程序控制, 做一些有趣的应用譬如,画中画,做一些有用的应用譬如二维码识别, ...

  10. U3D协程yield的使用和理解

    部分内容参考网址:http://blog.csdn.net/huang9012/article/details/29595747 Win7+U3D 4.6.7 1.在c#中使用①首选需要定义一个返回值 ...