https://vjudge.net/problem/UVALive-3486

题意:

判断u是否是v的祖先。

思路:

很简单,dfs遍历,记录每个节点第一次访问时的时间戳 in[i] 和第二次访问时的时间戳 out[i],如果满足in[x]<in[y]<out[x],那么x就是y的祖先。

用dfs实现是很容易的事,但这题需要用栈来模拟一下。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m;
int start[maxn];
int c[maxn];
int in[maxn];
int out[maxn]; void dfs(int u)
{
stack<int> S;
int dfs_clock=;
in[u]=;
S.push(u);
while(!S.empty())
{
int x=S.top();
if(in[x])
{
out[x]=++dfs_clock;
S.pop();
continue;
}
in[x]=++dfs_clock;
for(int i=start[x];i<start[x]+c[x];i++)
{
if(i<n) {in[i]=;S.push(i);}
else {in[i]=++dfs_clock;out[i]=++dfs_clock;}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
int T; int kase=;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int s=;
for(int i=;i<n;i++)
{
scanf("%d",&c[i]);
start[i]=s; s+=c[i];
}
dfs();
printf("Case %d:\n",++kase);
int q;
scanf("%d",&q);
while(q--)
{
int u,v;
scanf("%d%d",&u,&v);
if(in[u]<in[v] && out[u]>in[v]) puts("Yes");
else puts("No");
}
if(T) puts("");
}
return ;
}

LA 3486 Cells(判祖先+栈模拟dfs)的更多相关文章

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

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

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

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

  3. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  4. 【作业】用栈模拟dfs

    题意:一个迷宫,起点到终点的路径,不用递归. 题解: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdli ...

  5. 51 nod 1681 公共祖先 (主席树+dfs序)

    1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...

  6. 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)

    将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...

  7. 百炼3752:走迷宫--栈实现dfs

    3752:走迷宫 总时间限制:  1000ms 内存限制:  65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最 ...

  8. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  9. UVALive 7454 Parentheses (栈+模拟)

    Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...

随机推荐

  1. crontab定时任务-干货案例

    自定义需求:实现消息队列. 1.创建一张mysql表结构 2.编写php脚本,便于sh文件执行 3.编写sh脚本,便于crontab定时执行 4.crontab -e 注册定时任务,如果此步不清楚请参 ...

  2. [py][mx]django自带后台系统使用

    django的manytomany字段和后台搜索过滤功能 后台开发一般要求 后台要求能快速搭建, 主要精力放在前端用户系统开发上. 权限管理 少量样式 快速开发 django自带的后台手动注册模型 创 ...

  3. 重读《Java编程思想》

    相关最新代码已上传至我的GitHub了(https://github.com/WenyangSun/ThinkingInJava),后续例子没有在博客上更新. 1.在类的内部,变量定义的先后顺序决定了 ...

  4. [LeetCode] 197. Rising Temperature_Easy tag: SQL

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  5. js时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 深入理解php内核——读书笔记1

    第一章 基础准备 宏定义 #字符串化 ##连接符 do{}while(0) 多行 全局宏: EG.PG 第二章 用户代码的执行 php请求的生命周期 SAPI接口 php脚本执行 第三章 变量及数据类 ...

  7. UVM环境(一)

    1)如何避免绝对路径的出现:绝对路径一般都是用在信号的连接关系上,这样可以用virtual interface,来通过句柄的赋值来动态的建立连接关系.那么顶层模块怎么 样将interface的句柄赋值 ...

  8. xhtml 的三种 doctype

    {1}文档宣告 <!ODCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 [there]//EN" "http://www.w4org/TR/xhtml1/DTD/ ...

  9. nginx之全局设置,location,虚拟主机,日志管理

    nginx之全局设置,location,虚拟主机,日志管理 worker_processes 1;//子进程,cpu数*核数 ****************全局设置************** ** ...

  10. 谷歌插件--Advanced REST client

    早上在测试调用服务去获取数据的时候,因为自己的单元测试不是很熟悉,问了同事,同事给我介绍了一个插件Advanced REST client,这个可以在谷歌的“扩展与应用”中找打,使用 安装之后会提示要 ...