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

												

最新文章

  1. SQL Server附加数据库时报1813错误的解决方案
  2. 【Go入门教程3】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)
  3. JS处理JSON和数组
  4. MicroERP开发技术分享:技术选型
  5. [ACM_动态规划] 找零种类
  6. ubuntu 中增加鼠标右键菜单,为Windows 的exe 程序快速增加桌面快捷键
  7. iOS开发-Alpha,Hidden与Opaque区别
  8. 第11章 Windows线程池(2)_Win2008及以上的新线程池
  9. #import vs. @class
  10. C#转义字符总结
  11. mpu6050参数获取
  12. Java 8 Learn Notes
  13. jQuery中下拉select、复选checkbox、单选radio的操作代码
  14. 基于winpcap的以太网流量分析器(java)
  15. 第三周java学习总结
  16. ***CodeIgniter框架集成支付宝即时到账支付SDK
  17. 学习h264 的语法规则,如何才能看懂H264 的官方文档
  18. git修改历史记录
  19. SQL关闭自增长列标识:SET IDENTITY_INSERT
  20. Java DES 加解密(&quot;DES/CBC/PKCS5Padding&quot;)

热门文章

  1. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
  2. ansible 远程以普通用户执行命令
  3. Ubuntu系统上双节点部署OpenStack
  4. POJ1807&amp;&amp;1276
  5. ucos获得系统时间OSTimeGet();
  6. MySQL主从报错1594
  7. 利用OVS+FLOODLIGHT,为数据表添加VLAN_ID和MPLS
  8. 上google的方法
  9. CS100.1x Introduction to Big Data with Apache Spark
  10. .Net单元测试业务实践