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.
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.
For each test case, output a line containing a single integer, the size of the largest contact group.
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
2 2
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
二分法,二分图最大匹配
现在有n个人,有m个小组,每个人都有若干个小组供选择进入,但一个人最多只能进一个小组。现在求最大小组人数的最小值。
我在前面的文章中也说过,这种求最大小组人数最小值的方法就是二分答案。
每次二分最大小组人数,然后判断能否找到一种方案使得所有小组的人数都不超过这个值。
至于怎么判断,那我们就是用二分图最大匹配中的匈牙利算法。
关于匈牙利算法的基本内容,这里不再多说,可以参考我的这三篇文章。http://www.cnblogs.com/SYCstudio/p/7138206.htmlhttp://www.cnblogs.com/SYCstudio/p/7138221.htmlhttp://www.cnblogs.com/SYCstudio/p/7138230.html
最后要注意的是,因为这里的一个组内可以有多个人,所以组的Match不是唯一的,那么我们设Match[i][j],表示i组第当前匹配的第j个人,设Num[i]表示第i组的人数。那么匈牙利算法也要有一些变动,具体请看代码。
转载于:https://www.cnblogs.com/SYCstudio/p/7138477.html
相关资源:JAVA上百实例源码以及开源项目