POJ2289 Jamie's Contact Groups(二分图多重匹配)

26 篇文章 0 订阅
22 篇文章 0 订阅
Jamie's Contact Groups
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 7721 Accepted: 2599

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 

————————————————————————————————

题目意思是:一个人通讯录中好友有许多,然后需要分组,现在告诉你不同的的人能分进小组的编号,然后问你怎么分配是小组中人最多的人最少,输出最小值。

思路:二分最小值,然后二分图多重匹配验证

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
const int MAXN=1005;
int uN,vN;  //u,v数目
int g[MAXN][MAXN];
int linker[MAXN][MAXN];
bool used[MAXN];
int linknum[MAXN];
int cap[MAXN];
int vis[MAXN];
bool dfs(int u)
{
    int v;
    for(v=0; v<vN; v++)
        if(g[u][v]&&!used[v])
        {
            used[v]=true;
            if(linknum[v]<cap[v])
            {
                linker[v][linknum[v]++]=u;
                return true;
            }
            for(int i=0; i<cap[v]; i++)
                if(dfs(linker[v][i]))
                {
                    linker[v][i]=u;
                    return true;
                }
        }
    return false;
}

int hungary()
{
    int res=0;
    int u;
    memset(linknum,0,sizeof linknum);
    memset(linker,-1,sizeof linker);
    for(u=0; u<uN; u++)
    {
        memset(used,0,sizeof used);
        if(dfs(u))  res++;
    }
    return res;
}

bool ok(int mid)
{
    for(int i=0; i<vN; i++)
        cap[i]=mid;
    if(hungary()<uN)
        return 0;
    return 1;
}

int main()
{
    int n,m,x;
    char s[100];
    while(~scanf("%d%d",&n,&m)&&(m||n))
    {
        memset(g,0,sizeof g);
        for(int i=0; i<n; i++)
        {
            scanf("%s",s);
            while(getchar()!='\n')
            {
                scanf("%d",&x);
                g[i][x]=1;
            }
        }
        uN=n,vN=m;
        int l=1,r=n;
        int ans=0;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(ok(mid)) r=mid-1,ans=mid;
            else l=mid+1;
        }
        printf("%d\n",ans);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值