HDU1669 Jamie's Contact Groups 二分法+二分图的多重匹配

Jamie的朋友众多,手机通讯录过长导致查找不便。通过将联系人合理分组并最小化最大群组规模来提高查找效率。本篇介绍了一种算法解决方案,利用最大流/二分图多重匹配的方法,通过二分查找确定最佳分组方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jamie's Contact Groups
Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 589    Accepted Submission(s): 211


Problem 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
2004 Asia Regional Shanghai

设limit为每组的最大人数
显然可以通过最大流/二分图多重匹配 验证能够匹配成功
二分法确定能匹配成功的最小值即可

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
#include<sstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int NX = 1e3+5;
const int NY = 500 + 5;

struct Edge{
    int to,next;
}edge[NX*NY];
int head[NX];

inline void addEdge(int k,int u,int v){
    edge[k].to=v;
    edge[k].next=head[u];
    head[u]=k;
}

int nx,ny;//左右集合数目
bool bmask[NY];//需找增广路时的标记数组
int vcy[NY];//vcy[i]表示右集合i顶点匹配到左集合的顶点数 i:1-ny
int cy[NY][NX];//cy[i][j]表示右集合第i个顶点 匹配的第j个顶点的标号 j从0开始
int limit;//每个右集合顶点 最多匹配的左集合顶点数

bool findPath(int u){
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(!bmask[v]){
            bmask[v]=1;
            if(vcy[v]<limit){
                cy[v][vcy[v]++]=u;
                return 1;
            }
            else{
                for(int j=0;j<vcy[v];++j){
                    if(findPath(cy[v][j])){
                        cy[v][j]=u;
                        return 1;
                    }
                }
            }
        }
    }
    return 0;
}

bool mulMatch(){//返回 右集合每个点最多匹配limit个点 能否完美匹配
    fill(vcy,vcy+ny+1,0);
    for(int i=1;i<=nx;++i){
        fill(bmask,bmask+ny+1,false);
        if(!findPath(i)){
            return false;
        }
    }
    return true;
}

void buildG(){
    string line,name;
    int k;//第几个分组
    int numE=0;
    fill(head,head+nx+1,-1);
    for(int i=1;i<=nx;++i){
        getline(cin,line);
        istringstream in(line);
        in>>name;
        while(in>>k){
            addEdge(numE++,i,k+1);//原题右集合从0开始 这里从1开始
        }
    }
}

int binarySearch(){
    int l=1,r=nx;
    while(l<r){
        limit=(l+r)/2;
        if(mulMatch()){
            r=limit;
        }
        else{
            l=limit+1;
        }
    }
    return r;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    while(scanf("%d%d",&nx,&ny),nx){
        cin.ignore();
        buildG();
        printf("%d\n",binarySearch());
    }
    return 0;
}
内容概要:本文档定义了一个名为 `xxx_SCustSuplier_info` 的视图,用于整合和展示客户(Customer)和供应商(Supplier)的相关信息。视图通过连接多个表来获取组织单位、客户账户、站点使用、位置、财务代码组合等数据。对于客户部分,视图选择了与账单相关的记录,并提取了账单客户ID、账单站点ID、客户名称、账户名称、站点代码、状态、付款条款等信息;对于供应商部分,视图选择了有效的供应商及其站点信息,包括供应商ID、供应商名称、供应商编号、状态、付款条款、财务代码组合等。视图还通过外连接确保即使某些字段为空也能显示相关信息。 适合人群:熟悉Oracle ERP系统,尤其是应付账款(AP)和应收账款(AR)模块的数据库管理员或开发人员;需要查询和管理客户及供应商信息的业务分析师。 使用场景及目标:① 数据库管理员可以通过此视图快速查询客户和供应商的基本信息,包括账单信息、财务代码组合等;② 开发人员可以利用此视图进行报表开发或数据迁移;③ 业务分析师可以使用此视图进行数据分析,如信用评估、付款周期分析等。 阅读建议:由于该视图涉及多个表的复杂连接,建议读者先熟悉各个表的结构和关系,特别是 `hz_parties`、`hz_cust_accounts`、`ap_suppliers` 等核心表。此外,注意视图中使用的外连接(如 `gl_code_combinations_kfv` 表的连接),这可能会影响查询结果的完整性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值