標籤:
Jamie‘s Contact GroupsTime Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uSubmit Status Practice POJ 2289
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
我的第一道二分圖多重匹配題:
轉一個不錯的Blog:http://www.cnblogs.com/zhengguiping--9876/p/4728358.html
Jamie有很多連絡人,但是很不方便管理,他想把這些連絡人分成組,已知這些連絡人可以被分到哪個組中去,而且要求每個組的連絡人上限最小,即有一整數k,使每個組的連絡人數都不大於k,問這個k最小是多
少?
一對多的二分圖的多重匹配。二分圖的多重匹配演算法的實作類別似於匈牙利演算法,對於集合x中的元素xi,找到一個與其相連的元素yi後,檢查匈牙利演算法的兩個條件是否成立,若yi未被匹配,則將
xi,yi匹配。否則,如果與yi匹配的元素已經達到上限,那麼在所有與yi匹配的元素中選擇一個元素,檢查是否能找到一條增廣路徑,如果能,則讓出位置,讓xi與yi匹配。
二分求出limit,知道找到可以構成多重匹配的最小限制limit,在main函數中二分搜尋。
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;#define N 1010int vis[N], maps[N][N], ans, n, m;struct node{ int cnt;///和yi相匹配的個數; int k[N];///和yi相匹配的x的集合;}Linky[N];bool Find(int u, int limit){ for(int i=1; i<=m; i++) { if(!vis[i] && maps[u][i]) { vis[i] = 1; if(Linky[i].cnt < limit) { Linky[i].k[ Linky[i].cnt++ ] = u; return true; } for(int j=0; j<Linky[i].cnt; j++) { if(Find( Linky[i].k[j], limit )) { Linky[i].k[j] = u; return true; } } } } return false;}bool hungary(int limit)///匈牙利演算法;{ memset(Linky, 0, sizeof(Linky)); for(int i=1; i<=n; i++) { memset(vis, 0, sizeof(vis)); if(!Find(i, limit))///當前的limit讓i沒有匹配,所以不能用limit; return false; } return true;}int main(){ int x; char s[20], ch; while(scanf("%d %d", &n, &m), m+n) { memset(maps, 0, sizeof(maps)); for(int i=1; i<=n; i++) { scanf("%s", s); while(1) { scanf("%d%c", &x, &ch); maps[i][x+1] = 1; if(ch == '\n') break; } } int L = 1, R = n; ans = n; while(L <= R) { int mid = (L+R)/2; if(hungary(mid))///如果當前mid滿足題意; { R = mid-1; ans = mid; } else L = mid+1; } printf("%d\n", ans); } return 0;}
Jamie's Contact Groups(二分圖多重匹配+二分)(網路流)