欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 2289 Jamie's Contact Groups(二分图多重匹配 && 二分)

程序员文章站 2022-03-13 22:46:08
...

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


题意

Jamie手机通讯录有很多人,他想把这些联系人分组,已知各个好友可以分到哪些组。
求人数最多的那组最少有多少人。

思路

二分答案,以此为限制进行二分图多重匹配

代码
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;

vector<int> nxt[505];
int n, m, v, e[1005][505];
bool used[505];
char s[20], ch;

bool find(int u, int limit)
{
    for(int i = 0; i < m; ++i)
    {
        if(!used[i] && e[u][i])
        {
            used[i] = 1;
            if(nxt[i].size() < limit)
            {
                nxt[i].push_back(u);
                return 1;
            }
            for(int j = 0; j < nxt[i].size(); ++j)
            {
                if(find(nxt[i][j], limit))
                {
                    nxt[i][j] = u;
                    return 1;
                }
            }
        }
    }
    return 0;
}

bool match(int limit)
{
    for(int i = 0; i < m; ++i)nxt[i].clear();
    for(int i = 1; i <= n; ++i)
    {
        memset(used,0,sizeof(used));
        if(!find(i,limit)) return 0;
    }
    return 1;
}

int main()
{
    while(scanf("%d%d",&n,&m), n+m)
    {
        memset(e,0,sizeof(e));
        for(int i = 1; i <= n; ++i)
        {
            scanf("%s",s);
            while(1)
            {
                scanf("%d%c",&v,&ch);
                e[i][v] = 1;
                if(ch == '\n') break;
            }
        }

        int ans, l = 0, r = n, mid;
        while(l <= r)
        {
            mid = (l+r)/2;
            if(match(mid))
            {
                ans = mid;
                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
相关标签: 二分 二分图