eolymp
bolt
Try our new interface for solving problems
Problems

The Writer`s Club

The Writer`s Club

Forget Facebook, forget Second Life, \textit{The Writers’ Club} is the next big thing on the Internet. The Writers’ Club is where both writers and lovers of short stories get to meet, publish, read, and discuss the writers’ promising contributions. As any other Internet virtual community, the website gives readers the facility to create a list of their favorite writers. The website maintainers have noticed that readers tend to favor writers already favored by the readers’ favorite writers. For example, if John likes the stories written by Alice, then many of the readers who likes John’s stories also like Alice’s. Needless to say, John’s readers tend to also admire Alice’s favorite writers too. The website would like to start a recommendation service based on this observation. Continuing with the above example, the website would like to recommend Alice (and her favorite writers, and their favorite writers, and so on) to all of John’s readers. Of course, the recommendation service has to be smart enough not to recommend a writer already favored by the reader receiving the recommendation. \InputFile Your program will be tested on one or more test cases. The first line of a test case specifies two positive integers: \textbf{T} and \textbf{N} where \textbf{T} is the total number of users (not exceeding \textbf{100000}) and \textbf{N} is the number of writers (not exceeding \textbf{100}). \textbf{N} line follows, one for each writer. Each line is made of one or more names, the first being the name of a writer, and the remaining being the names of admirers of that writer. For example: \textbf{writer} \textbf{reader_1} \textbf{reader_2} \textbf{…} \textbf{reader_d} Where writer is the writer’s name, \textbf{reader_1} \textbf{…} \textbf{reader_d} are the names of the writer’s admirers. A name is a sequence of one or more lower-case letters, no longer than \textbf{16} characters. Names are unique throughout the test case. Names are separated by one or more spaces. The last line of the input file is made of two zero’s. \OutputFile For each test case, the first line of the output should be: \textbf{--- CASE k} Where \textbf{k} is the test case number (starting at \textbf{1}), and '\textbf{-}' is the minus sign. Following that, there should be zero or more lines, one line for each writer being recommended. The format is: \textbf{writer} \textbf{reader_1} \textbf{reader_2} \textbf{...} Where \textbf{writer} is the name of the writer to be recommended for \textbf{reader_1}, \textbf{reader_2}, and so on. The output lines should be sorted alphabetically according to writers, and for each writer, readers should also be sorted alphabetically. No writer should be recommended for himself. All names in the output are separated by a single space.
Time limit 1 second
Memory limit 64 MiB
Input example #1
7 3
john paul ringo
alice paul john
bob  alice sunny cher
0 0
Output example #1
--- CASE 1
alice ringo
bob john paul ringo