POJ 2289 Jamie's Contact Groups (二分答案+二分图的多重匹配)
2013-05-01 12:31
495 查看
Jamie's Contact Groups
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
Sample Output
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 5803 | Accepted: 1828 |
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
二分图的多重匹配。此题求的是所有匹配点中,最多匹配边数的点的匹配边要最少。做法是二分答案,由于有N个人,那么一个组最多就有N个人,最少是1个人,从1~N中二分,对于每一个答案进行多重匹配,找到满足多重匹配的最小答案。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define SIZE 505 using namespace std; struct node { int to,next; }edge[SIZE*1000]; int N,M; int head[SIZE<<1],idx; bool vis[SIZE]; int link[SIZE<<1][SIZE],num[SIZE]; //记录匹配对象,记录该点匹配的次数 int limit; void addNode(int from,int to) { edge[idx].to = to; edge[idx].next = head[from]; head[from] = idx ++; } bool dfs(int lt) { for(int i=head[lt]; i!=-1; i=edge[i].next) { int to = edge[i].to; if(!vis[to]) { vis[to] = true; if(num[to] < limit) { link[to][++num[to]] = lt; return true; } for(int j=1; j<=limit; j++) { if(dfs(link[to][j])) { link[to][j] = lt; return true; } } } } return false; } bool match() { memset(num,0,sizeof(num)); for(int i = 1; i <= N; i++) { memset(vis,0,sizeof(vis)); if(!dfs(i)) return false; } return true; } int binarySearch() //二分答案,每个group最少1人,最多N人 { int low = 1, high = N; while(low < high) { limit = (low + high) >> 1; if(match()) high = limit; else low = limit + 1; } return high; } int main() { while(~scanf("%d%d",&N,&M)) { if(!N && !M) break; idx = 0; memset(head,-1,sizeof(head)); char info[2048]; getchar(); for(int i = 1; i <= N; i++) { gets(info); int temp = 0; for(int j = 0; j <= (int)strlen(info); j++) { temp = 0; if(info[j] >= '0' && info[j] <= '9') { while(info[j] >= '0' && info[j] <= '9') temp = temp*10 + (info[j++] - '0'); addNode(i,++temp); } } } memset(link,0,sizeof(link)); int ans = binarySearch(); printf("%d\n",ans); } return 0; }
相关文章推荐
- poj2289 Jamie's Contact Groups(二分答案+最大流)
- POJ2289 Jamie's Contact Groups(二分图多重匹配)
- Poj 2289 Jamie's Contact Groups【二分+多重匹配】
- poj2289--Jamie's Contact Groups(二分多重匹配)
- POJ 2289 Jamie’s Contact Groups-二分匹配&多重匹配
- 【二分图多重匹配 && Dinic】POJ - 2289 Jamie's Contact Groups
- POJ 2289 Jamie's Contact Groups (二分答案+匹配流)
- POJ2289-Jamie's Contact Groups(二分图多重匹配)
- POJ 2289 Jamie's Contact Groups (二分+匹配/网络流)
- Poj 2289 Jamie's Contact Groups 【最大流Dinic+二分】
- POJ 2289 Jamie's Contact Groups 多重匹配+二分
- Jamie's Contact Groups(二分图多重匹配+二分)(网络流)
- POJ 2289 Jamie's Contact Groups (二分+dinic)
- POJ -2289 -Jamie's Contact Groups (二分图多重匹配)
- POJ2289 Jamie's Contact Groups(二分图多重匹配+二分)
- POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】
- hdu 1669 Jamie's Contact Groups(二分+二分图最大多重匹配)
- POJ 2289--Jamie's Contact Groups【二分图多重匹配问题 &&二分查找最大值的最小化 && 最大流求解】
- 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 (二分,二分图匹配)
- HDU1669 Jamie's Contact Groups (二分+二分图的多重匹配+一对多的匹配)