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;
}
内容概要:本文详细介绍了如何使用STM32微控制器精确控制步进电机,涵盖了从原理到代码实现的全过程。首先,解释了步进电机的工作原理,包括定子、转子的构造及其通过脉冲信号控制转动的方式。接着,介绍了STM32的基本原理及其通过GPIO端口输出控制信号,配合驱动器芯片放大信号以驱动电机运转的方法。文中还详细描述了硬件搭建步骤,包括所需硬件的选择与连接方法。随后提供了基础控制代码示例,演示了如何通过定义控制引脚、编写延时函数和控制电机转动函数来实现步进电机的基本控制。最后,探讨了进阶优化技术,如定时器中断控制、S形或梯形加减速曲线、微步控制及DMA传输等,以提升电机运行的平稳性和精度。 适合人群:具有嵌入式系统基础知识,特别是对STM32和步进电机有一定了解的研发人员和技术爱好者。 使用场景及目标:①学习步进电机与STM32的工作原理及二者结合的具体实现方法;②掌握硬件连接技巧,确保各组件间正确通信;③理解并实践基础控制代码,实现步进电机的基本控制;④通过进阶优化技术的应用,提高电机控制性能,实现更精细和平稳的运动控制。 阅读建议:本文不仅提供了详细的理论讲解,还附带了完整的代码示例,建议读者在学习过程中动手实践,结合实际硬件进行调试,以便更好地理解和掌握步进电机的控制原理和技术细节。同时,对于进阶优化部分,可根据自身需求选择性学习,逐步提升对复杂控制系统的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值