Uvalive 3486 Cells

Source: Internet
Author: User

Determine the ancestor relationship through the order of inbound and outbound Stack

Here, uvalive also

#include <map>#include <set>#include <list>#include <cmath>#include <ctime>#include <deque>#include <stack>#include <queue>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <climits>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define LL long long#define PI 3.1415926535897932626using namespace std;int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}#define MAXN 20000010int pre[MAXN],post[MAXN];int start[300005],c[300005];int N;void dfs(int root){        stack<int>s;        s.push(root);        pre[root] = 0;        int dfs_clock = 0;        while (!s.empty())        {                int x = s.top();                if (pre[x])                {                        post[x] = ++dfs_clock;                        s.pop();                        continue;                }                pre[x] = ++dfs_clock;                for (int i =start[x]; i < start[x] + c[x] ; i++)                {                        if (i < N)                        {                                pre[i] = 0 ;                                s.push(i);                        }                        else                        {                                pre[i] = ++dfs_clock;                                post[i] = ++dfs_clock;                        }                }        }}int main(){        int T ,kase = 1;        scanf("%d",&T);        while (T--)        {                printf("Case %d:\n",kase++);                scanf("%d",&N);                start[0] = 1;                for (int i = 0; i < N; i++)                {                        scanf("%d",&c[i]);                        if (i)                                start[i] = start[i - 1] + c[i - 1];                }                dfs(0);                int m;                scanf("%d",&m);                while (m--)                {                        int u,v;                        scanf("%d%d",&u,&v);                        if(pre[u] < pre[v] && post[u] > post[v]) puts("Yes");                        else puts("No");                }                if (T) putchar(‘\n‘);        }}

 

No, zoj is over.

Uvalive 3486 Cells

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.