Problem Solving report POJ2289 Jamie's contact Groups

Source: Internet
Author: User

Problem Solving report POJ2289 Jamie's contact Groups

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


The main topic: There are n contacts, m groups, tell you what each contact can be grouped, and each contact only need to divide into a group. Now, what is the minimum of the largest number of people in all groups?


Analysis: The maximum value is minimized with two points to solve, then how do we consider how to verify in the diagram whether the current value of mid is met, the answer is to experiment with the maximum flow. First the super source connects each contact node, the payload is 1, and then each contact is connected so that it can be divided into the group node, the load is 1 (because the super source point is limited so do not have to split), and then all the packet nodes are connected to the super sinks, the load is mid, indicating that all packet capacity does not exceed mid and , based on the results of the two points.



on the code:
#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include < cstring> #include <string> #include <sstream>using namespace std;const int maxm = 900000;const int MAXN = 2000;const int INF = 0x3f3f3f3f;struct edge{int to, cap, next;};  Edge edge[maxm];int level[maxn];int head[maxn];int group[maxn][maxn];int src, des, cnt;void addedge (int from, int. to, INT CAP) {edge[cnt].to = To;edge[cnt].cap = Cap;edge[cnt].next = Head[from];head[from] = Cnt++;swap (from, to); edge[cnt].to = To;edge[cnt].cap = 0;edge[cnt].next = Head[from];head[from] = cnt++;} int BFs () {memset (level,-1, sizeof level), Queue<int>q;while (!q.empty ()) Q.pop (); LEVEL[SRC] = 0;q.push (SRC); wh Ile (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;if (Edge [I].cap > 0 && level[v] = =-1) {Level[v] = Level[u] + 1;q.push (v);}}} return Level[des]! =-1;} int dfs (int u, int f) {if (U = = des) return f;int tem;for (int i = head[u]; I! =-1; i = edge[i].next) {int v = edge[i].to;if (edge[i].cap>0 && level[v] = = level [u] + 1) {tem = DFS (V, min (f, edge[i].cap)), if (Tem > 0) {edge[i].cap-= tem;edge[i ^ 1].cap + = Tem;return tem;}}} Level[u] = -1;return 0;} int dinic () {int ans = 0, Tem;while (BFS ()) {while (tem = DFS (src)) > 0) {ans + = tem;}} return ans;} int main () {int n, m;src = 0;des = 1980;string str;while (cin >> n >> m&&n&&m) {GetChar (); for ( int i = 1; I <= N; i++) {getline (cin, str); StringStream SS (str); SS >> str;int G;int num = 0;while (ss >> g) {group[i][++num] = g; }GROUP[I][0] = num;} int low = 0, high = inf-1;int ans = -1;while (Low <= high) {int mid = (low + high)/2;memset (head,-1, sizeof head) CNT = 0;for (int i = 1; I <= n; i++) {Addedge (src, I, 1);} for (int i = 1; I <= m; i++) {Addedge (i + +, DES, mid);} for (int i = 1; I <= n; i++) {for (int j = 1; J <= Group[i][0]; j + +) {Addedge (I, group[I][J] + 1001, 1);}} if (Dinic () < n) Low = mid + 1;else{ans = Mid;high = Mid-1;}} cout << ans << endl;} return 0;}

It's hard to hold a thigh these days, but I have to rely on myself!

Problem Solving report POJ2289 Jamie's contact Groups

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.