zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    selenium + python 环境配置 (三)之启动chrome
    selenium + python 环境配置 (二)之启动IE
    selenium + python 环境配置 (一)
    知源图四点坐标和目标图四点坐标,求透视变换矩阵
    memset()初始化为1的那些事
    NSOJ 4621 posters (离散化+线段树)
    申请到新博客了好开心
    SpringCloud之Hystrix-Dashboard监控,以及踩的坑...
    SpringBoot中在除Controller层 使用Validation的方式
    Docker安装ElasticSearch 以及使用LogStash实现索引库和数据库同步
  • 原文地址:https://www.cnblogs.com/bianjunting/p/11456522.html
Copyright © 2011-2022 走看看