Canadian Computing Competition: 2018 Stage 1, Junior #5
There is a genre of fiction called choose your own adventure books. These books allow the reader to make choices for the characters which alters the outcome of the story.
For example, after reading the first page of a book, the reader may be asked a choice, such as "Do you pick up the rock?" If the reader answers "yes", they are directed to continue reading on page , and if they choose "no", they are directed to continue reading on page . On each of those pages, they have further choices, and so on, throughout the book. Some pages do not have any choices, and thus these are the "ending" pages of that version of the story. There may be many such ending pages in the book, some of which are good (e.g., the hero finds treasure) and others which are not (e.g., the hero finds a leftover sandwich from 2001).
You are the editor of one of these books, and you must examine two features of the choose your own adventure book:
- ensure that every page can be reached – otherwise, there is no reason to pay to print a page which no one can ever read;
- find the shortest path, so that readers will know what the shortest amount of time they need to finish one version of the story.
Given a description of the book, examine these two features.
The first line of input contains , the number of pages in the book. Each of the next lines contain an integer , which is the number of different options from page , followed by space-separated integers in the range from to , corresponding to each of the pages to go to next from page . It will also be the case is at most .
If , then page is an ending page (i.e., there are no choices from that page). There will be at least one ending page in the book.
Note that you always begin the book on page .
For of the available marks, , for .
For an additional of the available marks, the book is guaranteed to have no cycles.
For an additional of the available marks, , for .
The output will be two lines. The first line will contain
Y if all pages are reachable, and
The last line will contain a non-negative integer , which is the shortest path a reader can take while reading this book. There will always be a finite shortest path.
Sample Input 1
3 2 2 3 0 0
Sample Output 1
Explanation for Sample Output 1
Since we start on page , and can reach both page and page , all pages are reachable. The only paths in the book are and , each of which is pages in length.
Sample Input 2
3 2 2 3 0 1 1
Sample Output 2
Explanation for Sample Output 2
Every page is reachable, since from page , we can reach pages and . The shortest path is the path , which contains two pages.