CCC '18 J5 - Choose your own path

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type
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 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 \le N \le 10\,000), the number of pages in the book. Each of the next N lines contain an integer M_i (1 \le i \le N; 0 \le M_i \le N), which is the number of different options from page i, followed by M_i 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 M_1 + M_2 + \dots + M_N is at most 10\,000.

If M_i = 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.

Note that you always begin the book on page 1.

For 4 of the available 15 marks, N \le 100, M_i \le 10 for 1 \le i \le N.

For an additional 3 of the available 15 marks, the book is guaranteed to have no cycles.

For an additional 4 of the available 15 marks, N \le 1\,000, M_i \le 25 for 1 \le i \le N.

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
2 2 3
0
0

Sample Output 1

Y
2

Explanation for Sample Output 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 \to 2 and 1 \to 3, each of which is 2 pages in length.

Sample Input 2

3
2 2 3
0
1 1

Sample Output 2

Y
2

Explanation for Sample Output 2

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


Comments


  • 0
    踏雪寻梅  commented on Feb. 19, 2024, 9:09 p.m. edit 2

    Can someone take a look at my code? I don't know what's wrong with my code. Somebody help me please. :/

    Nevermind, I misread the question. I thought I needed to check if the end page was reachable..................


  • 1
    info5d3c4a0c4a864cbe  commented on Dec. 29, 2023, 7:21 p.m.

    Breadth-first algorithm can also do it


    • 0
      thunder200911133  commented on March 17, 2024, 8:25 p.m.

      How to do DFS? The only way I can think of is using BFS


    • 0
      longcow  commented on Feb. 26, 2024, 5:33 a.m.

      yep just use a hashset to avoid the whole endless repition thing


  • 14
    vishnus  commented on Aug. 26, 2020, 7:40 p.m. edit 2

    I've been stuck on this problem for quite a while now... I am just not sure how to implement the shortest path part of this problem... Can someone please help? Thanks.

    I kinda solved it? I used Dijkstra but for the odd test case it is failing... My submission is at https://dmoj.ca/submission/2986907. Thanks!

    Thanks everyone. I solved it. I was just mishandling the input... I included the first number on each page line (supposed to represent the number of pages you can access) as a page you can access, causing this issue.


  • 0
    Ibby  commented on Jan. 21, 2020, 1:37 p.m.

    I keep getting RTE on the last batch. Can someone see what's wrong with my code? https://dmoj.ca/src/1834183


    • 3
      mdu  commented on Jan. 22, 2020, 1:34 a.m. edit 4

      a vector of N^2 ints, where N <= 10,000, can be up to 4e8 bytes of memory + a bit extra, which is far beyond the stack space that you have available, and in fact larger than the memory limit of the problem.

      Declare large data structures globally (see the tips section of About, at the top). That being said, your vector will never need to get that big.


      • 1
        Ibby  commented on Jan. 22, 2020, 9:50 p.m.

        I figured it out, thanks for the help!


      • 1
        Dingledooper  commented on Jan. 22, 2020, 2:26 a.m.

        I also want to add that vector elements are allocated on the heap, regardless of whether it is declared in or out of the main function. The allocation tip only applies to arrays.


    • 1
      Dingledooper  commented on Jan. 21, 2020, 11:24 p.m.

      Why do you resize the vector to n*n? I think n+1 suffices.


  • -5
    kittybunny  commented on July 31, 2018, 12:15 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 15
      998244353  commented on Aug. 29, 2018, 2:13 p.m.

      If you are lazy and you don't want to check the endless loop, you can try Dijkstra instead of bfs to solve this problem😂! To my surprise my code didn't TLE...