#C5615. Book Reading Order
Book Reading Order
Book Reading Order
You are given a set of books together with their prerequisites. Each book may require that one or more other books be read before it. Your task is to determine a valid reading order such that for every book, all of its prerequisite books are read before it.
If there exists a cycle in the prerequisites (i.e. if it is impossible to satisfy all prerequisites), you should output Not possible
.
This problem is equivalent to finding a topological order in a directed graph. Formally, if there is an edge \(u \rightarrow v\), then book \(u\) must be read before book \(v\).
inputFormat
The input is read from standard input (stdin).
The first line contains an integer (B) representing the number of books. Each of the next (B) lines describes one book. Each such line begins with the book name (a string with no spaces), followed by an integer (P) denoting the number of prerequisites, and then (P) prerequisite book names, all separated by spaces.
outputFormat
The output should be written to standard output (stdout).
If a valid reading order exists, output a single line with the book names in one valid order, separated by a single space. If no valid order exists, output the line Not possible
.## sample
4
Book1 2 Book2 Book3
Book2 1 Book3
Book3 0
Book4 1 Book1
Book3 Book2 Book1 Book4