1. 程式人生 > >POJ2289:Jamie's Contact Groups(二分+二分圖多重匹配)

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

desc pac nta shang ems others ase sample 最小

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;
}

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