eolymp
bolt
Try our new interface for solving problems
Məsələlər

Sub-dictionary

Sub-dictionary

In this problem, by the word "dictionary" we mean a list of alphabetically ordered words and their associated explanations in the same language. A dictionary must contain the definition for any word that appears in the explanation of another word. So you see, if a dictionary defines N words, it has exactly \textbf{N} distinct words in it. Also, we know that in a dictionary no word appears in the definition of itself. A sub-dictionary is a collection of dictionary's words and their definitions such that it can be published as an independent dictionary, obviously satisfying the mentioned condition. As a project of Computational Linguistics course, we are assigned to create a Lexical Knowledge Base which is the knowledge expressed by words. For this task we should create our knowledge foundation based on a dictionary. It's really hard for the computer to study words automatically. So, we decided to manually teach it some common words. We start from an appropriate sub-dictionary. By understanding its words, a computer could extend its knowledge to the whole dictionary word by word. For instance, a word "\textbf{xyz}" could be added to the computer's understanding if computer knows the meaning of every words used in \textbf{xyz}'s definition. You are asked to write a program that can find the smallest extendable sub-dictionary for a specific dictionary. \InputFile The input consists of multiple test cases. The first line of each test case is \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{100}), the number of dictionary's words. Each of the next \textbf{n} lines contains a word and its definition (that has at most \textbf{30} words). Words are separated by blanks and are made up of small English letters less than \textbf{25} characters. \OutputFile For each test case, on the first line print the number of sub-dictionary's words and on the second line write the alphabetically sorted list of words (separated by blanks).
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
5 
aue oizer piqoi oizer 
doy oizer hweqlo hweqlo 
hweqlo piqoi aue 
oizer piqoi 
piqoi aue aue 
0
Çıxış verilənləri #1
3
aue oizer piqoi
Mənbə 32nd ACM International Collegiate, 9th Asian Regional Contest in Iran, December 6-7 2007 (Azar 15-16, 1386)