Beam me out!

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Wastematerial/article/details/46476911
Description

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 ≤ mn –– 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

Sample #1
3
2
2 3
1
3


Sample #2
3
2
2 3
0


Sample #3
3
2
2 3
2
1 3


Sample #4
3
2
2 3
1
2

Sample Output

Sample #1
PARDON LIMITED

Sample #2
PRISON LIMITED

Sample #3
PARDON UNLIMITED

Sample #4
PRISON UNLIMITED

题意:有N个点,有2*N-1行,接下来一行m,表示第n个点能到多少个点,接下来一行是能到的点。

例如第一个例子2表示点1能到2个点,分别是2和3(单向方向);

如过进入一个点之后就不能到达n点即输出PRISON,否则输出PARDON;

如果能形成一个环使他能无限走下去即输出LIMITED,否则输出UNLIMITED;

思路:邻接表储存好图后直接判圈和搜下即可;

下面贴代码

#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
#define MAX 50005
int vist[MAX],book[MAX],flag ,n,m;;
vector<int> q[MAX];//链表储存
vector<int> p[MAX];
void dfs(int x)//判环和看1能到那些点
{

     vist[x]=1;
    for(int i=0;i<p[x].size();i++)
    {
        if(vist[p[x][i]]==1)//再次回到那个点,形成环
        {
             flag=1;
             continue;
        }
        if(vist[p[x][i]]==0)
           dfs(p[x][i]);
    }
    vist[x]=-1;//返还值表示这个点已经走过
}

void bfs(int x)//广搜
{
     book[x]=1;
     for(int i=0;i<q[x].size();i++)
    {
        if(book[q[x][i]]==0)
            bfs(q[x][i]);
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        flag=0;
        for(int i=1;i<n;i++)
        {
            scanf("%d",&m);
            while(m)
            {
                int x;
                scanf("%d",&x);
                p[i].push_back(x);//正向链表储存
                q[x].push_back(i);//反向链表储存
                m--;
            }
        }
        memset(book,0,sizeof(book));
        memset(vist,0,sizeof(vist));
        dfs(1);//判环和看点1能到那些点
        bfs(n);//广搜看n能到那些点
        int xx=0;
        for(int i=0;i<=n;i++)
          if(vist[i]!=0&&book[i]==0)//如果1能到的点而n不能到的点即进入一个点之后就不能到达n点
                    xx=1;
        if(xx==1)
            printf("PRISON ");
        else
            printf("PARDON ");
        if(flag==1)
            printf("UNLIMITED\n");
        else
            printf("LIMITED\n");
        for(int i=1; i<=n; i++)
        {
            p[i].clear();
            q[i].clear();
        }
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/Wastematerial/article/details/46476911
me