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

  1. 3 2
  2. John 0 1
  3. Rose 1
  4. Mary 1
  5. 5 4
  6. ACM 1 2 3
  7. ICPC 0 1
  8. Asian 0 2 3
  9. Regional 1 2
  10. ShangHai 0 2
  11. 0 0

Sample Output

  1. 2
  2. 2
    这是一对多的匹配,就是增广路改一改,改成num<limit时可以增广就行
    如果group的匹配数小于当前要找的答案,那么就直接加入group的匹配集里,否则在匹配集里找可以增广的点修改
    需要注意的是used数组,如果a->groupx->a就会无限调用,所以设置了一个used数组每次都清空,有点懒
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <sstream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. const int maxn=;
  9. int m,n;
  10. int num[maxn],cmp[maxn][maxn],used[maxn],tot;
  11. vector<int > G[maxn];
  12.  
  13. void printg(){
  14. for(int i=;i<n;i++){
  15. for(int j=;j<G[i].size();j++){
  16. int t=G[i][j];
  17. cout<<"G["<<i<<"]["<<j<<"]:"<<t<<" ";
  18. }
  19. cout<<endl;
  20. }
  21. }
  22.  
  23. bool fnd(int s,int limit){
  24. for(int i=;i<G[s].size();i++){
  25. int t=G[s][i];
  26. if(used[t])continue;
  27. used[t]=true;
  28. if(num[t]<limit){cmp[t][num[t]++]=s;return true;}
  29. for(int j=;j<num[t];j++){
  30. if(fnd(cmp[t][j],limit)){
  31. cmp[t][j]=s;
  32. return true;
  33. }
  34. }
  35. }
  36. return false;
  37. }
  38.  
  39. bool hungry(int limit){
  40. tot=;
  41. memset(num,,sizeof(num));
  42. for(int i=;i<n;i++){
  43. memset(used,,sizeof(used));
  44. if(fnd(i,limit))tot++;
  45. }
  46. if(tot==n)return true;
  47. return false;
  48. }
  49.  
  50. int getans(int s,int e){
  51. int mid=(s+e)/;
  52. if(hungry(mid)){
  53. return s==mid?mid:getans(s,mid);
  54. }
  55. return s==mid?e:getans(mid,e);
  56. }
  57.  
  58. string buff,rbuff;
  59. int main(){
  60. cin.tie();
  61. ios::sync_with_stdio(false);
  62. stringstream ss;
  63. while(cin>>n>>m&&(n||m)){
  64. getline(cin,rbuff);
  65. for(int i=;i<n;i++){
  66. G[i].clear();
  67. getline(cin,rbuff);
  68. ss.clear();
  69. ss<<rbuff;
  70. ss>>buff;
  71. int t;
  72. while(ss>>t){
  73. t+=n;
  74. G[i].push_back(t);
  75. }
  76. }
  77. //printg();
  78. if(m==)cout<<<<endl;
  79. else {
  80. int ans=getans(n/m,n+);
  81. cout<<ans<<endl;
  82. }
  83. }
  84. return ;
  85. }

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. RS(纠删码)技术浅析及Python实现

    前言 在Ceph和RAID存储领域,RS纠删码扮演着重要的角色,纠删码是经典的时间换空间的案例,通过更多的CPU计算,降低低频存储数据的存储空间占用. 纠删码原理 纠删码基于范德蒙德矩阵实现,核心公式 ...

  2. 分页器的js实现代码 bootstrap Paginator.js

    参考: http://www.jb51.net/article/76093.htm 如前所述, 不要什么都想到 jquery的 脚本js, 应该首先推荐的是 css 和 元素本身的事件 函数 如: o ...

  3. jquery hover最后解决 - 不再疑惑 - 例子在这里

    hover具有动画累计的bug, 可以使用 stop 或 filter(:not(:animated))来消除, 但是, 即使这样, 当鼠标反复滑入或滑出的时候, 虽然没有动画累计的问题, 但是 下面 ...

  4. 全球变暖|2018年蓝桥杯B组题解析第九题-fishers

    标题:全球变暖 你有一张某海域NxN像素的照片,"."表示海洋."#"表示陆地,如下所示: ....... .##.... .##.... ....##. .. ...

  5. win7系统远程桌面无法正常连接

    我的电脑--属性--远程设置:初步设置: 此外还需要确认服务是否开启

  6. Gym - 100345H Settling the Universe Up(bitset)

    https://vjudge.net/problem/Gym-100345H 题意: 给出一个图,求图中u能到达v的对数,并且u<v.并且会有更新和查询操作. 思路: bitset直接暴力,对于 ...

  7. C#高级编程第10版 note

    泛型接口的抗变和协变 https://www.cnblogs.com/yanfang/p/6635302.html ①泛型接口,如果泛型类型前没有关键字out或者in来标注,则该泛型接口不支持抗变和协 ...

  8. c++ primer plus 第四章 课后题答案

    #include<iostream> #include<string> using namespace std; int main() { string first_name; ...

  9. .net常见框架

    从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生了浓厚的兴趣,在工作和学习中也积累了一些开源的组件,就目前想到的先整理于此,如果再想到,就继 ...

  10. English trip M1 - PC12 I'd Like a Room Please Teacher:Taalan

    In this lesson you will learn to say what you need. 在本课中,您将学习如何说出您的需求. Words list elevator  电梯      ...