Jamie's Contact Groups
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 6511   Accepted: 2087

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
这是一对多的匹配,就是增广路改一改,改成num<limit时可以增广就行
如果group的匹配数小于当前要找的答案,那么就直接加入group的匹配集里,否则在匹配集里找可以增广的点修改
需要注意的是used数组,如果a->groupx->a就会无限调用,所以设置了一个used数组每次都清空,有点懒
#include <iostream>
#include <cstring>
#include <vector>
#include <sstream>
#include <string>
using namespace std; const int maxn=;
int m,n;
int num[maxn],cmp[maxn][maxn],used[maxn],tot;
vector<int > G[maxn]; void printg(){
for(int i=;i<n;i++){
for(int j=;j<G[i].size();j++){
int t=G[i][j];
cout<<"G["<<i<<"]["<<j<<"]:"<<t<<" ";
}
cout<<endl;
}
} bool fnd(int s,int limit){
for(int i=;i<G[s].size();i++){
int t=G[s][i];
if(used[t])continue;
used[t]=true;
if(num[t]<limit){cmp[t][num[t]++]=s;return true;}
for(int j=;j<num[t];j++){
if(fnd(cmp[t][j],limit)){
cmp[t][j]=s;
return true;
}
}
}
return false;
} bool hungry(int limit){
tot=;
memset(num,,sizeof(num));
for(int i=;i<n;i++){
memset(used,,sizeof(used));
if(fnd(i,limit))tot++;
}
if(tot==n)return true;
return false;
} int getans(int s,int e){
int mid=(s+e)/;
if(hungry(mid)){
return s==mid?mid:getans(s,mid);
}
return s==mid?e:getans(mid,e);
} string buff,rbuff;
int main(){
cin.tie();
ios::sync_with_stdio(false);
stringstream ss;
while(cin>>n>>m&&(n||m)){
getline(cin,rbuff);
for(int i=;i<n;i++){
G[i].clear();
getline(cin,rbuff);
ss.clear();
ss<<rbuff;
ss>>buff;
int t;
while(ss>>t){
t+=n;
G[i].push_back(t);
}
}
//printg();
if(m==)cout<<<<endl;
else {
int ans=getans(n/m,n+);
cout<<ans<<endl;
}
}
return ;
}

POJ 2289 Jamie's Contact Groups 二分图多重匹配 难度:1的更多相关文章

  1. POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】

    Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  2. POJ 2289 Jamie's Contact Groups(多重匹配+二分)

    题意: Jamie有很多联系人,但是很不方便管理,他想把这些联系人分成组,已知这些联系人可以被分到哪个组中去,而且要求每个组的联系人上限最小,即有一整数k,使每个组的联系人数都不大于k,问这个k最小是 ...

  3. POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 6 ...

  4. Poj 2289 Jamie's Contact Groups (二分+二分图多重匹配)

    题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...

  5. POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)

    POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...

  6. poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】

    题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K ...

  7. POJ - 2289 Jamie's Contact Groups (二分图多重匹配)

    题意:N个人,M个团体.每个人有属于自己的一些团体编号.将每个人分配到自己属于的团体中,问这个人数最多的团体其人数最小值是多少. 分析:一个一对多的二分图匹配,且是最大值最小化问题.二分图的多重匹配建 ...

  8. POJ 2289 Jamie's Contact Groups 【二分】+【多重匹配】(模板题)

    <题目链接> 题目大意: 有n个人,每个人都有一个或者几个能够归属的分类,将这些人分类到他们能够归属的分类中后,使所含人数最多的分类值最小,求出该分类的所含人数值. 解题分析: 看到求最大 ...

  9. POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment

    这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...

随机推荐

  1. NOIP 华容道

    描述 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间. 小 B 玩的华容道与经典的 ...

  2. newcode wyh的吃鸡(优势队列+BFS)题解

    思路: 要用优势队列,因为有的+2,有的+1,所以队列中的步长是不单调的,所以找到一个答案但不一定最小,所以用优势队列把小的放在队首. 要记录状态,所以开了三维,题目和昨天做的那道小明差不多 vis开 ...

  3. zedgraph多个graphpane的处理

    这个问题需要研究,需要使用  zedgraph.masterpane.panelist 其他人做的效果--先预留一个官网的链接http://zedgraph.dariowiz.com/index113 ...

  4. 51nod 1020 逆序排列 递推DP

    1020 逆序排列  基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么 ...

  5. 3、CommonChunkPlugin提取公共js-以提取一个jquery为例

    cnpm install css-loader --save-dev    //css-loader 是将css打包进js cnpm install style-loader --save-dev   ...

  6. Wireshark无法解析OpenFlow配置协议 解决方法

    在使用wireshark对OpenFlow交换机与FlowVisor的通信过程进行抓包分析的时候,在其选项中有openflow_v1选项,但Wireshark竟无法解析OpenFlow协议. 在查阅相 ...

  7. FAST:NetMagic交换机 与 Floodlight控制器 连接实战

    设备 NetMagic 08交换机 - 1; 装有Windows 7系统的PC - 1; VMware Workstation, Ubuntu 14.04 64bit - 1; 网线 - 1; 网口转 ...

  8. maven+nexus配置本地私有仓库

    以下是settting.xml的配置 <?xml version="1.0" encoding="UTF-8"?> <settings> ...

  9. 测试报告 之 testNG + Velocity 编写自定义html测试报告

    之前用testNG自带的test-outputemailable-report.html,做出的UI自动化测试报告,页面不太好看. 在网上找到一个新的报告编写,自己尝试了一下,埋了一些坑,修改了输出时 ...

  10. Docker Container的概述

    ·通过Image创建(copy) ·在Image layer之上建立一个container layer(可读写) ·类比对象:类和实例(Image相当于抽象的一个类,Container相当于实例化的一 ...