poj2289 Jamie's Contact Groups(二部图多重匹配)

来源:互联网 发布:万年历软件下载 编辑:程序博客网 时间:2024/04/29 08:57

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<stdio.h>#include<string.h>#include<vector>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;const int N=1005;const int M=505;int n,m,limit,ans;int match[M];//match[i] i当前的匹配数int link[M][N];//与i匹配的第j个匹配的数kvector<int>g[N];bool vis[M];bool find(int u){    for(int i=0; i<g[u].size(); i++)    {        int v=g[u][i];        if(!vis[v])        {            vis[v]=1;            if(match[v]<limit)//没有达到上限            {                link[v][match[v]]=u;                match[v]++;                return true;            }            for(int j=0; j<match[v]; j++)//达到上限,寻找是否存在增广路            {                if(find(link[v][j]))                {                    link[v][j]=u;                    return true;                }            }        }    }    return false;}bool matching(){    mem(match,0);    mem(link,0);    for(int i=0; i<n; i++)    {        mem(vis,0);        if(!find(i))return false;    }    return true;}int main(){    char name[25];    int x;    while(scanf("%d%d",&n,&m)&&n+m)    {        for(int i=0; i<n; i++)g[i].clear();        for(int i=0; i<n; i++)        {            scanf("%s",name);            while(getchar()!='\n')            {                scanf("%d",&x);                g[i].push_back(x);            }        }        int l=0,r=n;        while(l<=r)        {            limit=(l+r)>>1;            if(matching())            {                ans=limit;                r=limit-1;            }            else                l=limit+1;        }        printf("%d\n",ans);    }    return 0;}
原创粉丝点击