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. MyEclipse10.0 注册破解步骤

    MyEclipse 10.0破解 激活(java编写,适用于装有java环境的各种操作系统,Windows,Linux,MacOS) =====[方法一]=====[第一步]:输入任意用户名[第二步] ...

  2. 【深入理解javascript】this的用法

    引用:this的用法 在函数中this到底取何值,是在函数真正被调用执行的时候确定的,函数定义的时候确定不了 情况1:构造函数 函数作为构造函数用,那么其中的this就代表它即将new出来的对象.另外 ...

  3. git 下载单个文件 已经添加多个远程服务器

    175down vote It is possible to do (in the deployed repository) git fetch followed by git checkout or ...

  4. Floyd 判圈算法

    Floyd 判圈算法 摘自维基百科, LeetCode 上 141题 Linked List Cycle 用到这个, 觉得很有意思. 记录一下. 链接: https://zh.wikipedia.or ...

  5. [lr] 常用快捷键

    界面基本操作 F5               : 隐藏/显示上部面板 F6               : 隐藏/显示下部面板 F7               : 隐藏/显示左部面板 F8     ...

  6. EasyUI写的登录界面

    <!DOCTYPE html><html> <head>        <meta charset="utf-8" />       ...

  7. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  8. Linux中顿号

    ``的作用是运行``之间的命令,并且将命令运行的结果返回.一般shell脚本应该是这样:result=`ls -l` (用你的命令替换ls -l,这里只是举例)这样,result就有``里面的运行结果 ...

  9. python 展开嵌套的序列

    将一个多层嵌套的序列展开成一个单层列表 可以写一个包含yield from 语句的递归生成器来轻松解决这个问题. from collections import Iterable def flatte ...

  10. 好用的firefox浏览器、geckodriver驱动的版本组合(55 和 0.19.1)

    试过很多的firefox浏览器版本和geckodriver的组合,有时候好用,有时候不好用,现在确定了一个好用的版本组合,记录一下: firefox:版本55,而且此版本可以用firebug geck ...