Application of Zoj 2615 Cells stack

Source: Internet
Author: User

Title Link: ZOJ-2615

Scientists is conducting on the behavior of a newly discovered agamic Cellular microbe. This special kind of microbe are 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 it birth and lasts for approximately several seconds;
2. The multiplication phase, in which one ACM can procreate up to + offspring in only several milliseconds;
3. The mature phase, in which it remains inactive for the rest of their 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 is numbered, starting from 1, according to their positi ONS 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. Experiment is stopped after a certain time period.

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

Input Description

Standard input would contain multiple test cases. The first line of the input was a single integer t (1 <= t <=) which is the number of the test cases. T test Cases Follow, each preceded to a single blank line.

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

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

Output Description

Results should is directed to standard output. Start each case with a ' case #: ' On a ', where # is the case number starting from 1. The consecutive cases should is separated by a and blank line. No blank line should is produced after the last Test case.

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

Test Instructions Description: Give a tree, and then M inquire, each asking u,v, to determine whether U is the ancestor of V.

Algorithm analysis: At the beginning to see this problem, immediately think of LCA, so quickly find the template and knock on the LCA, check, pay,segmentation Fault (Wow, this is zoj unique judge results), and later changed, turn, MLE, no language in ... Seeing other people's ideas is done with stacks and DFS, two timestamps for each node: the timestamp of the first visit and the time stamp of the second access (which you can imagine in Dfs), and then the timestamp to determine whether it was an ancestor. Very fast in efficiency, and learned a lesson.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <stack>8 #defineINF 0x7fffffff9 using namespacestd;Ten Const intmaxn=300000+Ten; One Const intM =20000000+ -; A  - intN,m,pre[m],bac[m],vis[m],dfs_clock; - intan[maxn],c[maxn],sum; thestack<int>S; -  - voidDFS () - { +memset (Vis,0,sizeof(Vis)); -      while(!S.empty ()) S.pop (); +S.push (0); A      while(!s.empty ()) at     { -         intu=s.top (); -         if(vis[u]==0) -         { -vis[u]=1; -pre[u]= + +Dfs_clock; in              for(inti=an[u]+1; i<=an[u]+c[u]; i++) -             { to                 if(i<N) s.push (i); +                 Else { -pre[i]= + +Dfs_clock; thebac[i]= + +Dfs_clock; *                 } $             }Panax Notoginseng         } -         Else if(vis[u]==1) the         { +bac[u]= + +Dfs_clock; A S.pop (); the         } +     } - } $  $ intMain () - { -     intT,ncase=1; the     intok=0; -scanf"%d",&t);Wuyi      while(t--) the     { -         if(OK) printf ("\ n"); ok=1; Wuprintf"Case %d:\n", ncase++); -memset (PRE,0,sizeof(pre)); Aboutmemset (BAC,0,sizeof(BAC)); $Memset (AN,0,sizeof(an)); -Memset (c,0,sizeof(c)); -sum=0; -dfs_clock=0; A         intb; +scanf"%d",&n); the          for(intI=0; i<n; i++.) -         { $scanf"%d",&c[i]); thean[i]=sum; theSum + =C[i]; the         } the dfs (); -scanf"%d",&m); in          while(m--) the         { thescanf"%d%d",&a,&b); About             if(Pre[a]<pre[b] && bac[a]>bac[b]) printf ("yes\n"); the             Elseprintf"no\n"); the         } the     } +     return 0; -}

Application of Zoj 2615 Cells stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.