poj-2289.jamies contact groups(二分答案 + 二分多重匹配)

Jamie's Contact Groups

Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 9227 Accepted: 3180

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

 
这题做了有好一会,他是在求匹配所有人之后匹配分组中最大值的最小值,虽然很明显是个二分,但是......我老感觉改一下匹配的代码就过了...而且最致命的是我自己试的答案都过了嘤嘤嘤,我的第一种思路:由于题目要求使得组的上界最小,那么我们就可以通过判断条件来约束,如何约束呢,如果我们发现一个组还没有匹配人,那直接匹配,因为1一定是最小值,如果不是第一次匹配,那就让匹配他的人再去匹配其他人,匹配规则依然是这样的,但如果有一个节点不满足上述情况,意思就是这个结点所连的所有组都已经被别人匹配过了,那么就直接选第一个与其进行匹配就可以了,但是这里出问题了,因为我们不能确定第一个匹配的人就是匹配之后的最小值,因为这个值肯定要加到最小值上才不会影响答案.so下面二分答案.
 
二分答案:
  二分答案,每次用匹配check,如果改上界可以使得完备匹配,那么就改变r,否则改变l,然后ok.输出mid.
  1 /*************************************************************************
  2     > File Name: poj-2289.jamies_contact_groups.cpp
  3     > Author: CruelKing
  4     > Mail: 2016586625@qq.com 
  5     > Created Time: 2019年09月03日 星期二 21时09分58秒
  6  ************************************************************************/
  7 /*
  8 #include <iostream>
  9 #include <sstream>
 10 #include <string>
 11 #include <cstring>
 12 using namespace std;
 13 
 14 const int maxn = 1000 + 5, maxm = 500 + 5, inf = 0x3f3f3f3f;
 15 
 16 int n, m;
 17 string str;
 18 char name[20];
 19 int linker[maxm][maxn];
 20 bool used[maxm], g[maxn][maxm];
 21 
 22 bool dfs(int u) {
 23     for(int v = 0; v < m; v ++) {
 24         if(g[u][v] && !used[v]) {
 25             used[v] = true;
 26             if(linker[v][0] == 0) {
 27                 linker[v][++ linker[v][0]] = u;
 28                 return true;
 29             }
 30             for(int i = 1; i <= linker[v][0]; i ++) {
 31                 if(dfs(linker[v][i])) {
 32                     linker[v][i] = u;
 33                     return true;
 34                 }
 35             }
 36             linker[v][++ linker[v][0]] = u;
 37             return true;
 38         }
 39     }
 40     return false;
 41 }
 42 
 43 int main() {
 44     while(cin >> n >> m && (n || m)) {
 45         memset(g, false, sizeof g);
 46         for(int i = 0; i < n; i ++) {
 47             cin >> name;
 48             getline(cin, str);
 49             stringstream ss;
 50             ss << str;
 51             int x;
 52             while(ss >> x)
 53                 g[i][x] = true;
 54         }
 55         int res = 0;
 56         for(int i = 0; i < m; i ++) linker[i][0] = 0;
 57         for(int i = 0; i < n; i ++) {
 58             memset(used, false, sizeof used);
 59             if(dfs(i)) res ++;
 60         }
 61         int M = 0;
 62         for(int i = 0; i < m; i ++) {
 63             if(M < linker[i][0]) M = linker[i][0];
 64         }
 65         cout << M << endl;
 66     }
 67     return 0;
 68 }
 69 */
 70 #include <iostream>
 71 #include <sstream>
 72 #include <string>
 73 #include <cstring>
 74 #define mid ((l + r) >> 1)
 75 using namespace std;
 76 
 77 const int maxn = 1000 + 5, maxm = 500 + 5, inf = 0x3f3f3f3f;
 78 
 79 int n, m, l, r;
 80 string str;
 81 char name[20];
 82 int linker[maxm][maxn];
 83 bool used[maxm], g[maxn][maxm];
 84 
 85 bool dfs(int u) {
 86     for(int v = 0; v < m; v ++) {
 87         if(g[u][v] && !used[v]) {
 88             used[v] = true;
 89             if(linker[v][0] < mid) {
 90                 linker[v][++ linker[v][0]] = u;
 91                 return true;
 92             }
 93             for(int i = 1; i <= mid; i ++) {
 94                 if(dfs(linker[v][i])) {
 95                     linker[v][i] = u;
 96                     return true;
 97                 }
 98             }
 99         }
100     }
101     return false;
102 }
103 
104 int main() {
105     while(cin >> n >> m && (n || m)) {
106         memset(g, false, sizeof g);
107         for(int i = 0; i < n; i ++) {
108             cin >> name;
109             getline(cin, str);
110             stringstream ss;
111             ss << str;
112             int x;
113             while(ss >> x)
114                 g[i][x] = true;
115         }
116         /*
117         int res = 0;
118         l = 0, r = maxn << 1;
119         for(int i = 0; i < m; i ++) linker[i][0] = 0;
120         for(int i = 0; i < n; i ++) {
121             memset(used, false, sizeof used);
122             if(dfs(i)) res ++;
123         }
124         cout << res << endl;
125         */
126         l= 0, r = n;
127         int res;
128         while(l <= r) {
129             res = 0;
130             for(int i = 0; i < m; i ++) linker[i][0] = 0;
131             for(int i = 0; i < n; i ++) {
132                 memset(used, false, sizeof used);
133                 if(dfs(i)) res ++;
134             }
135             if(n == res) r = mid - 1;
136             else l = mid + 1;
137         }
138         cout << mid + 1 << endl;
139     }
140     return 0;
141 }

 

转载于:https://www.cnblogs.com/bianjunting/p/11456522.html

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值