CSUOJ 1526 Beam me out!

Beam me out!

King Remark, first of his name, is a benign ruler and every wrongdoer gets a second chance after repenting his crimes in the Great Maze!

Today’s delinquent is a renowned computer scientist, but his fame didn’t do him any good after he declined to do research 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 may 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: After the delinquent says the magic words “I was wrong and will never disappoint king Remark again!” he will be immediately beamed to the next room. It will 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 room 1 and hopes to get to the throne room n in which he will receive his pardon. If he ends up in a room, whose list of goal rooms is empty, his tour is over; through he could surely say the magic words again and again – that would not hurt, but would not help him either.

Great king Remark, as most of the kings, doesn’t like surprises and summoned you to answer two questions: Is it guaranteed, that the criminal will get to the throne room and is there a limit of beaming operations after which the game is over for sure.

You know better, than to disappoint the great king with a wrong answer or no answer at all, don’t you?

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, two lines will follow representing the corresponding list of the goal rooms (in order 1 to n − 1). Bear in mind, that after reaching the throne room n the quest is over. Thus, the list of the throne room is not a part of the input.

The first of these two lines will contain an integer 0 ≤ m n – the number of goal rooms on the list. The second line will contain a list of m goal rooms or an empty string, if m = 0. Each list will be sorted in strictly ascending order (this implies every number on the list will be unique) and consist from integers between 1 and n, inclusive.

The total number of goal rooms summed over all lists will not exceed 106.

Output

For each test case a line consisting of two words:

  • the first word must be “PARDON”, if the probability for the prisoner getting to the throne room during his random walk is 100%, or “PRISON” otherwise.
  • the second word must be “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

 

解题:两个判断,第一个看点1所能到达的点,这些点是否都能到达n,yes?pardon:prison

第二个,看看从点1出发,能不能找到环,yes?unlimited:limited

 

 1 #include <bits/stdc++.h>

 2 using namespace std;

 3 const int maxn = 100000;

 4 int hd[maxn],hd2[maxn],st[maxn],tot,n;

 5 bool va[maxn],vb[maxn];

 6 struct arc{

 7     int to,next;

 8     arc(int x = 0,int y = -1){

 9         to = x;

10         next = y;

11     }

12 }e[2000000];

13 void add(int *head,int u,int v){

14     e[tot] = arc(v,head[u]);

15     head[u] = tot++;

16 }

17 void bfs(int s,int *head,bool vis[maxn]){

18     queue<int>q;

19     memset(vis,false,sizeof(false)*maxn);

20     q.push(s);

21     vis[s] = true;

22     while(!q.empty()){

23         int u = q.front();

24         q.pop();

25         for(int i = head[u]; ~i; i = e[i].next){

26             if(!vis[e[i].to]){

27                 vis[e[i].to] = true;

28                 q.push(e[i].to);

29             }

30         }

31     }

32 }

33 bool cycle(int u,int *head){

34     st[u] = 1;

35     for(int i = head[u]; ~i; i = e[i].next){

36         if(st[e[i].to] == 1) return true;

37         if(!st[e[i].to] && cycle(e[i].to,head)) return true;

38     }

39     st[u] = 2;

40     return false;

41 }

42 int main(){

43     int m,v;

44     while(~scanf("%d",&n)){

45         memset(hd2,-1,sizeof(hd2));

46         memset(hd,-1,sizeof(hd));

47         memset(st,0,sizeof(st));

48         tot = 0;

49         for(int i = 1; i < n; ++i){

50             scanf("%d",&m);

51             while(m--){

52                 scanf("%d",&v);

53                 add(hd,v,i);

54                 add(hd2,i,v);

55             }

56         }

57         bool cyc = cycle(1,hd2);

58         bfs(n,hd,va);

59         bfs(1,hd2,vb);

60         int ans = 0;

61         for(int i = 1; i <= n; ++i) ans += vb[i]&&!va[i];

62         printf("%s %s\n",ans == 0?"PARDON":"PRISON",cyc?"UNLIMITED":"LIMITED");

63     }

64     return 0;

65 }
View Code

 

 

你可能感兴趣的:(out)