Jamie ' s Contact GroupsTime
limit:7000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 2289
description
Jamie is a very popular girl and have quite a lot of friends, so she's always keeps a very long contact list in her cell Phon E. The contact list have become so long that it often takes a long time for she to browse through the whole list to find a Friend ' s number. As Jamie's best friend and a programming genius, you suggest so she group the contact list and minimize the size of the Largest group, so that it would be is easier for she to search for a friend's number among the groups. Jamie takes your advice and gives she entire contact list containing her friends ' names, the number of groups she wish Es to has and what groups every friend could belong to. Your task is to write a program this takes the list and organizes it into groups such that all friend appears in only one Of those groups and the size of the largest group is minimized.
Input
There'll is at the most test cases. Ease case starts with a line containing-integers n and M. where n is the length of the contact list and M are the Numbe R 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 are no more than and M are no more than 500. The names would contain alphabet letters only and would be no longer than. No. Friends has the same name. The group label is an integer between 0 and M-1. After the last test case, there was 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 4ACM 1 2 3ICPC 0 1Asian 0 2 3Regional 1 2ShangHai 0 20 0
Sample Output
22
My first two-way graph multi-match problem:
Turn a nice blog:http://www.cnblogs.com/zhengguiping--9876/p/4728358.html.
Jamie has a lot of contacts, but it is very inconvenient to manage, he wants to divide these contacts into groups, know that these contacts can be divided into which group, and require each group of contacts to the minimum, that is, there is an integer k, so that each group's contact number is not more than K, ask this k minimum is more
Less?
Multiple matches of a one-to-many binary graph. The implementation of the multi-matching algorithm of the binary graph is similar to the Hungarian algorithm, for the element XI in the set X, to find an element that is connected to Yi, check whether the two conditions of the Hungarian algorithm are established, if Yi is not matched, it will
Xi,yi matches. Otherwise, if the element that matches the Yi has reached the upper limit, select an element in all the elements that match Yi, and check to see if an augmented path can be found, and if so, make a position and let Xi and Yi match.
Two points to find the limit, know that can form a multi-match the minimum restriction limits, in the main function binary search.
#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;///and Yi; the set of x matching int k[n];///and Yi;}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)///Hungarian algorithm; {memset (linky, 0, sizeof (linky)); for (int i=1; i<=n; i++) {memset (Vis, 0, sizeof (VIS)); if (! Find (i, limit))///Current limit Let I do not match, so can not use 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); 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))////If the current mid satisfies test instructions; {R = mid-1; Ans = mid; } else L = mid+1; } printf ("%d\n", ans); } return 0;}
Jamie's contact Groups (binary graph multiple match + dichotomy) (Network stream)