题目链接: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 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

题解:

题意:jamie的QQ有n个联系人,且设置了m个分组,规定了哪些朋友可以去哪些分组。为了能够快速地找到朋友,jamie希望人数最多的分组的人数最少(最大值最小),并且满足每个朋友仅存在于一个分组中。

1.二分最大值,即每个分组的容量。

2.利用二分图多重匹配,或者最大流,求出是否所有人都可以归到一个分组中。如果可以,则减小容量,否则增大容量。

多重匹配:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXM = 5e2+;
const int MAXN = 1e3+; int uN, vN;
int num[MAXM], linker[MAXM][MAXN];
bool g[MAXN][MAXM], used[MAXM]; bool dfs(int u)
{
for(int v = ; v<vN; v++)
if(g[u][v] && !used[v])
{
used[v] = true;
if(linker[v][]<num[v])
{
linker[v][++linker[v][]] = u;
return true;
}
for(int i = ; i<=num[v]; i++)
if(dfs(linker[v][i]))
{
linker[v][i] = u;
return true;
}
}
return false;
} bool hungary(int mid)
{
for(int i = ; i<vN; i++)
{
num[i] = mid;
linker[i][] = ;
}
for(int u = ; u<uN; u++)
{
memset(used, false, sizeof(used));
if(!dfs(u)) return false;
}
return true;
} char tmp[];
int main()
{
while(scanf("%d%d", &uN, &vN) && (uN||vN))
{
memset(g, false, sizeof(g));
getchar();
for(int i = ; i<uN; i++)
{
gets(tmp);
int j = , len = strlen(tmp);
while(tmp[j]!=' ' && j<len) j++;
j++;
for(int v = ; j<=len; j++)
{
if(tmp[j]==' '||j==len)
{
g[i][v] = true;
v = ;
}
else v = v*+(tmp[j]-'');
}
} int l = , r = uN;
while(l<=r)
{
int mid = (l+r)>>;
if(hungary(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}

最大流:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXM = 5e2+;
const int MAXN = 2e3+; struct Edge
{
int to, next, cap, flow;
}edge[MAXN*MAXN];
int tot, head[MAXN]; int uN, vN, maze[MAXN][MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN]; void add(int u, int v, int w)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ;
edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].flow = ;
edge[tot].next = head[v]; head[v] = tot++;
} int sap(int start, int end, int nodenum)
{
memset(dep, , sizeof(dep));
memset(gap, , sizeof(gap));
memcpy(cur, head, sizeof(head));
int u = pre[start] = start, maxflow = ,aug = INF;
gap[] = nodenum;
while(dep[start]<nodenum)
{
loop:
for(int i = cur[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap-edge[i].flow && dep[u]==dep[v]+)
{
aug = min(aug, edge[i].cap-edge[i].flow);
pre[v] = u;
cur[u] = i;
u = v;
if(v==end)
{
maxflow += aug;
for(u = pre[u]; v!=start; v = u,u = pre[u])
{
edge[cur[u]].flow += aug;
edge[cur[u]^].flow -= aug;
}
aug = INF;
}
goto loop;
}
}
int mindis = nodenum;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap-edge[i].flow && mindis>dep[v])
{
cur[u] = i;
mindis = dep[v];
}
}
if((--gap[dep[u]])==)break;
gap[dep[u]=mindis+]++;
u = pre[u];
}
return maxflow;
} bool test(int mid)
{
tot = ;
memset(head, -, sizeof(head));
for(int i = ; i<uN; i++)
{
add(uN+vN, i, );
for(int j = ; j<vN; j++)
if(maze[i][j])
add(i, uN+j, );
}
for(int i = ; i<vN; i++)
add(uN+i, uN+vN+, mid); int maxflow = sap(uN+vN, uN+vN+, uN+vN+);
return maxflow == uN;
} char tmp[];
int main()
{
while(scanf("%d%d", &uN, &vN) && (uN||vN))
{
memset(maze, , sizeof(maze));
getchar();
for(int i = ; i<uN; i++)
{
gets(tmp);
int j = , len = strlen(tmp);
while(tmp[j]!=' ' && j<len) j++;
j++;
for(int v = ; j<=len; j++)
{
if(tmp[j]==' '||j==len)
{
maze[i][v] = ;
v = ;
}
else v = v*+(tmp[j]-'');
}
} int l = , r = uN;
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}

POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分的更多相关文章

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

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

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

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

  3. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

  4. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

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

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

  6. HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...

  7. POJ2289 Jamie's Contact Groups(二分图多重匹配)

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

  8. hdu3605 Escape 二分图多重匹配/最大流

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...

  9. Jamie's Contact Groups---hdu1669--poj2289(多重匹配+二分)

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

随机推荐

  1. 【MVC】使用笔记

    1,在ASP.NET MVC中,路由机制特别碉堡,直接对应于动作方法.没有必要给每一个动作方法添加视图,当视图返回View时,路由系统会自动寻找指定目录下的视图资源. public ViewResul ...

  2. 创建ArrayList集合对象并添加元素

    ArrayListDemo.java import java.util.ArrayList; /* * 为什么出现集合类: * 我们学习的是面向对象编程语言,而面向对象编程语言对事物的描述都是通过对象 ...

  3. 会跳舞的树(只用HTML+CSS)(转)

    效果如下. 共有1022个<div>元素. See the Pen wKvrMa by moyu (@MoYu1991) on CodePen.

  4. luogu2596 [ZJOI2006]书架

    treap.树是以"优先级"(优先级越小,在书架上越靠上)形成的,堆是以rand()的权值形成的.还要再维护一个原编号. 置顶/置底:找到那个元素,把它拉出来修改优先级再塞回去. ...

  5. 弹飞绵羊(bzoj 2002)

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  6. python学习之 - 时间模块

    时间模块模块名:time时间模块的转换流程图. UTC:英国格林威治时间.时间戳作用:是用来进行时间计算的.进行加减时间.注意:时间计算是用秒为单位time.process_time():测量处理器运 ...

  7. Java数组操作方法收集(快速判断某个值在这个数组中)

    Java数组操作最高效的方式是循环取值,如果转换成集合那么就会分配内存,效率不如前者,但是方法多,需要在性能调优上去权衡.切记:数组是数组,集合是集合. 下面是收集最常用的数组转成集合的操作方法: i ...

  8. Eclipse中Maven运行项目时在Console中无日志出现的问题解决

    这是由于工作空间损坏造成的,比如重装JDK后,或者重装Maven后这些问题.解决方法如下: 1.删除现有工作空间,重新选择一个新的. 2.重置Workspece. 3.可能是Maven版本太新导致的, ...

  9. dubbo-admin安装和使用

    更新下链接,不知道是不是这个项目合入Apache的缘故,链接都变成了https://github.com/apache/incubator-dubbo/ 按照常理,直接去 https://github ...

  10. Spring4.0MVC学习资料,注解自己主动扫描bean,自己主动注入bean(二)

    Spring4.0的新特性我们在上一章已经介绍过了. 包含它对jdk8的支持,Groovy Bean Definition DSL的支持.核心容器功能的改进,Web开发改进.測试框架改进等等.这张我们 ...