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

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


二分最大值mid,源点连向n个联系人权值为1,联系人连向它可以属于的分类,m个分类与汇点建边权值为mid(该分类最多mid个人)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int d[maxn], head[maxn], cur[maxn];
int n, m, s, t, ans;
int cnt = ;
vector<int> G[maxn];
struct node
{
int u, v, c, next;
} Node[maxn*]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[e.u] + ;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = , V;
if(u == t || cap == )
return cap;
for(int &i=cur[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] == d[e.u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Node[i].c -= V;
Node[i^].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
return ret;
} int dinic(int u)
{
ans = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ans += dfs(u, INF);
}
return ans;
} int main()
{
char str[maxn], ch;
while(~scanf("%d%d",&n,&m) && n+m)
{
for(int i=; i<=n; i++) G[i].clear();
int ret = ;
s = n+m+; t = n+m+;
int v;
for(int i=; i<=n; i++)
{
cin>> str;
while(scanf("%d%c", &v, &ch))
{
G[i].push_back(v);
if(ch == '\n')
break;
}
}
int l = , r = n, mid;
while(l <= r)
{
mem(head, -);
cnt = ;
mid = (l + r) / ;
for(int i=; i<=n; i++)
for(int j=; j<G[i].size(); j++)
add(m+i, G[i][j], );
for(int i=; i<=n; i++)
add(s, m+i, );
for(int i=; i<m; i++)
add(i, t, mid);
if(dinic(s) == n)
{
ret = mid;
r = mid - ;
}
else
l = mid + ;
}
cout<< ret <<endl; } }

												

Jamie's Contact Groups POJ - 2289(多重匹配 最大值最小化 最大流)的更多相关文章

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

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

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

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

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

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

  4. M - Jamie's Contact Groups - poj 2289(二分图多重匹配)

    题意:某个人通讯录有很多人,现在他想把这个人分组,给的数据是可以把这个人分在那些组里面,现在他想知道分组后,人最多的那个组至少有多少人. 分析:因为没有给组限制有多少人,可以使用二分求出来最小的那个, ...

  5. 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 ...

  6. poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】

    题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K ...

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

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

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

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

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

    Jamie's Contact Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/ ...

随机推荐

  1. Docker容器运行GUI程序的配置方法

    0.环境说明 Ubuntu 16.04 docker 1.35 1.Docker的“可视化” Docker本身的工作模式是命令行的,因为主要的使用场景可能是做服务器后端方面的比较多. 但有时候我们会有 ...

  2. mac下载、破解、安装webstorm编辑器

    1.进入webstorm官网 http://www.jetbrains.com/webstorm/,点击DOWNLOAD,开始下载webstorm安装包. untitled.png 2.开始安装 双击 ...

  3. 路遥眼里的河南人<平凡的世界>

    路遥,一个作过农民,当过小学教师,用平凡的生命,却写出不平凡的小说<平凡的世界>,他喜欢夜的宁静,喜欢在夜里思考,他说只有在夜里我们才是最真实的自己.所以他喜欢在夜里创作,这部小说也是在这 ...

  4. 【转】从Shell脚本内部将所有标准输出及标准错误显示在屏幕并同时写入文件的方法

    如果全部都要重定向的话每一条命令后面>>并不方便,可以这么做.在开头就声明 exec 1>>$log_file表示将脚本中所有的正确输出全部追加到$log_file,错误信息会 ...

  5. Flask学习-Flask app接受第一个HTTP请求

    一.__call__() 在Flask app启动后,一旦uwsgi收到来自web server的请求,就会调用后端app,其实此时就是调用app的__call__(environ,start_res ...

  6. jersey2 整合 spring + hibernate + log4j2

    整合 spring jersey2 官方还未正式支持 spring4, 但网上有好多支持方案,折腾了一圈后,还是用了 spring3; pom 添加以下依赖配置 <!-- Spring --&g ...

  7. Yaml学习文档

    pdf文档地址 http://yaml.org/spec/ JS-Yaml demo地址 http://nodeca.github.io/js-yaml/

  8. Jenkins分布式构建

    Jenkins分布式构建 有时,如果有一个实例,它是一个更大,更重的项目,需要定期编译生成在许多计算机上.并运行所有这些构建了中央台机器上可能不是最好的选择.在这种情况下,人们可以配置其他Jenkin ...

  9. [SHELL]输入输出重定向与管道

    一 . 输出重定向(将命令的输出重定向到文件): ls -al > test 以覆盖的方式写入 ls -al >> test 以追加的方式写入 二 . 输入重定向(将文件的内容重定向 ...

  10. CentOS7使用winbind加入AD

    https://ishm.idv.tw/?p=336 CentOS 7 使用 winbind 加入 AD 需求:已經熟悉 CentOS 6 的 AD 加入方式,CentOS 7 已將 winbind ...