Beam me out!

Posted mxzf0213

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

以上是关于Beam me out!的主要内容,如果未能解决你的问题,请参考以下文章

Beam Search生成的句子基本都一样,是不是有方法扩展生成句子的多样性?

BEAM188和LINK180简单实例

Java语言基础之方法的设计

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

Java中程序的逻辑控制语句

Android 动画嵌套片段