Beam Me out!
King Remark, first of his name, was a benign ruler and every wrongdoer gets a second chance after repenting he crimes in t He great maze!
Today's delinquent is a renowned computer scientist, but he fame didn ' t does him any good after he declined On the so called and Soon-to-be-famous Remark ' s algorithms! Those strange randomized algorithms may run indefinitely long (or even never terminate) and may or could not produce a right Answer if terminated.
Handily, the great Maze got recently a major upgrade with the newest beaming technology which made all doors obsolete:aft Er the delinquent says the magic words "I was wrong and would never disappoint King Remark again!" he'll be immediately b Eamed to the next. It'll be chosen randomly from a list of possible goal rooms.
The great Maze consists of n rooms numbered 1 to N. Every detainee starts his quest for pardon in-the-1 and hopes to get-to-the-throne- N -which he'll recei ve his pardon. If He ends up in a class, whose list of goal rooms is empty, and his tour was over; Through he could surely say the magic words again and again–that would not hurt, but would does help him either.
Great King Remark, as most of the Kings, doesn ' t like surprises and summoned you to answer, questions:is it guaranteed , that the criminal would get to the throne-A and is there a limit of beaming operations after which the game was over fo R sure.
Know better, than to disappoint the great king with a wrong answer or no answer at all, don ' t?
Input
The input contains a single test case. It starts with a line consisting of an integer 2≤ n ≤50000–the number of rooms in the great Maze. For each of the rooms 1 to n − 1, lines would follow representing the corresponding list of the goal rooms (in Order 1 to n − 1). Reaching the throne, the quest is over. Thus, the list of the throne.
The first of these lines would contain an integer 0≤ m ≤ n , haven number of goal rooms on the list. The second line would contain a list of m goal rooms or an empty string, if m = 0. Each list would be sorted in strictly ascending order (this implies every number on the list would be unique) and consist fr Om integers between 1 and N, inclusive.
The total number of goal rooms summed through all lists would not exceed 106.
Output
For each test case a line consisting of words:
- The first word must be ' pardon ', if the probability for the prisoner getting to the throne, the during he random walk is 100%, or "PRISON" otherwise.
- The second word must is "LIMITED", if a limit for the number of beaming operations exists, or "UNLIMITED" otherwise.
Sample Input I |
Sample Output I |
3 2 2 3 1 3 |
Pardon LIMITED |
Sample Input II |
Sample Output II |
3 2 2 3 0 |
PRISON LIMITED |
Sample Input iii |
Sample Output iii |
3 2 2 3 2 1 3 |
Pardon UNLIMITED |
Sample Input IV |
Sample Output IV |
3 2 2 3 1 2 |
prison UNLIMITED |
Problem solving: Two judgments, the first spot 1 points to reach, whether these points can reach N,yes?pardon:prison
The second one, see from point 1, can not find the ring, yes?unlimited:limited
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =100000;4 intHd[maxn],hd2[maxn],st[maxn],tot,n;5 BOOLVA[MAXN],VB[MAXN];6 structarc{7 intTo,next;8Arcintx =0,inty =-1){9to =x;TenNext =y; One } A}e[2000000]; - voidAddint*head,intUintv) { -E[tot] =arc (V,head[u]); theHead[u] = tot++; - } - voidBFsintSint*head,BOOLVIS[MAXN]) { -queue<int>Q; +memset (Vis,false,sizeof(false)*MAXN); - Q.push (s); +Vis[s] =true; A while(!Q.empty ()) { at intU =Q.front (); - Q.pop (); - for(inti = Head[u]; ~i; i =E[i].next) { - if(!Vis[e[i].to]) { -Vis[e[i].to] =true; - Q.push (e[i].to); in } - } to } + } - BOOLCycleintUint*head) { theSt[u] =1; * for(inti = Head[u]; ~i; i =E[i].next) { $ if(St[e[i].to] = =1)return true;Panax Notoginseng if(!st[e[i].to] && cycle (e[i].to,head))return true; - } theSt[u] =2; + return false; A } the intMain () { + intm,v; - while(~SCANF ("%d",&N)) { $memset (hd2,-1,sizeof(HD2)); $memset (hd,-1,sizeof(HD)); -memset (St,0,sizeof(ST)); -tot =0; the for(inti =1; I < n; ++i) { -scanf"%d",&m);Wuyi while(m--){ thescanf"%d",&v); - Add (hd,v,i); Wu Add (hd2,i,v); - } About } $ BOOLCyc = cycle (1, HD2); - BFS (N,HD,VA); -BFs1, HD2,VB); - intAns =0; A for(inti =1; I <= N; ++i) ans + = vb[i]&&!Va[i]; +printf"%s%s\n", ans = =0?"Pardon":"PRISON", Cyc?"UNLIMITED":"LIMITED"); the } - return 0; $}
View Code
Csuoj 1526 Beam Me out!