这道题在LA是挂掉了,不过还好,zoj上也有这道题。

题意:好大一颗树,询问父子关系。。考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE。

安心啦,这肯定是一道正常人能做的题目。不过是需要几个小技巧。

1、2000w个点不一定都要保存下来,事实上,虽然题目给了256M的空间,只要开了两个这么大的数组,MLE是跑不了的,所以只保存30w个父节点。

2、如果这30w个父节点构成一条链,dfs的栈肯定爆。所以需要用栈模拟dfs。这里用的是stack<int>,当然手写栈会更快。

注意:1、时间戳的使用。

    2、本题中顺序对节点标号,使得所有>=n的节点都是叶子节点,同时能够二分也是因为它是有序的。

 #include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std; const int MAXN=; struct Point{
int l,r;
}point[MAXN]; struct T{
int in,out;
}tim[MAXN]; int child[MAXN],dfs_clock;
stack<int>stk; void init()
{
dfs_clock=;
while(!stk.empty())
stk.pop();
} void dfs(int n)
{
init();
stk.push();
tim[].in=++dfs_clock;
child[]=;
while(!stk.empty())
{
int x=stk.top();
int r=point[x].r;
if(child[x]>r||child[x]>=n){
tim[x].out=++dfs_clock;
stk.pop();
}else {
stk.push(child[x]);
tim[child[x]].in=++dfs_clock;
child[child[x]]=point[child[x]].l;
child[x]++;
}
}
} int find(int x,int n)//找父节点:利用顺序编码进行二分
{
int l=,r=n-;
while(l<r)
{ int m=l+(r-l+)/;
if(point[m].l<=x&&point[m].r>=x)
return m;
if(point[m].l>x)
r=m-;
else
l=m;
}
return l;
} int main()
{
int TT;
scanf("%d",&TT); int n;
for(int cas=;cas<=TT;cas++)
{
scanf("%d",&n);
int x,y=;
for(int i=;i<n;i++)
{
scanf("%d",&x);
point[i].l=y;
point[i].r=y+x-;
y+=x;
} dfs(n); int m,u,v,pv;
scanf("%d",&m);
printf("Case %d:\n",cas);
int flog;
for(int i=;i<m;i++)
{
flog=;
scanf("%d%d",&u,&v); if(u<n&&v<n){
if(tim[u].in<tim[v].in&&tim[u].out>tim[v].out)
flog=;
}
else if(u<n&&v>=n){//u<n漏掉了,导致RE;若u>=n,那么u就是叶子节点,不可能是父亲
pv=find(v,n);
if(tim[u].in<=tim[pv].in&&tim[u].out>=tim[pv].out)
flog=;
} if(flog)
printf("Yes\n");
else
printf("No\n");
}
if(cas!=TT)
puts("");
} return ;
}

UVALive 3486/zoj 2615 Cells(栈模拟dfs)的更多相关文章

  1. zoj 2615 Cells 栈的运用

    题目链接:ZOJ - 2615 Scientists are conducting research on the behavior of a newly discovered Agamic Cell ...

  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. LA 3486 Cells(判祖先+栈模拟dfs)

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

  5. ZOJ - 2615 Cells

    注意数组别开太小了,代码照着训练经典打的: #include <iostream> #include <sstream> #include <cstdio> #in ...

  6. 【作业】用栈模拟dfs

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

  7. UVALive 7454 Parentheses (栈+模拟)

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

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

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

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

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

随机推荐

  1. 【BZOJ】【2982】Combination

    排列组合 Lucas定理模板题…… 感觉我做题顺序有点问题啊……应该是BZOJ 2982-->HDOJ 3037-->BZOJ 1272 好吧这个现在来看就有些水了…… 预处理一下fact ...

  2. Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi

    Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi This spring, Kaggle hosted two competitions w ...

  3. Tutorial Unity 4 apk splitting into OBB for google play

    http://docs.unity3d.com/Manual/android-OBBsupport.html http://www.exoa.fr/tutorial-unity-4-apk-split ...

  4. Tesseract-OCR牛刀小试:模拟请求时的验证码识别

    原文:http://yaohuiji.com/tag/tesseract%EF%BC%8Cocr%EF%BC%8C%E9%AA%8C%E8%AF%81%E7%A0%81/ 有个邪恶的需求,需要识别验证 ...

  5. linux上部署应用

    1.编写traffic.sh 引入相关的jar包及java环境路径 2.crontab -e 加入: */10 * * * * cd /opt/sys/traffic_version/bin & ...

  6. Python分析NGINX LOG版本二

    不好意思,上一版逻辑有错误,(只分析了一次就没了) 此版改正. 按同事要改,作成传参数形式,搞定. #!/usr/bin/env python # coding: utf-8 ############ ...

  7. 取消 EditText 自动聚焦弹出输入法界面

    方法一(实测可行): 参考http://www.cnblogs.com/dream-cichan/p/aaaa.html 当我点击跳转至一个带有EditText的界面后,模拟器中的软键盘会自动弹出,严 ...

  8. JAVA多线程的问题以及处理【转】

    http://www.cnblogs.com/springcsc/archive/2009/12/03/1616394.html 12.4 多线程问题及处理          多线程编程为程序开发带来 ...

  9. Web中的监听器【Listener】与过滤器【Filter】 实例

    监听器实例: package com.gwssi.listener; import javax.servlet.http.HttpSession; import javax.servlet.http. ...

  10. P1026 犁田机器人

    P1026 犁田机器人 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 USACO OCT 09 2ND 描述 Farmer John為了让自己从无穷无尽的犁 ...