通过入栈出栈顺序判断祖先关系

这里UVALIVE还

#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.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 20000010
int pre[MAXN],post[MAXN];
int start[],c[];
int N;
void dfs(int root)
{
stack<int>s;
s.push(root);
pre[root] = ;
int dfs_clock = ;
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] = ;
s.push(i);
}
else
{
pre[i] = ++dfs_clock;
post[i] = ++dfs_clock;
}
}
}
}
int main()
{
int T ,kase = ;
scanf("%d",&T);
while (T--)
{
printf("Case %d:\n",kase++);
scanf("%d",&N);
start[] = ;
for (int i = ; i < N; i++)
{
scanf("%d",&c[i]);
if (i)
start[i] = start[i - ] + c[i - ];
}
dfs();
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');
}
}

是不行,ZOJ过了。

UVALIVE 3486 Cells的更多相关文章

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

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

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

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

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

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

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

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

  5. Gym 101194H / UVALive 7904 - Great Cells - [数学题+快速幂][2016 EC-Final Problem H]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  6. UVALive 5066 Fire Drill BFS+背包

    H - Fire Drill Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  7. C#使用Aspose.Cells导出Excel简单实现

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

  8. Aspose.Cells导出Excel(2)

    DataTable dtTitle = ds.Tables[]; DataTable dtDetail = ds.Tables[]; int columns = dtTitle.Columns.Cou ...

  9. Aspose.Cells导出Excel(1)

    利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...

随机推荐

  1. 阿里云SLB漏选“健康检查正常的http状态码”导致url重定向失败问题处理

    背景:           一客户将线下电商网站迁移到阿里云上,公网出口使用阿里云SLB,SLB后端实例为ECS(webserver)web服务使用nginx.后端APP服务器使用了tomcat:to ...

  2. 洛谷P1379八数码难题

    题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中. 要求解的问题是:给出一种初始布局(初始状态)和目标布局(为 ...

  3. 1.爬虫 urlib库讲解 Handler高级用法

    在前面我们总结了urllib库的 urlopen()和Request()方法的使用,在这一小节我们要使用相关的Handler来实现代理.cookies等功能. 写在前面: urlopen()方法不支持 ...

  4. HDU 4782 Beautiful Soup (模拟+注意细节)

    思路就是用栈模拟,不用开实体的栈,直接记一个top指针就行. 说说这题的细节: 1.tag标签里的内容不要动,原样输出.比如<p aa bb cc>,就这样输出就行,不要删空格.题目中说了 ...

  5. POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  6. 【转】hexo博客图片问题

    1.首先确认_config.yml 中有 post_asset_folder:true. Hexo 提供了一种更方便管理 Asset 的设定:post_asset_folder 当您设置post_as ...

  7. 使用window.getSelection()获取div中选中文字内容及位置

    div添加一个弹出事件: $(document).ready(function () { $("#marked-area").mouseup(function (e) { $sco ...

  8. JavaScript内置对象常用

    Math 提供了数学中常用的属性和方法,使用时直接用Math.属性/方法,而不需要new一个Math对象 Date 使用Date对象来对日期和时间进行操作.使用时,必须用new创建一个实例 windo ...

  9. CSS继承—深入剖析

    CSS的继承是指被包在内部的标签将拥有外部标签的样式性质.继承特性最典型的应用通常发挥在整个网页的样式预设,即整体布局声明.而需要要指定为其它样式的部份设定在个别元素里即可达到效果.这项特性可以给网页 ...

  10. DataBase -- Second Highest Salary

    Question: Write a SQL query to get the second highest salary from the Employee table. +----+-------- ...