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
#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!的更多相关文章
- Beam Search(集束搜索/束搜索)
找遍百度也没有找到关于Beam Search的详细解释,只有一些比较泛泛的讲解,于是有了这篇博文. 首先给出wiki地址:http://en.wikipedia.org/wiki/Beam_searc ...
- 1526. Martian Plates
http://acm.timus.ru/problem.aspx?space=1&num=1526 题目大意: 可以从n个碟子中任选一个放在桌子上(不断往上放),也可以把桌子上最顶端的盘子拿走 ...
- csuoj 1511: 残缺的棋盘
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec 内存限制: 128 MB 题目描述 输入 ...
- 关于Beam Search
Wiki定义:In computer science, beam search is a heuristic search algorithm that explores a graph by exp ...
- Erlang 虚拟机 BEAM 指令集之内存管理相关的指令
翻看 BEAM 虚拟机指令集的时候(在编译器源码目录下:lib/compiler/src/genop.tab),会发现有一些和内存分配/解除分配相关的指令,如下所示: allocate StackNe ...
- hrbust oj 1526+2028 树状数组
冒泡排序中 如果一个数的后面的某个数和这个数不符合排序规则 那么这个数就会在未来的某次冒泡中与那个数进行交换 这里用到了 树状数组求逆序数的办法来做 需要注意的是2028并不可以改完数组大小后直接套1 ...
- Why Apache Beam? A data Artisans perspective
https://cloud.google.com/dataflow/blog/dataflow-beam-and-spark-comparison https://github.com/apache/ ...
- hdu 1526(最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1526 思路:floyd求传递闭包,然后就是最大匹配了,不过一开始输入没看清,被坑了将近2个小时. #i ...
- 14、NFC技术:使用Android Beam技术传输文本
Android Beam的基本理念 Android Beam的基本理念就是两部(只能是两部)NFC设备靠近时(一般是背靠背),通过触摸一部NFC设备的屏幕,将数据推向另外一部NFC设备.在传递数据的过 ...
随机推荐
- IP地址的规划和设计方法(二)
五,IP地址规划方法 (1)IP地址规划的基本步骤 网络地址规划须要按下面6步进行: a)推断用户对网络与主机数的需求: ...
- 6. MongoDB——Java操作(增删改查)
转自:https://blog.csdn.net/kai402458953/article/details/79626148 import java.net.UnknownHostException; ...
- App server 与 Web server之间的区别
原文: http://www.javaworld.com/javaqa/2002-08/01-qa-0823-appvswebserver.html 简单来说,web服务器提供页面给浏览器,而app服 ...
- java9新特性-13-增强的 Stream API
1.使用说明 Java 的 Steam API 是java标准库最好的改进之一,让开发者能够快速运算,从而能够有效的利用数据并行计算.Java 8 提供的 Steam 能够利用多核架构实现声明式的数据 ...
- SharePoint InfoPath 保存无法发布问题
设计完表单以后提示以下错误 错误描述 InfoPath无法保存下列表单:******* 此文档库已被重命名或删除,或者网络问题导致文件无法保存.如果此问题持续存在,请于网络管理员联系. 可参考网站 & ...
- SSRS 报表 日期类表达式
一.如何填写表达式 右键点击单元格-表达式 二.表达式 当月1号 =DateSerial(DatePart("yyyy",Now()), DatePart("m" ...
- Storm Zookeeper
本文记录了storm 1.1.0 在zookeeper中保存的信息. 下面的图是在[4]的基础上进行修改的. /-storm -- storm在zookeeper上的根目录 | |-/assignme ...
- Android RecyclerView和ScrollView嵌套使用
我们的recyclerView有多个layoutmanager,通过重写layoutmanager的方法就可以让recyclerView和ScrollView嵌套了.但是请注意,如果recyclerV ...
- java 8 , merge()
import java.util.HashMap; import java.util.Map; public class j8merge { public static void main(Strin ...
- hdu 2018 - 递推
dp[i][1..4] 第i年时年龄为1234的牛的数目 */ #include <cstdio> #include <cstring> ; ]; int main(){ me ...