POJ-2289(二分答案+多重匹配)

题目:POJ - 2289 

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
扫描二维码关注公众号,回复: 5080444 查看本文章

题意:联系人和分组进行匹配,求最大的组的最少人数。

分析:见到最大求最小,我们用二分答案,之后进行二分答案,枚举每一个可能的值来检验,能不能达到要求。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

const int maxn = 1010;
int mapp[maxn][maxn];
int n,m;
bool used[maxn];
struct node
{
    int cnt; //y节点的匹配的个数
    int k[maxn];//和y匹配的x的集合数
}linker[maxn];

bool dfs(int u,int limit)
{
    for(int i = 1;i <= m;i ++)
    {
        if(!used[i] && mapp[u][i])
        {
            used[i] = 1;
            if(linker[i].cnt < limit)//如果当前y节点还有空间可以匹配
            {
                linker[i].k[linker[i].cnt++] = u;
                return true;
            }
            for(int j = 0;j < linker[i].cnt;j ++)//如果y节点的容量已经满了,尝试为y节点的某个对象换对象
            {
                if(dfs(linker[i].k[j],limit))
                {
                    linker[i].k[j] = u;//y节点的第j个位置让给x
                    return true;
                }
            }
        }
    }
    return false;
}

bool hungary(int limit)
{
    memset(linker,0,sizeof(linker));
    for(int i = 1;i <= n;i ++)//为所有的x节点匹配对象
    {
        memset(used,false,sizeof(used));
        if(!dfs(i,limit))
            return false;
    }
    return true;
}

int main()
{
    int x;
    char s[20],ch;
    while(scanf("%d %d",&n,&m) == 2 && ( n + m ))
    {
        memset(mapp,0,sizeof(mapp));
        for(int i = 1;i <= n;i ++)
        {
            scanf("%s",s);
            while(1)
            {
                scanf("%d%c",&x,&ch);
                mapp[i][x + 1] = 1;
                if(ch == '\n')
                break;
            }
        }
        int L = 1,R = n;
        int ans = n;
        while(L <= R)
        {
            int mid = ( L + R ) / 2;
            if(hungary(mid))
            {
                R = mid - 1;
                ans = mid;
            }
            else L = mid + 1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/guagua_de_xiaohai/article/details/86656393