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 (二分,二分图匹配)

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

Http

POJ:https://vjudge.net/problem/POJ-2289

UVA:https://vjudge.net/problem/UVA-1345

ZOJ:https://vjudge.net/problem/ZOJ-2399

HDU:https://vjudge.net/problem/HDU-1669

SCU:https://vjudge.net/problem/SCU-1996

Source

二分法,二分图最大匹配

题目大意

现在有n个人,有m个小组,每个人都有若干个小组供选择进入,但一个人最多只能进一个小组。现在求最大小组人数的最小值。

解决思路

我在前面的文章中也说过,这种求最大小组人数最小值的方法就是二分答案。

每次二分最大小组人数,然后判断能否找到一种方案使得所有小组的人数都不超过这个值。

至于怎么判断,那我们就是用二分图最大匹配中的匈牙利算法。

关于匈牙利算法的基本内容,这里不再多说,可以参考我的这三篇文章。

http://www.cnblogs.com/SYCstudio/p/7138206.html

http://www.cnblogs.com/SYCstudio/p/7138221.html

http://www.cnblogs.com/SYCstudio/p/7138230.html

最后要注意的是,因为这里的一个组内可以有多个人,所以组的Match不是唯一的,那么我们设Match[i][j],表示i组第当前匹配的第j个人,设Num[i]表示第i组的人数。那么匈牙利算法也要有一些变动,具体请看代码。

