HDU 1669 二分+多重匹配

Jamie's Contact Groups

Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 403    Accepted Submission(s): 124


Problem 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
 

Recommend
xhd   |   We have carefully selected several similar problems for you:   1661  1663  1667  1662  1664 
 


二分+多重匹配。。。


代码:

//二分多重匹配,复杂度
#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll;   //记得必要的时候改成无符号
const int maxn=100005;   //点数
const int maxm=2000005;   //边数
struct EdgeNode
{
    int to;
    int next;
}edge[maxm];
int head[maxn],cnt;
void add(int x,int y)
{
    edge[cnt].to=y;edge[cnt].next=head[x];head[x]=cnt++;
}

struct q
{
    int pre[maxn],lim,num;
}a[505];

int n,m,v[505];       //v[]标记的是右边的集合

void init()
{
    cnt=1;
    memset(head,-1,sizeof(head));
}

bool dfs(int x)
{
    int y;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        y=edge[i].to;
        if(!v[y])
        {
            v[y]=1;
            if(a[y].num<a[y].lim)
            {
                a[y].pre[++a[y].num]=x;
                return true;
            }
            for(int j=1;j<=a[y].lim;j++)
            {
                if(dfs(a[y].pre[j]))
                {
                    a[y].pre[j]=x;
                    return true;
                }
            }
        }
    }
    return false;
}

bool Mulmatch(int num)
{
    for(int i=0;i<m;i++)
    {
        a[i].lim=num;
        a[i].num=0;
    }
    for(int i=0;i<n;i++)
    {
        memset(v,0,sizeof(v));
        if(!dfs(i))
            return false;
    }
    return true;
}

char s[maxn];

int main()
{
    int i,j,t,l,r,mid,chu;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        getchar(); init();
        memset(v,0,sizeof(v));
        for(i=0;i<n;i++){
            gets(s);
            int len=strlen(s);
            for(j=0;s[j]!=' ';j++); j++; t=0;
            for(;j<len;j++){
                if(s[j]==' '){
                    v[t]++;
                    add(i,t);
                    t=0;
                }
                else t=t*10+s[j]-'0';
            }
            v[t]++;
            add(i,t);
        }
        l=0; r=-1;
        for(i=0;i<m;i++)r=max(r,v[i]);
        while(l<=r){
            mid=(l+r)/2;
            if(Mulmatch(mid)){
                chu=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%d\n",chu);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值