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.

样例输入

  1. 3
  2. 2
  3. 2 3
  4. 1
  5. 3

样例输出

  1. PARDON LIMITED
    分析:有向无环图判定及bfs
    代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <vector>
  14. #include <list>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  17. #define mod 1000000007
  18. #define inf 0x3f3f3f3f
  19. #define vi vector<int>
  20. #define pb push_back
  21. #define mp make_pair
  22. #define fi first
  23. #define se second
  24. #define ll long long
  25. #define pi acos(-1.0)
  26. #define pii pair<int,int>
  27. #define Lson L, mid, ls[rt]
  28. #define Rson mid+1, R, rs[rt]
  29. const int maxn=5e4+;
  30. using namespace std;
  31. ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  32. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  33. inline ll read()
  34. {
  35. ll x=;int f=;char ch=getchar();
  36. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  37. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  38. return x*f;
  39. }
  40. int n,m,k,t,vis[maxn],vis1[maxn],tot,tot1,h[maxn],u[maxn];
  41. struct node
  42. {
  43. int to,nxt;
  44. }e[],f[];
  45. void add(int x,int y)
  46. {
  47. tot++;
  48. e[tot].to=y;
  49. e[tot].nxt=h[x];
  50. h[x]=tot;
  51. tot1++;
  52. f[tot1].to=x;
  53. f[tot1].nxt=u[y];
  54. u[y]=tot1;
  55. }
  56. bool flag,flag1;
  57. void dfs(int now)
  58. {
  59. if(flag)return;
  60. vis[now]=-;
  61. for(int i=h[now];i;i=e[i].nxt)
  62. {
  63. int x=e[i].to;
  64. if(vis[x]==-)
  65. {
  66. flag=true;
  67. return;
  68. }
  69. else if(!vis[x])dfs(x);
  70. }
  71. vis[now]=;
  72. }
  73. void bfs()
  74. {
  75. queue<int>p;p.push();vis[]=;
  76. while(!p.empty())
  77. {
  78. int q=p.front();p.pop();
  79. for(int i=h[q];i;i=e[i].nxt)
  80. {
  81. int x=e[i].to;
  82. if(!vis[x])p.push(x);vis[x]=;
  83. }
  84. }
  85. p.push(n);
  86. vis1[n]=;
  87. while(!p.empty())
  88. {
  89. int q=p.front();p.pop();
  90. for(int i=u[q];i;i=f[i].nxt)
  91. {
  92. int x=f[i].to;
  93. if(!vis1[x])
  94. {
  95. p.push(x);vis1[x]=;
  96. }
  97. }
  98. }
  99. }
  100. int main()
  101. {
  102. int i,j;
  103. scanf("%d",&n);
  104. rep(i,,n-)
  105. {
  106. scanf("%d",&m);
  107. while(m--)
  108. {
  109. scanf("%d",&j);
  110. add(i,j);
  111. }
  112. }
  113. dfs();
  114. memset(vis,,sizeof(vis));
  115. bfs();
  116. rep(i,,n)if(vis[i]&&!vis1[i])flag1=true;
  117. if(!flag1)printf("PARDON ");
  118. else printf("PRISON ");
  119. if(flag)puts("UNLIMITED");
  120. else puts("LIMITED");
  121. //system("Pause");
  122. return ;
  123. }

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. MySQL Replicationation进阶

    摘要 上一篇: MySQL Replication 基础  下一篇 MySQL Replication-MHA 一.主主复制 二.半同步复制 三.复制过滤器 四.总结 五.切分 待续 一.主主复制 M ...

  2. ubuntu下安装BeyondCompare比较工具

    在ubuntu12.04下使用比较工具,这里参考了网上的一个方法来安装BeyondCompare3 首先,下载相关软件: 这里选择了BCompare: http://www.scootersoftwa ...

  3. AJAx 刷新页面

    <html><head> <meta http-equiv="Content-Type" content="text/html; chars ...

  4. java集合概念

    Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector 底层数据结构是数组,查询快,增删慢 线程安全,效率 ...

  5. Sql CE 数据库编程

    用户工具: http://www.linqpad.net/ 查询编辑数据 http://sqlcequery.codeplex.com/  查询编辑数据 开发人员工具: 安装:EF Tools nug ...

  6. gcc及其选项详解 【转载】

    1.简介: gcc是gnu旗舰产品,目前基本上就是和unix捆绑在一起分发的.这个东西功能强大,但是有多达上千个选项,其用户手册也有将近一万行.虽然其中的多数选项平时很少用到.但是不管装软件还是写程序 ...

  7. 元素NULL判断

    元素取值val() val()方法主要用来获取form元素的值像input select textarea.在对select取值的时候当没有option被选定时val()会返回null,至少一个opt ...

  8. php 分页类(3)

    <?php class Page { private $total; //总记录 private $pagesize; //每页显示多少条 private $limit; //limit pri ...

  9. touchesBegan: withEvent: <--- with UIScrollView / UIImageView

    touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等只能被UIView捕获(如有问题请指出对 ...

  10. java程序使用memcached

    Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. 1.linux环境下安装与启动memcache: 以编译安装的方式安装.具体参看: http://wang ...