Jamie's Contact Groups(二分图多重匹配+二分)(网络流)

Jamie‘s Contact Groups

Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice POJ
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

我的第一道二分图多重匹配题:

转一个不错的Blog:http://www.cnblogs.com/zhengguiping--9876/p/4728358.html

Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是多

少?

一对多的二分图的多重匹配。二分图的多重匹配算法的实现类似于匈牙利算法,对于集合x中的元素xi,找到一个与其相连的元素yi后,检查匈牙利算法的两个条件是否成立,若yi未被匹配,则将

xi,yi匹配。否则,如果与yi匹配的元素已经达到上限,那么在所有与yi匹配的元素中选择一个元素,检查是否能找到一条增广路径,如果能,则让出位置,让xi与yi匹配。

二分求出limit,知道找到可以构成多重匹配的最小限制limit,在main函数中二分搜索。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define N 1010
int vis[N], maps[N][N], ans, n, m;

struct node
{
    int cnt;///和yi相匹配的个数;
    int k[N];///和yi相匹配的x的集合;
}Linky[N];

bool Find(int u, int limit)
{
    for(int i=1; i<=m; i++)
    {
        if(!vis[i] && maps[u][i])
        {
            vis[i] = 1;
            if(Linky[i].cnt < limit)
            {
                Linky[i].k[ Linky[i].cnt++ ] = u;
                return true;
            }
            for(int j=0; j<Linky[i].cnt; j++)
            {
                if(Find( Linky[i].k[j], limit ))
                {
                    Linky[i].k[j] = u;
                    return true;
                }
            }
        }
    }
    return false;
}

bool hungary(int limit)///匈牙利算法;
{
    memset(Linky, 0, sizeof(Linky));
    for(int i=1; i<=n; i++)
    {
        memset(vis, 0, sizeof(vis));
        if(!Find(i, limit))///当前的limit让i没有匹配,所以不能用limit;
            return false;
    }
    return true;
}

int main()
{
    int x;
    char s[20], ch;
    while(scanf("%d %d", &n, &m), m+n)
    {
        memset(maps, 0, sizeof(maps));
        for(int i=1; i<=n; i++)
        {
            scanf("%s", s);
            while(1)
            {
                scanf("%d%c", &x, &ch);
                maps[i][x+1] = 1;
                if(ch == '\n')
                    break;
            }
        }
        int L = 1, R = n;
        ans = n;
        while(L <= R)
        {
            int mid = (L+R)/2;
            if(hungary(mid))///如果当前mid满足题意;
            {
                R = mid-1;
                ans = mid;
            }
            else
                L = mid+1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

Jamie's Contact Groups(二分图多重匹配+二分)(网络流)

时间: 2025-01-05 14:12:10

Jamie's Contact Groups(二分图多重匹配+二分)(网络流)的相关文章

POJ2289 Jamie&#39;s Contact Groups —— 二分图多重匹配/最大流 + 二分

题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 8147   Accepted: 2736 Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very lon

POJ 2289 Jamie&#39;s Contact Groups 二分图多重匹配

题意:n个人,m个组,给出每个人可以分到那些组中,把每个人都分进某组中,问最大组的人数最小为?n<=1e3,m<=500,二分图多重匹配 左边为人 右边为分组 可以多个人对一个组由于要求最大组的最小人数 二分答案x,右边点最多流量为x,判断匹配数是否n即可. #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; t

POJ 2289 Jamie&#39;s Contact Groups(多重匹配+二分)

题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是多少? 题目分析: 多重匹配,二分枚举所有极限值. 多重匹配如何匹配? 假如我们有两个集合X, Y 但是呢 Y可以匹配多个X, 这个时候我们需要给这个匹配设置一个极限值.比如Y可以匹配三个X. 假如匹配的值不到三个X我们就将他们匹配, 直到到达极限值为止.在这里Y要保存所有的与之匹配的X,若是匹配值

POJ--2289--Jamie&#39;s Contact Groups【二分图多重匹配+二分答案】

链接:http://poj.org/problem?id=2289 题意:有n个人,m个分组,每个人可以分配到一些组别,问如何分能使得人数最多的组别人数最少. 思路:这道题二分+网络流也可以做,我这里是二分图多重匹配的做法.因为一个组别是一对多的关系,所以是多重匹配,我们二分多重匹配的限制,得到最小的限制可使二分图匹配,这个限制就是答案. 网上找的模板 #include<cstring> #include<string> #include<fstream> #inclu

Jamie&#39;s Contact Groups---hdu1669--poj2289(多重匹配)

题目链接 题意:Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是多 少? 一对多的二分图的多重匹配.二分图的多重匹配算法的实现类似于匈牙利算法,对于集合x中的元素xi,找到一个与其相连的元素yi后,检查匈牙利算法的两个条件是否成立,若yi未被匹配,则将 xi,yi匹配.否则,如果与yi匹配的元素已经达到上限,那么在所有与yi匹配的元素中选择一个元素,检查

uva 1345 Jamie&#39;s Contact Groups (最大流+二分)

uva 1345 Jamie's Contact Groups 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 br

Optimal Milking(二分图多重匹配+二分)(网络流)

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

UVALive-3268 Jamie&#39;s Contact Groups (最大流,网络流建模)

题目大意:你的手机通讯录里有n个联系人,m个分组,其中,有的联系人在多个分组里.你的任务是在一些分组里删除一些联系人,使得每个联系人只在一个分组里并且使人数最多的那个分组人数最少.找出人数最多的那个分组中的人数. 题目分析:要求的是最小的最大值,二分枚举这个最小的最大人数x.增加源点s和汇点t,从s向每一个联系人连一条弧,容量为1,表示一个联系人只能在一个分组中:然后对于每个联系人向他所在的分组连一条弧,容量为1,表示在这个分组里最多保存一次该联系人:然后从每个分组向汇点连一条弧,容量为x,表示

HDU 1669 二分图多重匹配+二分

Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 747    Accepted Submission(s): 303 Problem Description Jamie is a very popular girl and has quite a lot of friends, so she