题目链接:ZOJ - 2615

Scientists are conducting research on the behavior of a newly discovered Agamic Cellular Microbe. This special kind of microbe is capable of massively reproducing by itself in a short time. The lifetime of an ACM consists of three phases:
1. The infancy phase, which starts from its birth and lasts for approximately several seconds;
2. The multiplication phase, in which one ACM can procreate up to 100 offspring in only several milliseconds;
3. The mature phase, in which it remains inactive for the rest of its life.

At the beginning of the experiment, a newborn, single cell of ACM, is
put into a suitable circumstance for its production. This cell,
numbered as 0, starts to multiply and its descendants are numbered,
starting from 1, according to their positions in the family hierarchy.
During the experiment special equipment is used to record the numbers of
the offspring generated by each of the ACM's. The experiment is stopped
after a certain time period.

Your task is to help the scientists to determine whether one ACM is an ancestor of another.

Input Description

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.

Each test case starts with a single integer N (1 <= N <= 300,000) which is the number of ACM's that have their descendants recorded. The following N integers (not necessarily on a same line), Ci (0 <= i < N, 0 <= Ci <= 100), give the number of offspring of the i-th ACM. The next line contains an integer M (1 <= M <= 1,000,000) which is the number of queries. M lines follow, each contains two integers a and b, querying whether the a-th ACM is an ancestor of the b-th ACM.

The total number of ACM's may be greater than N, but would never exceed 20,000,000.

Output Description

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.

For each query, print either "Yes" or "No" on a single line, which is the answer to the query.

题意描述:给出一棵树,然后M个询问,每个询问u,v,判断u是否是v的祖先。

