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

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

原文地址:https://www.cnblogs.com/heyuhhh/p/9949354.html

时间: 2024-11-05 18:58:57

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

POJ2289 Jamie&#39;s Contact Groups(二分图多重匹配)

题意: 给定一个规模为n的名单,要将名单中的人归到m个组中,给出每个人可能的分组号,需要确定一种分配方案,使得最大规模的组最小. 思路: 二分图多重匹配 如果所到的组没满,就去那个组 如果满了,就从那个组里踢出一个 如果能踢出来,就把那个踢出来,把当前的放进去 如果所有能到的组都踢不出来,就不对了 至于那个最大规模的具体值,二分一下就OK了 /* *********************************************** Author :devil Created Time

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

题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多少人? 解题思路: 二分图多重匹配相对于二分匹配来说不再是节点间的一一对应,而是Xi可以对应多个Yi.所以我们就需要一个限制(Xi最多匹配几个Yi).当Yi需要匹配Xi的时候,Xi的匹配未到上限,直接匹配,否则进行增广路.其实是二分图多重匹配的模板题,再套一个二分枚举最多组的人数就OK咯.下面就上板

Jamie&#39;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

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

题意:N个人,M个团体.每个人有属于自己的一些团体编号.将每个人分配到自己属于的团体中,问这个人数最多的团体其人数最小值是多少. 分析:一个一对多的二分图匹配,且是最大值最小化问题.二分图的多重匹配建立在匈牙利算法的基础上,令每个Y部的点可匹配多个点,但是规定其上限,超过上限就要在已有的匹配点中寻找增广路.对于X部的点,只要有一个点没有被匹配,那么算法失败.以此二分确定答案,注意二分的姿势... 该题可做模板. #include<iostream> #include<stdio.h>

POJ 2289--Jamie&#39;s Contact Groups【二分图多重匹配问题 &amp;&amp;二分查找最大值的最小化 &amp;&amp; 最大流】

Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 6902   Accepted: 2261 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 con

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

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

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

解题报告 之 POJ2289 Jamie&#39;s Contact Groups

解题报告 之 POJ2289 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

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

题目大意: 有n个人,可以分成m个组,现在给出你每个人可以去的组的编号,求分成的m组中人数最多的组最少可以有多少人. 算法讨论: 首先喷一下这题的输入,太恶心了. 然后说算法:最多的最少,二分的字眼.二分什么,因为我们说的是组的人,所以要对组的流出量进行二分.其余的都连流量为1的边,然后对“小组”点的流出量二分连边,最后跑最大流判断 是否等于N即可.还是蛮简单的. Codes: 1 #include <cstdio> 2 #include <cstring> 3 #include