Beam me out!
Time limit1sMemory limit256 MB
Decide whether a random walk from room 1 reaches room n with certainty and whether every possible walk ends within a bounded number of steps.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Topological sort
- Solved
- No attempts yet
Problem
King Remark is a lenient ruler. A wrongdoer who repents his crimes gets a second chance in the Great Maze.
Today's delinquent is a well known computer scientist. His fame did him no good after he declined to study the randomized algorithms that king Remark invented. Those algorithms may run for a very long time, may never stop at all, and are not guaranteed to give a right answer even when they do stop.
The Great Maze was rebuilt with the newest beaming technology, which made all doors unnecessary. Once the delinquent says the magic words "I was wrong and will never disappoint king Remark again!", he is beamed to the next room at once. That room is chosen at random from the list of goal rooms written in the room he is standing in.
The Great Maze has rooms numbered to . Every detainee starts in room and receives his pardon once he reaches the throne room . If he ends up in a room whose list of goal rooms is empty, his tour is over there. Saying the magic words again in that room does not hurt him, but it does not help him either.
King Remark dislikes surprises and asks two questions. Is the delinquent guaranteed to reach the throne room, and is there a limit on the number of beaming operations after which the game is over for sure?
Every room on a list is chosen with probability greater than .
Input
The input contains a single test case.
The first line contains the number of rooms in the Great Maze, ().
Two lines follow for each of the rooms to , in that order. Reaching the throne room ends the quest, so the list of room is not part of the input.
The first of the two lines contains the number of goal rooms on the list, (). The second line contains the goal rooms, and it is an empty line if . Every list consists of integers between and , inclusive, and is sorted in strictly increasing order, so no room appears twice on the same list.
The total number of goal rooms summed over all lists does not exceed .
Output
Print two words on one line, separated by a space.
- The first word is
PARDONif the probability that the delinquent reaches the throne room during his random walk is 100%, andPRISONotherwise. - The second word is
LIMITEDif a limit on the number of beaming operations exists, andUNLIMITEDotherwise.