zoj 2615 Cells 栈的运用

题目链接: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=300000+10;
11 const int M = 20000000+100;
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,0,sizeof(vis));
20     while (!S.empty()) S.pop();
21     S.push(0);
22     while (!S.empty())
23     {
24         int u=S.top() ;
25         if (vis[u]==0)
26         {
27             vis[u]=1;
28             pre[u]= ++dfs_clock;
29             for (int i=an[u]+1 ;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]==1)
39         {
40             bac[u]= ++dfs_clock;
41             S.pop();
42         }
43     }
44 }
45
46 int main()
47 {
48     int t,ncase=1;
49     int ok=0;
50     scanf("%d",&t);
51     while (t--)
52     {
53         if (ok) printf("\n");ok=1;
54         printf("Case %d:\n",ncase++);
55         memset(pre,0,sizeof(pre));
56         memset(bac,0,sizeof(bac));
57         memset(an,0,sizeof(an));
58         memset(c,0,sizeof(c));
59         sum=0;
60         dfs_clock=0;
61         int a,b;
62         scanf("%d",&n);
63         for (int i=0 ;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 0;
79 }
时间: 2024-10-09 22:52:29

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

ZOJ 3643 kmp+栈处理失配指针

当kmp在母串中匹配到子串之后,删去子串相当于把失配指针往回退到匹配位置的前一个,如果只回退一次的话,直接用数组就可以处理失配指针了,但是本题可能删除后存在多个子串,所以用栈把失配指针位置先存起来,然后找到一次把len长度的失配指针删去,然后栈顶元素就是第一句话说的回退出匹配位置的前一个了. 为了方便调用函数,把以前的kmp改成了接口,方便使用,这样就不用开全局变量的数组了,代码看起来更符合工作中开发的代码风格. #include <iostream> #include <cstdio&

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

B - Building Fire Stations Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3820 Appoint description:  System Crawler  (2015-08-15) Description Marjar University is a beautiful and peaceful place.

七方件业金离以提领前群约会ODjdidtlwfWv

为了从不同环节,尤其与广大使用人群直接关系的环节反映质量状况,对共享自行车投放点.运营仓库.生产企业等不同环节的产品抽查,覆盖了共享自行车从成品出厂到待投放的关键环节. 该负责人称,根据新车投放情况,结合共享自行车行业市场占有分布特点,本次重点抽查了摩拜.ofo.Hellobike三个品牌的产品,占本次抽查批次总数的83.3%.其中,在天津.无锡.武汉.广州.深圳.东莞6个城市抽查了9批次摩拜产品,占产品抽查批次总数的37.5%,抽查批次合格率88.9%,抽查不合格的1批次产品为待投放于广州市的

ZOJ 3967 Colorful Rainbows --栈的应用

题意:给出n条y=ai*x+bi的直线.对于这些直线,如果存在x使得该直线y大于其他任意一直线,那么这条直线可以被看见,问有多少条直线可以被看见. 做法什么的不讲了,参见:http://blog.csdn.net/ten_three/article/details/12289427  以及  http://blog.sina.com.cn/s/blog_7eee8bf3010136d8.html 利用了堆栈来做,总体复杂度O(nlogn) 代码: #include <iostream> #in

【火车出栈】ZOJ - 2603 Railroad Sort

好久没写递归了,怕手生再来练练手. 题意:车轨上有上图所示的n个中转栈,现有2n个列车,给出列车初始编号序列.列车从最右边驶入车轨,并且列车只能从右向左移动,要求给出列车中转操作序列,使列车经过这n个中转栈后从最左端输出时满足1~2n的排列. 分析:其实这不就是火车出栈问题吗,可以用分治递归的思路,首先使一半有序,然后再合并使整体有序. 思路:具体来讲就是,一段无序的序列(编号1~2n),通过一个中转栈使1 ~ 2n-1在整个序列的前面2n-1+1 ~ 2n在整个序列的后面:再经过下一中转栈时,

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

Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given initially empty stacks, there are three types of operations: 1 s v: Push the value onto the top of the -th stack. 2 s: Pop the topmost value out of the -th stack, and print that

ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in

Cells UVALive - 3486(dfs序+手动开栈)

给一棵树,每次每次询问一个点是否是另一个点的祖先? 输入时是每个下标对应节点的儿子的数量 用dfs序 时间戳.. 如果一个点是另一个点的祖先,那么它的两个标记一定在祖先的范围之内 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector>

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio