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设备.在传递数据的过 ...
随机推荐
- HDU 5274(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- nj11--http
概念:Node.js提供了http模块.其中封装了一个高效的HTTP服务器和一个建议的HTTP客户端. http.server是一个基于事件的HTTP服务器.内部有C++实现.接口由JavaScrip ...
- 1 QT设置背景色
首先需要添加头文件#include <QPalette> 然后接下来这样操作: 控件.setAutoFillBackground(true); //这里的控件就是你要设置背景颜色的控件 Q ...
- 一天一个算法:求Sn=a+aa+aaa+…+aa…a之和
/* 求Sn=a+aa+aaa+…+aa…a之值,其中a是一个数字. 例如:2+22+222+…+22222(此时n=5),n由键盘输入.*/ void Function3() { int a,n,s ...
- Ionic2中的Navigation.md
1. 概述 为了能够得到同原生应用类似的导航效果,Ionic创建了几个navagation组件来实现pages之间的导航操作,这种导航跟原生Angular2中的route机制是不一样的,我们可以借助于 ...
- theano import error (win10 python2.7)
因为项目需要,在win10-64位电脑上配置theano.但是一直有 import error的错误,找不到解决方法.作为一个python新手,实在搞不定,请大家不吝赐教!小女子不胜感激! 按照网上的 ...
- codeforces 527 C Glass Carving
Glass Carving time limit per test 2 seconds Leonid wants to become a glass carver (the person who cr ...
- Debian9.5下sftp配置和scp用法
基于 ssh 的 sftp 服务相比 ftp 有更好的安全性(非明文帐号密码传输)和方便的权限管理(限制用户的活动目录). 1.如果只想让某些用户只能使用 sftp 操作文件, 而不能通过ssh进行服 ...
- vue 学习笔记1 入门
可以在 JSFiddle上在线学习vue 注意:所演示的示例,都是在JS中将Vue实例绑定至HTML中的指定元素,然后再通过Vue实例中data内的属性或者methods中的方法,来对所绑定元素的子元 ...
- opencv3.4.1和vs2017配置
官网下载opencv,双击之后会将文件提取出来,提取出来的文件放在一个合适的位置(选个好地方,不要乱改,环境的配置依赖于这个目录),我放在了D:\program下 配置环境变量: 右键此电脑--> ...