poj2289
来源:互联网 发布:织梦 列表 ajax 排序 编辑:程序博客网 时间:2024/05/17 08:07
Jamie's Contact Groups
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 7213 Accepted: 2398
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 2John 0 1Rose 1Mary 15 4ACM 1 2 3ICPC 0 1Asian 0 2 3Regional 1 2ShangHai 0 20 0
Sample Output
22
Source
Shanghai 2004
在通讯录中有N个人,每个人能可能属于多个group,现要将这些人分组m组,设各组中的最大人数为max,求出该最小的最大值
分析:二分group的最大值,+二分图多重匹配
/**二分图多重匹配 */#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int N = 2000+10,inf=0x3f3f3f3f;const int M = 2000+10;int n,m;int g[N][M],vlink[M],link[M][N],max_cap;bool vis[M];int path(int s){ for(int i=0;i<m;i++) { if(g[s][i] && !vis[i]) { vis[i]=true; if(vlink[i]<max_cap) { link[i][vlink[i]++]=s; return 1; } for(int j=0;j<vlink[i];j++) { if(path(link[i][j])) { link[i][j]=s; return 1; } } } } return 0;}bool max_match(int mid){ max_cap=mid; memset(vlink,0,sizeof(vlink)); for(int i=0;i<n;i++) { memset(vis,false,sizeof(vis)); if(!path(i)) return false; } return true;}int main(){ char id[5000]; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; memset(g,0,sizeof(g)); getchar(); for(int i=0;i<n;i++) { gets(id); int len=strlen(id),v; for(int j=0;j<len;j++) { if(id[j]<='9'&&id[j]>='0') { v=0; while(id[j]<='9'&&id[j]>='0') v=v*10+id[j++]-'0'; g[i][v]=1; } } /**这样写更简洁 scanf("%s",str); while(true) { scanf("%d%c",&a,&c); map[i][a]=true; if(c=='\n') break; } */ } int left=0,right=n; while(left<right) { int mid=(left+right)>>1; if(max_match(mid)) right=mid; else left=mid+1; } printf("%d\n",right); } return 0;}
0 0
- poj2289
- poj2289多重二分匹配
- POJ2289--Jamie's Contact Groups
- 二分图多重匹配--poj2289
- 【POJ2289】【多重匹配】【二分】【模板】
- poj2289(二分多重匹配)
- poj2289 Jamie's Contact Groups
- POJ2289 Jamie's Contact Groups 二分+最大流匹配
- 解题报告 之 POJ2289 Jamie's Contact Groups
- POJ2289 Jamie's Contact Groups 二分图多重匹配
- POJ2289-Jamie's Contact Groups(二分图多重匹配)
- POJ2289 Jamie's Contact Groups(二分图多重匹配)
- poj2289--Jamie's Contact Groups(二分多重匹配)
- POJ2289 Jamie's Contact Groups(二分图多重匹配+二分)
- poj2289 Jamie's Contact Groups(二部图多重匹配)
- poj2289 Jamie's Contact Groups(二分答案+最大流)
- 常用软件下载地址
- 硬盘分区表知识
- 【排序算法】希尔排序(java实现)
- RMI使用
- VR产业链
- poj2289
- sso with ad credential and saml2.0 integration(2)
- 图像的矩
- POJ2406 Power Strings
- 练习3-M
- [bzoj4513][SDOI2016]储能表
- DataGirdView可作为参数传入一个类去,在类里会被修改
- 31-Longest Common Prefix
- ....are only available on JDK 1.5 and higher