Process class declarations in order, accepting each only if the name is fresh, all parents exist, and no diamond forms.
Medium7GraphDFSHash mapNo attempts yetTime limit2sMemory limit512 MBYou are watching class declarations in an object oriented language similar to C++. Every declaration has the form K : P1 P2 ... Pk ;, where K is the name of the new class and P1,P2,…,Pk are the names of the classes that class K 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 K1 inherits K2, class K2 inherits K3, and so on up to class Km−1 that inherits Km, then the classes K1,K2,…,Km−1 are all derived from class Km. 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 A, B, X, Y that satisfies all of the following.

Figure 1: A diamond

Figure 2: The hierarchy after all declarations of the first example are processed
You are given n 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.
Write a program that processes the declarations in order as described above and decides the correctness of each one.
The first line contains the integer n, the number of declarations (1≤n≤1000).
Each of the next n lines contains a single declaration in the form K : P1 P2 ... Pk ;, where P1,P2,…,Pk is a list of zero or more classes that class K inherits. All names K,P1,P2,…,Pk 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 k satisfies 0≤k≤1000.
Output n lines. The ith line contains ok if the ith declaration is correct, and greska if it is not.
The first example
circle was already defined in the third line.object has not been defined yet.object is declared by now, and the sixth declaration was discarded, so class runnable is still undefined.shape, applet, square, runnable form a diamond.The second example

x, g, y, d form a diamond. Several other diamonds appear along with it.