UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在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)的更多相关文章
- zoj 2615 Cells 栈的运用
题目链接:ZOJ - 2615 Scientists are conducting research on the behavior of a newly discovered Agamic Cell ...
- 【栈模拟dfs】Cells UVALive - 3486
题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
- LA 3486 Cells(判祖先+栈模拟dfs)
https://vjudge.net/problem/UVALive-3486 题意: 判断u是否是v的祖先. 思路: 很简单,dfs遍历,记录每个节点第一次访问时的时间戳 in[i] 和第二次访问时 ...
- ZOJ - 2615 Cells
注意数组别开太小了,代码照着训练经典打的: #include <iostream> #include <sstream> #include <cstdio> #in ...
- 【作业】用栈模拟dfs
题意:一个迷宫,起点到终点的路径,不用递归. 题解: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdli ...
- UVALive 7454 Parentheses (栈+模拟)
Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...
- 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)
将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...
- 百炼3752:走迷宫--栈实现dfs
3752:走迷宫 总时间限制: 1000ms 内存限制: 65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最 ...
随机推荐
- MySQL数据库错误server_errno=2013的解决
MySQL数据库错误server_errno=2013的解决 一组MySQL复制环境中的Master意外掉电,重启后Master运行正常,但该复制环境中的其它slave端,Error Log中却抛出的 ...
- SVN提交错误:working copy is not up-to-date解决方法
我在项目中删了2个jar,然后SVN提交,一直提交不成功 svn在提交时报错如下图: working copy is not up-to-date svn:commit failed(details ...
- div均匀分布代码实例
多个div在同一行以相同间隔分布: 这样的布局效果使用非常的频繁,也就是让多个div在一行分布,并且div于div之间的间隙是一样的,多用在对于产品的展示之用,下面就介绍一下如何实现此中布局,代码实例 ...
- catci监控
snmp安装:yum install net-snmp* 配置/etc/snmp/snmpd.conf:com2sec notConfigUser 192.168.79.129 publicac ...
- C++ explict 关键字
关键字explicit可以禁止“单参数构造函数”被用于自动类型转换class Stack{explicit Stack(int size);};没有explicit的话Stack s = 40;能编译 ...
- hibernate Session
转: http://kayo.iteye.com/blog/204143 Session 接口 Session 接口对于Hibernate 开发人员来说是一个最重要的接口.然而在Hibernate 中 ...
- 理解lua 语言中的点、冒号与self
转载自: http://blog.csdn.net/wangbin_jxust/article/details/12170233 lua编程中,经常遇到函数的定义和调用,有时候用点号调用,有时候用冒号 ...
- Java垃圾收集器
概述 说起垃圾收集(Garbage Collection,GC),大部分人都把这项技术当做Java语言的伴生产物.事实上,GC的历史远远比Java久远,1960年诞生于MIT的Lisp是第一门真正使用 ...
- zju 2972 Hurdles of 110m(简单的dp)
题目 简单的dp,但是我还是参考了网上的思路,具体我没考虑到的地方见代码 #include<stdio.h> #include<iostream> #include<st ...
- hdu 4704 Sum
思路:对于给定的n,s(i)即将n分解为i个数的组合数,也就是在n-1个位置插入i-1个板即C(n-1,i-1); ∑S=2^(n-1); phi(1000000007)=1000000006; 对于 ...