Jamie's Contact Groups
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 8473   Accepted: 2875

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

Source

题意:
  N个人,M个组,给出每个人可以考虑选择的组号,要求每个人都必须选且只能选一个组,求一个划分方案使得最多人数的组的人数 是所有方案中人数最少的,结果输出这个最小值。

题解:

  一对多的二分图多重匹配问题。网络流建图:源点到每个人连边,边权为1;每个人到可以考虑选择的组连边,边权为1;每个组到汇点连边,边权为求解的最小值。这个最小值很明显可以想到二分求解。

代码:

 #include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N = ;
const int M = 1e6;
const int inf = 1e9;
int n, m, S, T;
int dep[N], cur[N];
int head[N];
struct Edge{
int v, c, nex;
Edge(int _v=,int _c=,int _nex=):v(_v),c(_c),nex(_nex){}
}E[M]; int cnt;
void add(int u, int v, int c){
E[cnt].v = v;
E[cnt].c = c;
E[cnt].nex = head[u];
head[u] = cnt++;
} bool bfs() {
queue<int> q;
memset(dep, -, sizeof(dep));
q.push(S); dep[S] = ;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = E[i].nex) {
int v = E[i].v;
if(E[i].c && dep[v] == -) {
dep[v] = dep[u] + ;
q.push(v);
}
}
}
return dep[T] != -;
}
int dfs(int u, int flow) {
if(u == T) return flow;
int w, used=;
for(int i = head[u]; ~i; i = E[i].nex) {
int v = E[i].v;
if(dep[v] == dep[u] + ) {
w = flow - used;
w = dfs(v, min(w, E[i].c));
E[i].c -= w; E[i^].c += w;
if(v) cur[u] = i;
used += w;
if(used == flow) return flow;
}
}
if(!used) dep[u] = -;
return used;
}
int dinic() {
int ans = ;
while(bfs()) {
for(int i = ; i <= T;i++)
cur[i] = head[i];
ans += dfs(S, inf);
}
return ans;
}
int main() {
char s[];
int i, j, x, t;
while(~scanf("%d%d", &n, &m),n+m) {
memset(head, -, sizeof(head));
cnt = ;
S = n+m+; T = n+m+;
vector<int>g[N];
for(i = ; i <= n; ++i) g[i].clear();
for(i = ; i < n; ++i) {
scanf("%s", s);
while(getchar() != '\n') {
scanf("%d", &x);
g[i].push_back(x);
}
}
int l = , r = n, mid;
while(l <= r) {
memset(head, -, sizeof(head));
cnt = ;
mid = (l+r)/;
for(i = ; i < n; ++i) {
for(j = ; j < g[i].size(); ++j)
add(i, n+g[i][j], ),add(n+g[i][j], i, );
}
for(i = ; i < n; ++i) add(S,i,), add(i,S,);
for(i = n; i < n+m; ++i) add(i,T,mid),add(T,i,);
t = dinic();
if(t == n) r = mid - ;
else l = mid + ;
}
printf("%d\n", l);
}
}

485ms

poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】的更多相关文章

  1. POJ 2289 Jamie's Contact Groups (二分+最大流)

    题目大意: 有n个人,可以分成m个组,现在给出你每个人可以去的组的编号,求分成的m组中人数最多的组最少可以有多少人. 算法讨论: 首先喷一下这题的输入,太恶心了. 然后说算法:最多的最少,二分的字眼. ...

  2. Poj 2289 Jamie's Contact Groups (二分+二分图多重匹配)

    题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...

  3. POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)

    POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...

  4. POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】

    Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  5. POJ 2289 Jamie's Contact Groups 二分图多重匹配 难度:1

    Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: ...

  6. POJ 2289 Jamie's Contact Groups(多重匹配+二分)

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

  7. POJ 2289 Jamie's Contact Groups 【二分】+【多重匹配】(模板题)

    <题目链接> 题目大意: 有n个人,每个人都有一个或者几个能够归属的分类,将这些人分类到他们能够归属的分类中后,使所含人数最多的分类值最小,求出该分类的所含人数值. 解题分析: 看到求最大 ...

  8. POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment

    这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...

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

    题意:N个人,M个团体.每个人有属于自己的一些团体编号.将每个人分配到自己属于的团体中,问这个人数最多的团体其人数最小值是多少. 分析:一个一对多的二分图匹配,且是最大值最小化问题.二分图的多重匹配建 ...

随机推荐

  1. CEF加载FLASH插件时弹出CMD命令行窗口的问题

    这个是flash插件的一个bug,CEF(chromium系列浏览器)关闭sandbox第一次加载flash插件就会跳出这样的一个提示,在Google官方也看到了chromium的issue: 解决方 ...

  2. Hive可视化工具

    目前市面上的Hive可视化客户端工具,大都是C/S模式的,安装使用都不是太方便,目前有一款基于WEB的可视化工具TreeSoft,通过浏览器就可以访问使用了,并且可以同时管理.维护.监控MySQL,O ...

  3. Mybatis源代码分析之parsing包

    parsing,从字面上理解就是编译解析的意思,那么这个包中的内容就应该和mybatis配置文件的编译解析有关系.本文首先会按照引用层次来分别介绍这个包中各个类的作用,而后再用实际的例子解释它们是如何 ...

  4. DDD Quickly - 读书笔记

    读后感:关于领域驱动设计,过去多多少少用到一些.所以,这本精简版看起来很快,很多概念很熟悉,它帮助我把散乱的知识串起来.最后,Eric Evans谈到一点,本来软件的发展是向着处理复杂的业务逻辑走的, ...

  5. 关于Google圆角高光高宽自适应按钮及其拓展

    关于Google圆角高光高宽自适应按钮及其拓展————源自张鑫旭css讲解 这篇文章发布于 2009年10月24日,星期六,18:08,归类于 css相关. 阅读 48770 次, 今日 1 次 by ...

  6. [清华集训]Rmq Problem / mex

    题目链接 我们离线处理这些询问 在右端点所在的位置用vector来push_back询问 维护每个数值最后出现的位置p[x] 从左往右扫,边走边回答询问 对于每个询问我们回答第一个p[x]<l的 ...

  7. Hackerrank GCD Product(莫比乌斯反演)

    题意 题目链接 Sol 一道咕咕咕了好长时间的题 题解可以看这里 #include<bits/stdc++.h> #define LL long long using namespace ...

  8. pipenv虚拟环境和依赖管理工具

    一.pipenv用来干嘛 每门编程语言发展到现在,都需要一个工具,能够管理代码版本和控制生产环境和测试环境依赖一致的,这样减少不可代码上线之后不可控的问题出现.Php有Composer.Nodejs有 ...

  9. egg.js连接和使用Mongodb

    一.Egg连接Mongodb方法一   Cnpm i egg-momgo-native --save Plugin.js中配置 exports.mongo = { enable: true, pack ...

  10. HiveSql调优经验

    背景 在刚使用hive的过程中,碰到过很多问题,任务经常需要运行7,8个小时甚至更久,在此记录一下这个过程中,我的一些收获 join长尾 背景 SQL在Join执行阶段会将Join Key相同的数据分 ...