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

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组里,求每组最小的人数上限。
这个题的话可以采用枚举人数上限来求解。这样问题转换为 如何判断人数上限为k时,是否存在解。
如果在人和分组之间建边,不难发现每个组可以匹配很多个人,也就是相当于在求最大匹配时,将match数组表示的含义,从一个确切的点,变成了一个集合。
这样的改动会给匹配时添加匹配造成如下影响,

  1. 当一个点的匹配数未达到匹配限制
    在该点处添加一个新的匹配
  2. 当一个点的匹配数达到匹配限制但想要添加新的匹配
    查找这个点对应的匹配集合,在集合里的每个点中查找是否存在增广路,如果有,拆分原匹配,添加新匹配;如果没有,那么这个匹配关系不能建立

#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define MAXN 1005
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return

#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;

int n,m;
bool used[MAXN];
vector<int> match[MAXN];
vector<int> adj[MAXN];

bool judge(int num);
//group consisting not more than num
//is exist return true;

bool dfs(int v,int lim);
int main(){
    ios::sync_with_stdio(false);

    while(cin>>n>>m,n+m){
        char c;int belong;
        string name;
        rep(i,0,n){
            cin>>name;
            while(cin>>belong){
                adj[i].Push(belong);
                cin.get(c);
                if(c=='\n')
                    break;
            }
        }

        int inf=0,sup=n;
        while(inf+1<sup){
            int mid=inf+(sup-inf)/2;

            if(judge(mid))
                sup=mid;
            else
                inf=mid;
        }
        cout<<sup<<endl;

        rep(i,0,n){
            adj[i].clear();
        }
    }
    re 0;
}
bool judge(int num){
    int ans=0;
    rep(i,0,n)
        match[i].clear();

    rep(i,0,n){
        memset(used,false,sizeof(used));
        if(!dfs(i,num))
            return false;
    }

    return true;
}
bool dfs(int v,int lim){
    rep(i,0,adj[v].size()){
        int u=adj[v][i];
        if(!used[u]){
            used[u]=true;
            int len=match[u].size();
            if(len>=lim){
                rep(j,0,len){
                    int matched=match[u][j];
                    if(dfs(matched,lim)){
                        match[u][j]=v;
                        return true;
                    }
                }
            }else{
                match[u].Push(v);
                return true;
            }
        }
    }
    return false;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值