1968. The Writers' Club

单点时限: 3.0 sec

内存限制: 256 MB

Forget Facebook, forget Second Life, 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.

输入格式

Your program will be tested on one or more test cases. The first line of a test case specifies two positive integers: T and N where T is the total number of users (not exceeding 100,000) and N is the number of writers (not exceeding 100). 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:

writer reader1 reader2 … readerd

Where writer is the writer’s name, reader1 … readerd are the names of the writer’s admirers. A name is a sequence of one or more lower-case letters, no longer than 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.

输出格式

For each test case, the first line of the output should be:

— CASE k

Where k is the test case number (starting at 1), and ‘-‘ is the minus sign.

Following that, there should be zero or more lines, one line for each writer being recommended. The format is:

writer reader1 reader2 …

Where writer is the name of the writer to be recommended for reader1, reader2, 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.

样例

Input
7 3
john paul ringo
alice paul john
bob alice sunny cher
5 3
tantawi taha najeeb
aqqad najeeb ehsan
taha aqqad najeeb
0 0
Output
--- CASE 1
alice ringo
bob john paul ringo
--- CASE 2
taha ehsan
tantawi aqqad ehsan

0 人解决,2 人已尝试。

0 份提交通过,共有 2 份提交。

9.9 EMB 奖励。

创建: 16 年,8 月前.

修改: 7 年,3 月前.

最后提交: 4 年,1 月前.

来源: Arab and North Africa 2007

题目标签