Poj 2289 Jamie's Contact Groups【二分+多重匹配】

Jamie's Contact Groups

Time Limit: 7000MS

 

Memory Limit: 65536K

Total Submissions: 7393

 

Accepted: 2473

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

Source

Shanghai 2004

 

题目大意:有n个人,将被分成m组,每个人都有一些个能去的组,问最后分配出的集合中,最大规模的那个组最少有多少人。


思路:


1、经典模型多重匹配问题,相关网络流解法:http://blog.csdn.net/mengxiang000000/article/details/52292975


2、相关多重匹配问题,其实就是将匈牙利最大匹配算法从一维匹配变成了二维匹配,将match【i】变成了match【i】【j】,表示第i个组里边,第j个匹配的人的编号。

二分查找一个当前解mid,对于每个组设定一个num【i】数组,表示第i个组现在匹配了多少个人,在匹配过程中,如果有一个人不能继续得到匹配,那么当前mid值就是一个不合法的情况。如果所有人都能找到相应的组别进行匹配,那么当前mid值就是一个可行解。


3、一直二分下去,更新最后的可行解,直到不能二分为止,输出最后一个可行解。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int match[1100][1100];
int num[1100];
int vis[1100];
char name[5000];
vector<int >mp[5000];
int n,m;
int find(int u,int mid)
{
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            if(num[v]>=mid)
            {
                for(int j=0;j<num[v];j++)
                {
                    if(find(match[v][j],mid)==1)
                    {
                        match[v][j]=u;
                        return 1;
                    }
                }
            }
            else
            {
                match[v][num[v]]=u;
                num[v]++;
                return 1;
            }
        }
    }
    return 0;
}
int Slove(int mid)
{
    memset(num,0,sizeof(num));
    memset(match,-1,sizeof(match));
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(find(i,mid)==0)return 0;
    }
    return 1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",name);
            while(1)
            {
                int v;
                scanf("%d",&v);
                mp[i].push_back(v+1);
                if ( getchar() == '\n' ) break;
            }
        }
        int mid;
        int ans=0;
        int l=0;
        int r=100000;
        while(r>=l)
        {
            mid=(l+r)/2;
            if(Slove(mid)==1)
            {
                ans=mid;
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }
        printf("%d\n",ans);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值