代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<string>
  8. #include<set>
  9. using namespace std;
  10. const int maxN=2001;
  11. const int maxM=1001;
  12. const int inf=2147483647;
  13. int n,m;
  14. int MaxCnt;
  15. vector<int> E[maxN];
  16. int Match[maxN][maxN];
  17. int Num[maxN];
  18. bool vis[maxN];
  19. bool check(int mid);
  20. bool Hungary(int u);
  21. int main()
  22. {
  23. while (cin>>n>>m)
  24. {
  25. if ((n==0)&&(m==0))
  26. break;
  27. for (int i=1;i<=n;i++)//init
  28. E[i].clear();
  29. string str;
  30. int x;
  31. for (int i=1;i<=n;i++)
  32. {
  33. cin>>str;
  34. char ch=getchar();
  35. while (ch!='\n')//读入,一定要注意啊
  36. {
  37. cin>>x;
  38. E[i].push_back(x+1);
  39. ch=getchar();
  40. }
  41. }
  42. /*for (int i=1;i<=n;i++)
  43. {
  44. for (int j=0;j<E[i].size();j++)
  45. cout<<E[i][j]<<' ';
  46. cout<<endl;
  47. }
  48. cout<<endl;*/
  49. int l=0,r=n;
  50. int Ans=0;
  51. do//二分答案
  52. {
  53. //cout<<l<<' '<<r<<endl;
  54. int mid=(l+r)/2;
  55. if (check(mid))
  56. {
  57. r=mid;
  58. Ans=mid;
  59. }
  60. else
  61. {
  62. l=mid+1;
  63. }
  64. }
  65. while (l<r);
  66. cout<<l<<endl;//最后输出
  67. }
  68. return 0;
  69. }
  70. bool check(int mid)
  71. {
  72. MaxCnt=mid;
  73. memset(Num,0,sizeof(Num));
  74. memset(Match,-1,sizeof(Match));
  75. for (int i=1;i<=n;i++)
  76. {
  77. memset(vis,0,sizeof(vis));
  78. if (!Hungary(i))//因为必须所有人都匹配,所以若有一个人不能则匹配失败
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. bool Hungary(int u)
  84. {
  85. for (int i=0;i<E[u].size();i++)
  86. {
  87. int v=E[u][i];
  88. if (vis[v]==0)
  89. {
  90. vis[v]=1;
  91. if (Num[v]<MaxCnt)//首先判断能否直接加入
  92. {
  93. Num[v]++;
  94. Match[v][Num[v]]=u;
  95. return 1;
  96. }
  97. for (int j=1;j<=Num[v];j++)//否则依次将v之前匹配的人进行“让”操作,看能否让出来给u
  98. {
  99. if (Hungary(Match[v][j]))
  100. {
  101. Match[v][j]=u;
  102. return 1;
  103. }
  104. }
  105. }
  106. }
  107. return 0;
  108. }

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 (二分,二分图匹配)的更多相关文章

  1. BZOJ_4443_[Scoi2015]小凸玩矩阵_二分+二分图匹配

    BZOJ_4443_[Scoi2015]小凸玩矩阵_二分+二分图匹配 Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个 ...

  2. zoj 3460 二分+二分图匹配

    不错的思想 /* 大致题意: 用n个导弹发射塔攻击m个目标.每个发射架在某个时刻只能为 一颗导弹服务,发射一颗导弹需要准备t1的时间,一颗导弹从发 射到击中目标的时间与目标到发射架的距离有关.每颗导弹 ...

  3. 【洛谷P4251】[SCOI2015]小凸玩矩阵(二分+二分图匹配)

    洛谷 题意: 给出一个\(n*m\)的矩阵\(A\).现要从中选出\(n\)个数,任意两个数不能在同一行或者同一列. 现在问选出的\(n\)个数中第\(k\)大的数的最小值是多少. 思路: 显然二分一 ...

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

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

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

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

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

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

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

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

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

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

  9. POJ2289:Jamie's Contact Groups(二分+二分图多重匹配)

    Jamie's Contact Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/ ...

随机推荐

  1. 容器扩展属性 IExtenderProvider 实现WinForm通用数据验证组件

    大家对如下的Tip组件使用应该不陌生,要想让窗体上的控件使用ToolTip功能,只需要拖动一个ToolTip组件到窗口,所有的控件就可以使用该功能,做信息提示. 本博文要记录的,就是通过容器扩展属性 ...

  2. 红包项目总结---MVC版

    起因: 针对传统版的明显缺陷做优化.主要是提升可维护性. 效果  线上:  未发布 线下:http://10.27.5.1/svn/FED/code/hongbao/year-end   hb-fac ...

  3. 判断网站URL是否正常访问脚本

    #!/bin/bash [ -f /etc/init.d/functions ] && . /etc/init.d/functions function usage(){ echo & ...

  4. nodejs零基础详细教程1:安装+基础概念

    第一章 建议学习时间2小时  课程共10章 学习方式:详细阅读,并手动实现相关代码 学习目标:此教程将教会大家 安装Node.搭建服务器.express.mysql.mongodb.编写后台业务逻辑. ...

  5. [UWP]用Shape做动画

    相对于WPF/Silverlight,UWP的动画系统可以说有大幅提高,不过本文无意深入讨论这些动画API,本文将介绍使用Shape做一些进度.等待方面的动画,除此之外也会介绍一些相关技巧. 1. 使 ...

  6. (转) Unicode(UTF-8, UTF-16)令人混淆的概念

    原文地址:http://www.cnblogs.com/kingcat/archive/2012/10/16/2726334.html 为啥需要Unicode 我们知道计算机其实挺笨的,它只认识010 ...

  7. python socketserver监听多端口多进程

    多进程监听多端口 # 多线程socket # 程序监听两个端口,端口逻辑相同其中一个端口放在子进程下 # 每次请求会在产生一个进程处理请求 import SocketServer from multi ...

  8. 手机wap网站建设的方法和技巧

    随着互联网技术的不断进步,越来越多的运营商对于手机wap网站的建设有了更多的投入,手机wap网站的建设和开发要根据网站的特点和经营范围来进行设计和建设,这样才可以提升手机wap网站建设的效果.现在智能 ...

  9. 【原】vue单文件组件互相通讯

    在vue中,我们可以把一个页面各个部分单独封装起来,做成各种小组件,然后加载,这样的好处是代码维护起来比较容易,重复的代码多处调用! 在一个vue项目中,单文件组件以.vue形式文件命名 每个组件之间 ...

  10. [转]Java多线程学习(吐血超详细总结)

    转自:http://www.mamicode.com/info-detail-517008.html 本文主要讲了Java中多线程的使用方法.线程同步.线程数据传递.线程状态及相应的一些线程函数用法. ...