UVA 1345 Jamie's contact Groups (Max Stream + two minutes)

Source: Internet
Author: User

UVA 1345 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 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

The main topic: give some people, and they may join the group number. Ask each group what the minimum maximum number of people is. The idea of solving problems: To see the smallest maximum value to think of two points, the question is what two points. The two points of this problem are the capacity of the maximum flow. Set a super source point, connect to all people, capacity is 1. Set a super meeting point so that all the groups are connected to the super sinks, and the two points are the capacity (0~n) here. Then connect people and groups according to the relationship between the person and group, the capacity is 1. Two ticks, if the current state to find the maximum flow equals the number of people, then the lower bound equals mid, if not equal, then the upper bound equals mid. The two-out capacity is the smallest maximum number of people in the group.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <sstream>#include <iostream>using namespace STD;Const intN =1010+510;Const intM =1010*510*4;Const intPO =1005;Const intINF =0x3f3f3f3f;typedef Long LongllintN, M, S, T, Rec[po][po], Cnt[po];intEC, Head[n], first[n], que[n], lev[n];intNEXT[M], to[m], v[m];voidInit () {EC =0;memset(First,-1,sizeof(first)); }voidAddedge (intAintBintc) {To[ec] = b;      V[EC] = c;      NEXT[EC] = First[a];      First[a] = ec++;      TO[EC] = A; V[EC] =0;      NEXT[EC] = first[b];  FIRST[B] = ec++; }intBFS () {intKid, now, F =0, r =1Imemset(Lev,0,sizeof(Lev)); que[0] = s, lev[s] =1; while(F < R) {now = que[f++]; for(i = First[now]; I! =-1; i = Next[i]) {kid = To[i];if(!lev[kid] && v[i]) {Lev[kid] = Lev[now] +1;if(Kid = = t)return 1;              que[r++] = kid; }          }      }return 0; }intDFS (intNowintSUM) {intKid, flow, RT =0;if(now = = t)returnSum for(inti = Head[now]; I! =-1&& RT < sum;            i = Next[i]) {Head[now] = i; Kid = To[i];if(Lev[kid] = = Lev[now] +1&& V[i]) {flow = DFS (Kid, Min (Sum-rt, v[i]));if(flow)                  {V[i]-= flow; v[i^1] + = flow;              RT + = flow; }ElseLev[kid] =-1; }                 }returnRt }intDinic () {intAns =0; while(BFS ()) { for(inti =0; I <= t;          i++) {Head[i] = First[i]; } ans + = DFS (s, INF);if(Ans >= N)returnAns }returnAns }voidInput () {init (); s =0, T = (n + m) +2;memset(CNT,0,sizeof(CNT));Charch[ -];intNumstringss for(inti =0; I < n; i++) {getline (Cin, SS);StringStreamStream (ss); Stream >> ch; while(Stream >> num) {Addedge (i +1, Num +1+ N,1); }    } for(inti =1; I <= N; i++) {Addedge (S, I,1); } for(inti =1; I <= m; i++) {Addedge (i + N, T,1); }}intBuildintx) { for(inti =0; I < EC; i + =2) {if(To[i] = = t) v[i] = x;ElseV[i] =1; v[i^1] =0; }inttemp = Dinic ();if(temp = = N)return 1;Else return 0;}intMain () { while(scanf("%d%d\n", &n, &m) = =2, (n | | m)) {input ();intL =0, R = n +2; while(L < R) {intMid = (L + R)/2;if(Build (mid)) R = mid;ElseL = mid +1; }printf("%d\n"+ k); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.

UVA 1345 Jamie's contact Groups (Max Stream + two minutes)

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.