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

Jamie's Contact Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)

题目链接http://poj.org/problem?id=2289

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

题意:

有n个人,m个朋友圈,每个人都可以被分到对应的朋友圈,问怎么分能使朋友圈里面人数最大的最小。

 

题解:

每个人可以对应一个朋友圈,但是一个朋友圈不一定只接纳一个朋友,所以这是二分图的多重匹配。

二分图的多重匹配其实就是对二分图匹配的匈牙利算法做了个改进,以前增广的是匹配边,现在增广的是“匹配点”,如果发现目前这个朋友圈满员了,就看看能不能把这个朋友圈里面的人通过增广路分到其它朋友圈里面去。可以通过代码体会一下。

另外这里求的是最大值最小,显然二分答案。

 

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mem(x) memset(x,0,sizeof(x))
using namespace std;

const int N = 1005 ,M = 505;
int n,m,ans,mid;
int vy[N],linky[M][N],link[N][M],check[N];

inline int dfs(int x){
    for(int i=0;i<m;i++){
        if(link[x][i] && !check[i]){
            check[i]=1;
            if(vy[i]<mid){
                linky[i][++vy[i]]=x;
                return 1;
            }else{
                for(int j=1;j<=vy[i];j++){
                    int now = linky[i][j];
                    if(dfs(now)){
                        linky[i][j]=x;
                        return 1;
                    }
                }
            }
        }
    }
    return 0;
}

inline int Check(int x){
    mem(linky);mem(vy);
    for(int i=1;i<=n;i++){
        mem(check);
        if(!dfs(i)) return 0;
    }
    return 1;
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        if(!n && !m) break;
        mem(link);ans=0;
        for(int i=1;i<=n;i++){
            char s[20];
            scanf("%s",s);
            while(true){
                int x;char tmp;
                scanf("%d%c",&x,&tmp);
                link[i][x]=1;
                if(tmp=='\n') break ;
            }
        }
        int l=1,r=1001,Ans=0x3f3f3f;
        while(l<=r){
            mid=l+r>>1;
            if(Check(mid)){
                r=mid-1;
                Ans=mid;
            }else l=mid+1;
        }
        printf("%d\n",Ans);
    }
    return 0;
}

 

posted @ 2018-11-12 21:50  heyuhhh  阅读(246)  评论(0编辑  收藏  举报