【POJ2289】【多重匹配】【二分】【模板】
2015-09-05 10:56
309 查看
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 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
Sample Output
Source
Shanghai 2004
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 6967 | Accepted: 2292 |
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
Shanghai 2004
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; int n,m; int vN,uN; const int N = 1010; const int M = 510; int g [M]; int match[M] ; int num[M]; bool used[M]; char str[110]; int mid; bool dfs(int u) { for(int i=0;i<vN;i++) { if(!used[i] && g[u][i]) { used[i] = true; if(num[i] < mid) { match[i][num[i]++] = u; return true; } else { for(int k=0;k<num[i];k++) { if(dfs(match[i][k])) { match[i][k] = u; return true; } } } } } return false; } int hungary() { int ret = 0; memset(num,0,sizeof(num)); for(int i=0;i<uN;i++) { memset(used,0,sizeof(used)); if(!dfs(i)) return false; } return true; } int main() { while(scanf("%d%d",&n,&m)) { if(n == 0 && m == 0) break; memset(g,0,sizeof(g)); uN = n; vN = m; for(int i=0;i<n;i++) { scanf("%s",str); char ch; while((ch=getchar()) != '\n') { int v; scanf("%d",&v); g[i][v] = 1; } } int left = 0; int right = n; //( ] while(left < right) { mid = (left + right) / 2; if(hungary()) { //(left,mid] right = mid; } else { //(mid,right] left = mid + 1; } } printf("%d\n",right); } return 0; }
相关文章推荐
- Java 解析chm文件实战(原创)
- 剑指offer——面试题42:(一)翻转单词顺序
- PHP之——官方手册下载地址
- 乱七八糟记事本
- HDU1556 color the ball(区间修改,单点查询)
- 用户及用户组管理:useradd,psswd,groupadd
- 『细节』是什么
- 『细节』是什么
- 黑马程序员——CSS
- C++11 auto和decltype推导规则
- 数据结构之反黄牛火车票订票系统
- Mysql字符转义
- C++11 auto和decltype推导规则
- EF 常见语句以及sql语句简单 后续继续添加
- acm的一点思考
- ActionScript 3.0 学习(十) AS3 模拟转盘抽奖小程序
- glCleatDepth
- glCleatDepth
- 数据结构在软件编程中的地位
- 给定n个数,要求比较次数1.5n同时找出最大值和最小值