Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USING JAVA Problem Description There is a genre of fiction called choose your own adventure books. These books allow the reader to make choices for

USING JAVA

Problem Description

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 47, and if they choose no, they are directed to continue reading on page 18. 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:

1. ensure that every page can be reached otherwise, there is no reason to pay to print a page which no one can ever read;

2. 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.

Input Specification

The first line of input contains N (1 N 10000), the number of pages in the book. Each ofthenextN linescontainanintegerMi (1 i N;0 Mi N),whichisthenumber of different options from page i, followed by Mi space-separated integers in the range from 1 to N, corresponding to each of the pages to go to next from page i. It will also be the case M1 +M2 ++MN isatmost10000.

If Mi = 0, then page i is an ending page (i.e., there are no choices from that page). There will be at least one ending page in the book.

Output Specification

The output will be two lines. The first line will contain Y if all pages are reachable, and N otherwise.

The last line will contain a non-negative integer K, 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 223 0 0

Output for Sample Input 1

Y 2

Explanation of Output for Sample Input 1

Since we start on page 1, and can reach both page 2 and page 3, all pages are reachable. The only paths in the book are 1 2 and 1 3, each of which is 2 pages in length.

Sample Input 2

3 223 0 11

Output for Sample Input 2

Y 2

Explanation of Output for Sample Input 2

Every page is reachable, since from page 1, we can reach pages 2 and 3. The shortest path is the path 1 2, which contains two pages.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions