Beam me out!

时间:2023-01-28 08:44:31

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 *er getting to the throne room during his random walk is 100%, or “*” 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("* ");
if(flag)puts("UNLIMITED");
else puts("LIMITED");
//system("Pause");
return ;
}