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列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最 ...
随机推荐
- c++ switch case
http://www.cnblogs.com/RealOnlyme/articles/2579628.html
- C#中两个日期类型相减得到天数
protected int GetDuration(DateTime start, DateTime finish) { return (finish - start).Days; } 直接相减得到的 ...
- 【WCF--初入江湖】07 分布式事务
07 分布式事务 一.前言 [1]理解事务特性 [2]掌握TransactionFlow 特性 [3]掌握WCF中的事务属性 TransactionAutoCompleteOnSessionClose ...
- Extension Methods
Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or ...
- oracle RAC--归档日志的开启方法
oracle RAC--归档日志的开启方法 2011-10-07 15:53:04 分类: Oracle oracle RAC--归档日志的开启方法 ======================= ...
- 【面试题002】java实现的单例模式,c++实现单例模式,实现禁止拷贝
[面试题002]java实现的单例模式,c++实现单例模式,实现禁止拷贝 一 c++实现单例模式 保证一个类,在一个程序当中只有一个对象,只有一个实例,这个对象要禁止拷贝,注意这里要区别于java. ...
- POJ 3258
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5961 Accepted: 2579 D ...
- POJ2299Ultra-QuickSort
http://poj.org/problem?id=2299 题意 : 排序,求排序次数,本来以为用冒泡可以搞定,事实上,那么大的数据以及一个TLE告诉我,会超时......... 思路 : 问了一下 ...
- lintcode 中等题:Singleton number II 落单的数 II
题目 落单的数 II 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字. 样例 给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4 挑战 一次遍历,常数级 ...
- 欧拉工程第74题:Digit factorial chains
题目链接:https://projecteuler.net/problem=74 数字145有一个著名的性质:其所有位上数字的阶乘和等于它本身. 1! + 4! + 5! = 1 + 24 + 120 ...