二分图多重匹配--poj2289

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

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


题意:给定一个规模为n的名单,要将名单中的人归到m个组中,给出每个人可能的分组号,需要确定一种分配方案,是的最大规模的组最小。
思路:一对多的二分图的多重匹配。二分图的多重匹配算法的实现类似于匈牙利算法,对于集合C中的元素xi,找到一个与其相连的元素yi后,检查匈牙利算法的两个条件是否成立,若yi未被匹配,则将
xi,yi匹配。否则,如果与yi匹配的元素已经达到上限,那么在所有与yi匹配的元素中选择一个元素,检查是否能找到一条增广路径,如果能,则让出位置,让xi与yi匹配。
二分求出limit,知道找到可以构成多重匹配的最小限制limit,在main函数中二分搜索。

首先说一下,最大值最小化问题(以前没遇到过),把优化问题转化成判定枚举的值是否成立的问题,猜一个数字,然后验证是否正确,一般用二分查找(一个最大值最小化的问题,自然我们又想到二分枚举+网络流判定这样的做法)。

然后是二分图的多重匹配。还可以用网络流来做,目前还不会,先学习一下这个写法。

#include
#include
#include
#include
using namespace std;
int con[2010][2010];
int vis[2010];
int pre[2010][2010];
int amount[2010];
int mid;
int n,m;
bool dfs(int i)
{
    for(int j=0; j>n>>m,n&&m)
    {
        getchar();
        memset(con,0,sizeof(con));
        for(int i=0; i='0'&&a[j]<='9')
                {
                    int v=0;
                    while(a[j]>='0'&&a[j]<='9')
                    {
                        v=10*v+a[j]-'0';
                        j++;
                    }
                    //cout<>1;
            if(mult())
                r=mid;
            else
                l=mid+1;
        }
        cout<



你可能感兴趣的:(二分图)