HDU - 1669 Jamie's Contact Groups

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669

题面:

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个分组,接下来n行给定每个联系人的名字及可以放在的分组编号,要求将每个联系人放在某一个分组中,一个分组可以放多个联系人,要求最大分组的人数最小化,并输出这个最小值。

分析及思路:

典型的二分图多重匹配问题,注意每个联系人只需要知道编号就行,不需要名字,建图后跑二分图最大匹配即可,由于要求最大值的最小化,可以用二分答案的方法优化时间复杂度。
AC代码:

#include<iostream>
#include<sstream>
#include<cstring>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define per(i,x,n) for(int i=n-1;i>=x;i--)
using namespace std;
//head
const int maxn=1006;
int n,m,nx,ny,r,l,limit,vis[maxn],mp[maxn][maxn],cy[maxn][maxn],vcy[maxn];
bool findpath(int x)
{
    rep(i,0,m)
    {
        if(mp[x][i]&&vis[i]==0)
      {

        vis[i]=1;
        if(vcy[i]<limit)
        {
            cy[i][vcy[i]++]=x;
            return 1;
        }
        rep(j,0,vcy[i])
        {
            if(findpath(cy[i][j]))
            {
                cy[i][j]=x;
                return 1;
            }
        }
    }
    }
    return 0;
}
bool match()
{
     memset(vcy,0,sizeof(vcy));
     rep(i,0,n)
     {
         memset(vis,0,sizeof(vis));
         if(!findpath(i))return 0;
     }
     return 1;
}
int main()
{
     char str[20];int tmp;char q;
     while(scanf("%d%d",&n,&m),m+n)
     {
         memset(mp,0,sizeof(mp));
         if(n==0&&m==0)break;
         rep(i,0,n)
         {
            scanf("%s",str);
            while(1)
            {
                scanf("%d%c",&tmp,&q);
                mp[i][tmp]=1;
                if(q == '\n')
                    break;
            }
         }
     l=1,r=n;
     while(l<r)
     {
         limit=(l+r)/2;
         if(match())r=limit;
         else l=limit+1;
     }
     printf("%d\n",r);
     }
     return 0;
}

版权声明:本文为原创文章,转载请标明出处。
https://blog.csdn.net/u014390156

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值