Beam me out!

Beam me out!

题目描述

King Remark, ?rst 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 inde?nitely 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 ?rst 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 ?rst 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+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;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[1000001],f[1000001];
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]=-1;
    for(int i=h[now];i;i=e[i].nxt)
    {
        int x=e[i].to;
        if(vis[x]==-1)
        {
            flag=true;
            return;
        }
        else if(!vis[x])dfs(x);
    }
    vis[now]=1;
}
void bfs()
{
    queue<int>p;p.push(1);vis[1]=1;
    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]=1;
        }
    }
    p.push(n);
    vis1[n]=1;
    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]=1;
            }
        }
    }
}
int main()
{
    int i,j;
    scanf("%d",&n);
    rep(i,1,n-1)
    {
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&j);
            add(i,j);
        }
    }
    dfs(1);
    memset(vis,0,sizeof(vis));
    bfs();
    rep(i,1,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 0;
}
时间: 2024-10-12 23:19:29

Beam me out!的相关文章

爱因斯坦的光线(Einstein’s Light Beam)

史上第七大哲学命题:爱因斯坦的光线(Einstein's Light Beam) ~ 爱因斯坦著名的狭义相对论是受启于他16岁做的思想实验.在他的自传中,爱因斯坦回忆道他当时幻想在宇宙中追寻一道光线.他推理说,如果他能够以光速在光线旁边运动,那么他应该能够看到光线成为"在空间上不断振荡但停滞不前的电磁场". ~ 对于爱因斯坦,这个思想实验证明了对于这个虚拟的观察者,所有的物理定律应该和一个相对于地球静止的观察者观察到的一样. .wiz-todo, .wiz-todo-img {widt

一个强大的Java开源遥感处理软件(库)---Beam

1.Beam简介 为了在Hadoop中可以序列化HDF文件,在各种搜索之后,无意中发现了一个强大的Java开源软件,它就是Beam. 对于从事遥感的研究者来说,可以通过安装Beam,将它当成一个强大开源的遥感处理软件使用.虽然没有envi那么好用,还马马虎虎过得去.至少安装Beam,不用授权文件去破解~ 对于遥感软件开发人员来说,Beam的库简直一大利器!!! 用Java实现什么重投影.波段运算.主成分分析.图像裁剪.图像镶嵌,创建直方图,五花八门.各式各样.繁简不一的遥感操作,对它来说都是一碟

关于 Image Caption 中测试时用到的 beam search算法

关于beam search 之前组会中没讲清楚的 beam search,这里给一个案例来说明这种搜索算法. 在 Image Caption的测试阶段,为了得到输出的语句,一般会选用两种搜索方式,一种是贪心采样的方法(sample),即:每个时刻都选择输出概率最大的那个单词,作为当前时刻的输出. 另一种常用的搜索方法就是:beam search.此处,借用知乎的一个案例: 假设词表大小为3,内容为a,b,c.beam search size是2,那么在decoder解码的时候: 1: 生成第1个

[Erlang_Question26]怎么通过beam文件得到所有的record或源代码?

怎么通过beam文件得到所有的record或源代码? 1. 首先必须要在compile里面加上debug_info信息: 1 > c(load,[debug_info]). {ok,load} 2> beam_lib:chunks(code:which(load),[compile_info]). {ok,{load,[{compile_info,[{options,[debug_info]}, {version,"5.0.2"}, {time,{2014,11,10,15

HDOJ 5091 Beam Cannon 扫描线

线段树+扫描线: 我们用矩形的中心点来描述这个矩形,然后对于每个敌舰,我们建立一个矩形中心的活动范围,即矩形中心在该范围内活动就可以覆盖到该敌舰.那么我们要求的问题就变成了:任意一个区域(肯定也是矩形的)最多能被矩形覆盖的最大值. Beam Cannon Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 159    Accepted S

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

题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,现在要有一个W?H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,假设有点(x,y),那么(x,y)~(x+W,y+H)形成的矩形,以框的右下角落的位置是可以圈住(x,y) 点,所以N个点即为N个矩形,求覆盖的最大次数,扫描线裸题. #include <cstdio> #include <cstring> #include <vector> #include <algorithm&

android Beam

Beaming NDEF Messages to Other Devices Android Beam allows simple peer-to-peer data exchange between two Android-powered devices. The application that wants to beam data to another device must be in the foreground and the device receiving the data mu

Apache Beam WordCount编程实战及源码解读

概述:Apache Beam WordCount编程实战及源码解读,并通过intellij IDEA和terminal两种方式调试运行WordCount程序,Apache Beam对大数据的批处理和流处理,提供一套先进的统一的编程模型,并可以运行大数据处理引擎上.完整项目Github源码 负责公司大数据处理相关架构,但是具有多样性,极大的增加了开发成本,急需统一编程处理,Apache Beam,一处编程,处处运行,故将折腾成果分享出来. 1.Apache Beam编程实战–前言,Apache B

erlang beam 文件格式

beam文件是erlang编译器生成的文件格式,可以直接加载到erlang vm中运行的文件格式. 一. 文件格式 beam文件的文件布局如下: 文件由一个文件头加各种块构成,块的结构由块头加自定义结构组成.在beam文件中atom块,code块,字符串块,导入表,导出表,是必须出现的块.其它可选(意思是你没用到beam文件就不会出现这些块). 接下来逐一介绍这些块: 1. 文件头 4字节 4字节 4字节 "FOR1" Size "BEAM" "FOR1&