Diamond Inheritance
Time limit2sMemory limit512 MB
Process class declarations in order, accepting each only if the name is fresh, all parents exist, and no diamond forms.
Problem
You are watching class declarations in an object oriented language similar to C++. Every declaration has the form K : P1 P2 ... Pk ;, where is the name of the new class and are the names of the classes that class inherits. For example, shape : ; declares a class shape that inherits no class, while square : shape rectangle ; declares a class square that inherits the classes shape and rectangle.
If class inherits , class inherits , and so on up to class that inherits , then the classes are all derived from class . The rules of the language forbid circular definitions, so no class is derived from itself. In other words, the class hierarchy is a directed acyclic graph. A diamond in the hierarchy is forbidden too. A diamond is a set of four different classes , , , that satisfies all of the following.
- Classes and are derived from class .
- Class is derived from class and also from class .
- Class is not derived from , and class is not derived from .

Figure 1: A diamond

Figure 2: The hierarchy after all declarations of the first example are processed
You are given declarations to process in the given order, and you decide for each one whether it is declared correctly. A correct declaration is added to the hierarchy, an incorrect one is discarded. The declaration K : P1 P2 ... Pk ; is correct when all of the following hold.
- Class has not been declared yet.
- Classes have all been declared earlier. Because of this condition no class can ever be derived from itself and no cycle can appear in the hierarchy.
- Adding class that inherits keeps the hierarchy in order, that is, not a single diamond is formed.
Write a program that processes the declarations in order as described above and decides the correctness of each one.
Input
The first line contains the integer , the number of declarations ().
Each of the next lines contains a single declaration in the form K : P1 P2 ... Pk ;, where is a list of zero or more classes that class inherits. All names in a single declaration are different. Each class name is a string of at most 10 lowercase letters of the English alphabet. All elements of a declaration (the class names and the characters : and ;) are separated by exactly one space. In each declaration the number of inherited classes satisfies .
Output
Output lines. The th line contains ok if the th declaration is correct, and greska if it is not.
Notes
The first example
- The fourth declaration is incorrect because class
circlewas already defined in the third line. - The sixth declaration is incorrect because class
objecthas not been defined yet. - The eighth declaration is correct. Class
objectis declared by now, and the sixth declaration was discarded, so classrunnableis still undefined. - The tenth declaration is incorrect. Adding it makes the classes
shape,applet,square,runnableform a diamond.
The second example

- The last declaration is incorrect. Adding it makes the classes
x,g,y,dform a diamond. Several other diamonds appear along with it.