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

这里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. Django源码分析之server

    乍见 Django内置的server基本包括两部分:django.core.servers和django.core.handlers 相识 servers.basehttp是Django自身提供的一个 ...

  2. pandas DataFrame的创建方法

    pandas DataFrame的增删查改总结系列文章: pandas DaFrame的创建方法 pandas DataFrame的查询方法 pandas DataFrame行或列的删除方法 pand ...

  3. Linux 简单socket实现TCP通信

    服务器端代码 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <stri ...

  4. PhpStorm 配置IDE

    IDE => Xdebug => Apache(XAMPP) => Firefox + easist Xdebug 1>XAMPP停止apache服务;2>在安装目录下找 ...

  5. Spring框架(依赖注入)

    特点 1轻量级和侵入性低 2依赖注入和面向接口实现松耦合 3面向切面编程 减少样式代码 专有名词: 1依赖注入:对象无需自行管理依赖关系.通过系统负责协调在创建对象的第三方组件的设定,实现依赖关系自动 ...

  6. iOS-登录发送验证码时60秒倒计时,直接用

    __block NSInteger timeout= ; //倒计时时间 KWeakSelf dispatch_queue_t queue = dispatch_get_global_queue(DI ...

  7. 封装 RabbitMQ.NET

    这篇文章内容会很短,主要是想给大家分享下我最近在做一个简单的rabbitmq客户端类库的封装的经验总结,说是简单其实一点都不简单.为了节省时间我主要按照Library的执行顺序来介绍,在你看来这里仅仅 ...

  8. qemu的drive参数解释

    drive参数很简单,可以理解成是定义了一个实际的硬盘(或者是cd)与drive对应的是device-drive option[,option[,option[,...]]] Define a new ...

  9. 【bzoj3379】[Usaco2004 Open]Turning in Homework 交作业 区间dp

    题目描述 数轴上有C个点,每个点有一个坐标和一个访问时间,必须在这个时间后到达这个点才算访问完成.可以在某个位置停留.每在数轴上走一个单位长度消耗一个单位的时间,问:访问所有点并最终到B花费的最小时间 ...

  10. mac --snip 滚动截屏

    1.snip 下载配置:https://jingyan.baidu.com/article/fec4bce2458d03f2618d8b8e.html 2.mac的火狐浏览器好像不支持,必须在sofa ...