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. Woody的Python学习笔记4

    Python模块 Import语句 想要使用Python源文件,仅仅须要在还有一个源文件中运行import语句.语法例如以下: import module1 当解释器遇到import语句.假设模块在当 ...

  2. 怎样选择正确的HTTP状态码

    本文来源于我在InfoQ中文站翻译的文章.原文地址是:http://www.infoq.com/cn/news/2015/12/how-to-choose-http-status-code 众所周知. ...

  3. BZOJ4477: [Jsoi2015]字符串树

    [传送门:BZOJ4477] 简要题意: 给出一棵n个点的树,树上的边都代表一个字符串,给出Q个询问,每个询问输入x,y和字符串s,求出x到y的路径上以s为前缀的字符串个数 题解: 自己yy了一波可持 ...

  4. Android textView开头空两格问题,排版缩进2个汉字

    一般为了排版,textView中字符段落开头一般都会空两格显示,如下图 但是如果你靠敲击空格来解决那就错了,那样在不同的屏幕上显示会差异,完美的解决方法是用转义字符”\t“,在段首加\t\t就解决.加 ...

  5. C# 使用指针将不同值类型赋值到字节数组中

    C#指针操作字节数组 Demo(以添加short类型的值为例): //bytes:目标字节数组; offset:目标在字节数组的位置; value:添加的类型值public static unsafe ...

  6. UVa 1599 Ideal Path【BFS】

    题意:给出n个点,m条边,每条边上涂有一个颜色,求从节点1到节点n的最短路径,如果最短路径有多条,要求经过的边上的颜色的字典序最小 紫书的思路:第一次从终点bfs,求出各个节点到终点的最短距离, 第二 ...

  7. objc_clear_deallocating 与弱引用

    void *objc_destructInstance(id obj){ if (obj) { Class isa_gen = _object_getClass(obj); class_t *isa ...

  8. Blink Coordinate Spaces

    Blink Coordinate Spaces Blink Coordinate Spaces Types of Zoom There are two types of zoom in Chromiu ...

  9. mysql主从复制主服务器日志格式的区别

        statement(语句级别,从服务器直接把语句拿来执行):            影响一大片(插入很多条或修改很多条),就适合用 statement       row(行级别,从服务器直接 ...

  10. [USACO14FEB]路障Roadblock

    题目:洛谷P2176. 题目大意:有n个点m条无向边,一个人要从1走到n,他会走最短路.现在可以让一条边的长度翻倍,求翻倍后这个人要多走多少距离. 解题思路:首先可以知道,翻倍肯定是在最短路上的某条边 ...