Jamie's Contact Groups
InterviewTime limit1sMemory limit128 MB
Assign each of N friends to one of M allowed groups so that every friend lands in exactly one group and the largest group size is as small as possible.
- Level
Medium6 of 10
- Topics
- Binary search, Graph, BFS, Dynamic programming
- Solved
- No attempts yet
Problem
Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list on her phone. The list has grown so long that it often takes her a long time to browse through the whole thing to find a friend's number. As Jamie's best friend and a programming genius, you suggest that she split the contact list into groups and minimize the size of the largest group, so that it becomes much easier to search for a friend's number within a group.
Jamie takes your advice and gives you the full list of her friends' names, the number of groups she wants, and the groups each friend could belong to. Your task is to write a program that organizes the list into groups so that each friend belongs to exactly one group and the size of the largest group is minimized.
Input
The input consists of several test cases; there are at most of them.
Each test case starts with a line containing two integers and , where is the length of the contact list (the number of friends) and is the number of groups. Then lines follow, each containing a friend's name and the group numbers that friend could belong to, separated by spaces.
You may assume that is no more than and is no more than . Names contain alphabet letters only and are no longer than characters, and no two friends share the same name. Each group label is an integer between and , inclusive.
After the last test case, a single line 0 0 terminates the input.
Output
For each test case, output a single line containing one integer: the smallest possible size of the largest group over all valid groupings.