#C11571. Organize Books by Genre
Organize Books by Genre
Organize Books by Genre
In this problem, you are given multiple test cases. In each test case, you have a list of books, each identified by a unique identifier and associated with a specific genre. Your task is to group the books by their genres under the constraint that each genre group contains no more than (k) books. If any genre group exceeds (k) books, output "NO"; otherwise, output "YES" followed by the genre groups. Each genre group should be printed on a new line in the format:
Genre: book_id1 book_id2 ...
The order of genres in the output should follow their first appearance in the input.
inputFormat
The input begins with an integer (T) representing the number of test cases. For each test case:
1. The first line contains two integers (n) and (k) where (n) is the number of books and (k) is the maximum number of books allowed in any genre group.
2. The following (n) lines each contain two strings: the book identifier and its genre, separated by a space.
All input should be read from standard input (stdin).
outputFormat
For each test case, if every genre group contains no more than (k) books, print "YES" on the first line and then for each genre, print a line in the format:
Genre: book_id1 book_id2 ...
If any genre group contains more than (k) books, print "NO" instead. The output for each test case should be printed to standard output (stdout).## sample
1
5 3
B1 Fiction
B2 Fiction
B3 Science
B4 Fiction
B5 Romance
YES
Fiction: B1 B2 B4
Science: B3
Romance: B5
</p>