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. POJ 3074 Sudoku DLX精确覆盖

    DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: ...

  2. 【通信框架】Google的开源通信框架protobuf概述

    在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 一.作用 protobuf(Protocol Buffers)是Google内部 ...

  3. What's the difference between Unicode and UTF-8?

    https://stackoverflow.com/questions/3951722/whats-the-difference-between-unicode-and-utf-8 If asked ...

  4. CodeWars for JavaScript

    SubClass https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes Sub classing with ...

  5. 用jquery给select加选中事件

    select在前端开发过程中很常用,现在我们要实现一个效果,那就是选中select中的某一项,执行事件,本来自己没怎么接触过这些,最后网上找了一些资料,自己研究了一下,把方法分享给大家,大家如果有需要 ...

  6. 区间dp学习笔记

    怎么办,膜你赛要挂惨了,下午我还在学区间\(dp\)! 不管怎么样,计划不能打乱\(4\)不\(4\).. 区间dp 模板 为啥我一开始就先弄模板呢?因为这东西看模板就能看懂... for(int i ...

  7. Boom

    紧急事件!战场内被敌军埋放了n枚炸弹! 我军情报部门通过技术手段,掌握了这些炸弹的信息.这些炸弹很特殊,每枚炸弹的波及区域是一个矩形.第i枚炸弹的波及区域是以点(xi1,yi1)为左下角,点(xi2, ...

  8. GoldenGate V11.1数据复制限制

    以下对goldengate数据复制的限制情况进行说明. 不支持文件等非结构化数据复制 GoldenGate依赖对于数据库日志的解析获取数据变化,因此只能支持数据库中的数据变化复制,无法支持文件等非结构 ...

  9. ML words

    samples:样本 multi-dimensional entry / multivariate data:多属性记录 features:特征,属性 supervised learning:监督学习 ...

  10. php八大设计模式之工厂模式

    简单点来说,就是用到什么类,就去实例化对应的类.比如:php 可能连接 mysql,可能连接 sqlserver,也可能是 oracle 数据库,可以动态的去链接. 书籍<php权威编程> ...