算法分析:刚开始看到这道题的时候,立马想到了LCA,于是快速找到LCA的模板并敲上,检查一下,交之,Segmentation Fault (哇,这是ZOJ独有的Judge结果),后来又改了改,交之,MLE,无语中。。。看到别人的想法是运用栈和dfs来处理即可了,给每个节点搞两个时间戳:第一次访问的时间戳和第二次访问(可以想象dfs中回溯的思想)的时间戳,然后通过时间戳来判断是否为祖先。效率上挺快的,又学习了一课。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<stack>
  8. #define inf 0x7fffffff
  9. using namespace std;
  10. const int maxn=+;
  11. const int M = +;
  12.  
  13. int n,m,pre[M],bac[M],vis[M],dfs_clock;
  14. int an[maxn],c[maxn],sum;
  15. stack<int> S;
  16.  
  17. void dfs()
  18. {
  19. memset(vis,,sizeof(vis));
  20. while (!S.empty()) S.pop();
  21. S.push();
  22. while (!S.empty())
  23. {
  24. int u=S.top() ;
  25. if (vis[u]==)
  26. {
  27. vis[u]=;
  28. pre[u]= ++dfs_clock;
  29. for (int i=an[u]+ ;i<=an[u]+c[u] ;i++)
  30. {
  31. if (i<n) S.push(i);
  32. else {
  33. pre[i]= ++dfs_clock;
  34. bac[i]= ++dfs_clock;
  35. }
  36. }
  37. }
  38. else if (vis[u]==)
  39. {
  40. bac[u]= ++dfs_clock;
  41. S.pop();
  42. }
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48. int t,ncase=;
  49. int ok=;
  50. scanf("%d",&t);
  51. while (t--)
  52. {
  53. if (ok) printf("\n");ok=;
  54. printf("Case %d:\n",ncase++);
  55. memset(pre,,sizeof(pre));
  56. memset(bac,,sizeof(bac));
  57. memset(an,,sizeof(an));
  58. memset(c,,sizeof(c));
  59. sum=;
  60. dfs_clock=;
  61. int a,b;
  62. scanf("%d",&n);
  63. for (int i= ;i<n ;i++)
  64. {
  65. scanf("%d",&c[i]);
  66. an[i]=sum;
  67. sum += c[i];
  68. }
  69. dfs();
  70. scanf("%d",&m);
  71. while (m--)
  72. {
  73. scanf("%d%d",&a,&b);
  74. if (pre[a]<pre[b] && bac[a]>bac[b]) printf("Yes\n");
  75. else printf("No\n");
  76. }
  77. }
  78. return ;
  79. }

zoj 2615 Cells 栈的运用的更多相关文章

  1. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  2. ZOJ - 2615 Cells

    注意数组别开太小了,代码照着训练经典打的: #include <iostream> #include <sstream> #include <cstdio> #in ...

  3. 牡丹江.2014B(图论,树的直径)

    B - Building Fire Stations Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & ...

  4. ZOJ 2967计算几何+单调栈

    ZOJ - 2967Colorful Rainbows 题目大意:给你道彩虹,每条彩虹有两个属性,a斜率和b截距,也就是彩虹描述为y=ax+b的直线,并且不存在垂直的彩虹以及一样的彩虹.然后就说明,如 ...

  5. ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given initially empty stacks, ther ...

  6. LA 3486 Cells(判祖先+栈模拟dfs)

    https://vjudge.net/problem/UVALive-3486 题意: 判断u是否是v的祖先. 思路: 很简单,dfs遍历,记录每个节点第一次访问时的时间戳 in[i] 和第二次访问时 ...

  7. 【栈模拟dfs】Cells UVALive - 3486

    题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...

  8. 【火车出栈】ZOJ - 2603 Railroad Sort

    好久没写递归了,怕手生再来练练手. 题意:车轨上有上图所示的n个中转栈,现有2n个列车,给出列车初始编号序列.列车从最右边驶入车轨,并且列车只能从右向左移动,要求给出列车中转操作序列,使列车经过这n个 ...

  9. ZOJ 3967 Colorful Rainbows --栈的应用

    题意:给出n条y=ai*x+bi的直线.对于这些直线,如果存在x使得该直线y大于其他任意一直线,那么这条直线可以被看见,问有多少条直线可以被看见. 做法什么的不讲了,参见:http://blog.cs ...

随机推荐

  1. java高级编程技巧

    1. boolean a= b==null;这句话很亮. public class Test { public static void main(String[] args) { String b=& ...

  2. IOS开发---菜鸟学习之路--(十)-实现新闻详细信息浏览页面

    前面已经将了上下拉刷新 实现了上下拉刷新后我们的第一级界面就做好,接下来我们就需要实现 新闻详细信息浏览了 我个人认为一般实现新闻详细页面的方法有两种(主要是数据源的不同导致了方法的不同) 第一种是本 ...

  3. 引用其他头文件时出现这种错误,莫名其妙,error C2065: “ColorMatrix”: 未声明的标识符

    今天做项目时,直接拷贝了另一个工程里的头文件和源文件,然后运行时就出现这种问题,莫名其妙,在原程序里运行一点问题就没有,但是在新工程里就是error. >e:\c++\button_fly2\b ...

  4. MFC深入浅出读书笔记第三部分1

    第八章 Document-View 深入探讨(总结) 1.Document Document 在MFC 的CDocument 里头被具体化.CDocument 本身并无任何具体数据,它只是提供一个空壳 ...

  5. 【SDOI2009】HH的项链 线段树

    题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链变得越来越长. ...

  6. Leetcode 599.两个列表的最小索引总和

    两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答 ...

  7. Spring框架的AOP

    Spring学习笔记(四) 本文目录 1 AOP的介绍 2 Spring的AspectJ实现AOP(annotation) 3 Spring的AspectJ实现AOP (XML) Spring文档ht ...

  8. 【转】C# client 与java netty 服务端的简单通信,客户端采用Unity

    http://blog.csdn.net/wilsonke/article/details/24721057 近日根据官方提供的通信例子自己写了一个关于Unity(C#)和后台通信的类,拿出来和大家分 ...

  9. vue使用stylus

    在package.json中添加  stylus-loader "css-loader": "^0.28.0", "stylus-loader&quo ...

  10. Secure services with TLS ---Docker配置https

    官方文档:https://docs.docker.com/ee/ucp/interlock/usage/tls/