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?

输入

The input contains a single test case. It starts with a line consisting of an integer 2 ≤ n ≤ 50 000 – 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.

输出

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.

样例输入

3
2
2 3
1
3

样例输出

PARDON LIMITED
分析:有向无环图判定及bfs;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=5e4+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,vis[maxn],vis1[maxn],tot,tot1,h[maxn],u[maxn];
struct node
{
int to,nxt;
}e[],f[];
void add(int x,int y)
{
tot++;
e[tot].to=y;
e[tot].nxt=h[x];
h[x]=tot;
tot1++;
f[tot1].to=x;
f[tot1].nxt=u[y];
u[y]=tot1;
}
bool flag,flag1;
void dfs(int now)
{
if(flag)return;
vis[now]=-;
for(int i=h[now];i;i=e[i].nxt)
{
int x=e[i].to;
if(vis[x]==-)
{
flag=true;
return;
}
else if(!vis[x])dfs(x);
}
vis[now]=;
}
void bfs()
{
queue<int>p;p.push();vis[]=;
while(!p.empty())
{
int q=p.front();p.pop();
for(int i=h[q];i;i=e[i].nxt)
{
int x=e[i].to;
if(!vis[x])p.push(x);vis[x]=;
}
}
p.push(n);
vis1[n]=;
while(!p.empty())
{
int q=p.front();p.pop();
for(int i=u[q];i;i=f[i].nxt)
{
int x=f[i].to;
if(!vis1[x])
{
p.push(x);vis1[x]=;
}
}
}
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n-)
{
scanf("%d",&m);
while(m--)
{
scanf("%d",&j);
add(i,j);
}
}
dfs();
memset(vis,,sizeof(vis));
bfs();
rep(i,,n)if(vis[i]&&!vis1[i])flag1=true;
if(!flag1)printf("PARDON ");
else printf("PRISON ");
if(flag)puts("UNLIMITED");
else puts("LIMITED");
//system("Pause");
return ;
}

Beam me out!的更多相关文章

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

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

  2. 关于Beam Search

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

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

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

  4. Why Apache Beam? A data Artisans perspective

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

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

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

  6. NFC(13)使用Android Beam技术传输文件

    注意 Android Beam技术传输文件时nfc只负责连接两个手机,而传输文件实际是用蓝牙模块.且目前接收文件功能只是系统完成,不用自写个接收程序. 传输文件相关的重要api 从Android4.1 ...

  7. NFC(12)使用Android Beam技术传输文本数据及它是什么

    Android Beam技术是什么 Android Beam的基本理念就是两部(只能是1对1,不可像蓝牙那样1对多)NFC设备靠近时(一般是背靠背),通过触摸一部NFC设备的屏幕,将数据推向另外一部N ...

  8. Android Beam 详细实现步骤

    前言 最近没怎么写东西了,主要是在了解Beam这个东东.这方面的教程真的非常有限,找了不少资料目前还没看到一篇能够让一个新手看一遍就知道实现一个Beam功能都需要那些步骤的.而且,都是用的官方的例子, ...

  9. hdu 5091 Beam Cannon(扫描线段树)

    题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,如今要有一个W∗H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,如果有点(x,y),那么(x,y)~(x+W,y+ ...

随机推荐

  1. JS面向对象基础1

    根据之前看了面向对象相关的视频,按照自己的理解,整理出相关的笔记,以便自己的深入理解.     如果要判断两个数是否相等,将值乘以10或者100,转换成整数再进行比较 例如:a++与++a 短路运算符 ...

  2. Access denied for user 'root'@'localhost' (using password:YES) 解决方案[转]

    关于昨天下午说的MySQL服务无法启动的问题,解决之后没有进入数据库,就直接关闭了电脑. 今早打开电脑,开始-运行 输入“mysql -uroot -pmyadmin”后出现以下错误: “Access ...

  3. .parents() 与 .parent()对比

    今天使用jQuery时候需要用到parents()方法,css()方法的使用 现在只是知道可以取到父级DOM节点,研究是否可以通过选取class名或者id名进行选取. 取到父级DOM节点 $(e.sr ...

  4. Java 1.0 类与对象

    1.Java中main()的作用: a.测试真正的类 b.启动Java应用程序 2. Java程序只会让对象与对象交互 3.创建对象时存储在堆中,自动回收. 4.Java无全局变量 5.Java程序由 ...

  5. CF 602B Approximating a Constant Range

    (●'◡'●) #include<iostream> #include<cstdio> #include<cmath> #include<algorithm& ...

  6. MySQL字符串类型

    VARCHAR类型用于存储变长字符串,它会删除末尾的所有空格,它比定长字符串更省空间,因为它仅使用必要的空间(越短的字符串占用越少),VARCHAR会用1或2个额外字节记录字符串长度(如果字符串长度不 ...

  7. Openjudge-计算概论(A)-放苹果

    描述: 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法.输入第一行是测试数据的数目t(0 <= t < ...

  8. KVM 虚拟化基本搭建

    KVM虚拟化技术 KVM是基于x86架构上Linux操作系统的全虚拟化解决方案 ,在Centos6.3系统中,kvm已经被集成到内核中,相当于使用内核来做虚拟机管理程序.由于KVM本身就工作于内核环境 ...

  9. C#中泛型默认关键字(default)详解

    我们在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T:(T 是引用类型还是值类型?)对此我们将如何处理? C#代码实例: /// <summary&g ...

  10. 将当天时间转换为unix时间戳

    /** * @return * * @Title: getDate * @Description: TODO(时间戳转换为String类型的日期数据) * @param @param unixDate ...