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

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int hd[maxn],hd2[maxn],st[maxn],tot,n;
bool va[maxn],vb[maxn];
struct arc{
int to,next;
arc(int x = ,int y = -){
to = x;
next = y;
}
}e[];
void add(int *head,int u,int v){
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void bfs(int s,int *head,bool vis[maxn]){
queue<int>q;
memset(vis,false,sizeof(false)*maxn);
q.push(s);
vis[s] = true;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(!vis[e[i].to]){
vis[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
bool cycle(int u,int *head){
st[u] = ;
for(int i = head[u]; ~i; i = e[i].next){
if(st[e[i].to] == ) return true;
if(!st[e[i].to] && cycle(e[i].to,head)) return true;
}
st[u] = ;
return false;
}
int main(){
int m,v;
while(~scanf("%d",&n)){
memset(hd2,-,sizeof(hd2));
memset(hd,-,sizeof(hd));
memset(st,,sizeof(st));
tot = ;
for(int i = ; i < n; ++i){
scanf("%d",&m);
while(m--){
scanf("%d",&v);
add(hd,v,i);
add(hd2,i,v);
}
}
bool cyc = cycle(,hd2);
bfs(n,hd,va);
bfs(,hd2,vb);
int ans = ;
for(int i = ; i <= n; ++i) ans += vb[i]&&!va[i];
printf("%s %s\n",ans == ?"PARDON":"PRISON",cyc?"UNLIMITED":"LIMITED");
}
return ;
}

CSUOJ 1526 Beam me out!的更多相关文章

  1. Beam Search(集束搜索/束搜索)

    找遍百度也没有找到关于Beam Search的详细解释,只有一些比较泛泛的讲解,于是有了这篇博文. 首先给出wiki地址:http://en.wikipedia.org/wiki/Beam_searc ...

  2. 1526. Martian Plates

    http://acm.timus.ru/problem.aspx?space=1&num=1526 题目大意: 可以从n个碟子中任选一个放在桌子上(不断往上放),也可以把桌子上最顶端的盘子拿走 ...

  3. csuoj 1511: 残缺的棋盘

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec  内存限制: 128 MB 题目描述 输入 ...

  4. 关于Beam Search

    Wiki定义:In computer science, beam search is a heuristic search algorithm that explores a graph by exp ...

  5. Erlang 虚拟机 BEAM 指令集之内存管理相关的指令

    翻看 BEAM 虚拟机指令集的时候(在编译器源码目录下:lib/compiler/src/genop.tab),会发现有一些和内存分配/解除分配相关的指令,如下所示: allocate StackNe ...

  6. hrbust oj 1526+2028 树状数组

    冒泡排序中 如果一个数的后面的某个数和这个数不符合排序规则 那么这个数就会在未来的某次冒泡中与那个数进行交换 这里用到了 树状数组求逆序数的办法来做 需要注意的是2028并不可以改完数组大小后直接套1 ...

  7. Why Apache Beam? A data Artisans perspective

    https://cloud.google.com/dataflow/blog/dataflow-beam-and-spark-comparison https://github.com/apache/ ...

  8. hdu 1526(最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1526 思路:floyd求传递闭包,然后就是最大匹配了,不过一开始输入没看清,被坑了将近2个小时. #i ...

  9. 14、NFC技术:使用Android Beam技术传输文本

    Android Beam的基本理念 Android Beam的基本理念就是两部(只能是两部)NFC设备靠近时(一般是背靠背),通过触摸一部NFC设备的屏幕,将数据推向另外一部NFC设备.在传递数据的过 ...

随机推荐

  1. 主程的晋升攻略(4):TCP、消息分包和协议设计

    在<主程的晋升攻略(3):IP.DNS和CDN>中,一次网络请求经过DNS解析知道了目的IP,如今就要发出网络包,这里我们说一说TCP的相关话题. TCP是一种流式协议 讲网络编程的教科书 ...

  2. POJ 2528 Mayor&#39;s posters 离散化和线段树题解

    本题就是要往墙上贴海报,问最后有多少可见的海报. 事实上本题的难点并非线段树,而是离散化. 由于数据非常大,直接按原始数据计算那么就会爆内存和时间的. 故此须要把数据离散化. 比方有海报1 6   7 ...

  3. linux内核设计的艺术--系统启动第一步

    计算机究竟是如何执行起来的呢,在我学习计算机的时候一直不是非常明确,可是近期借了本<linux内核设计的艺术>算是知道了计算机从按开机到启动操作系统之间究竟做了些什么. 这本书刚開始介绍的 ...

  4. js 实现 水仙花数

    水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身.(例如:1^3 + 5^3+ 3^3 = 153) <!DOCTYPE html><html ...

  5. java9新特性-7-语法改进:接口的私有方法

    1.官方Feature 213: Milling Project Coin Support for private methods in interfaces was briefly in consi ...

  6. 使用ShareSDK分享-图片的链接

    微信中使用ShareSDK分享,需要申请微信开放平台账号,并且以微信中的声明的应用签名打包程序. private void showShare(String url, String title, St ...

  7. python BeautifulSoup 获取页面多个子节点中的各个节点的内容

    页面html格式为 <tr bgcolor="#7bb5de"><td style="border-bottom: 1px solid #C9D8AD& ...

  8. How Javascript works (Javascript工作原理) (十五) 类和继承及 Babel 和 TypeScript 代码转换探秘

    个人总结:读完这篇文章需要15分钟,文章主要讲解了Babel和TypeScript的工作原理,(例如对es6 类的转换,是将原始es6代码转换为es5代码,这些代码中包含着类似于 _classCall ...

  9. linux驱动编译时候出现的问题

    1.在编译驱动的时候,提示错误,找不到<asm/xxxx.h>这些类的头文件? 答:因为在内核编译的时候,会在内核目录的include中创建一个asm文件再软链接到对应的一些架构.比如我当 ...

  10. ln---创建链接

    ln命令用来为文件创件连接,连接类型分为硬连接和符号连接两种,默认的连接类型是硬连接.如果要创建符号连接必须使用"-s"选项. 注意:符号链接文件不是一个独立的文件,它的许多属性依 ...