UVALIVE 3486 Cells
通过入栈出栈顺序判断祖先关系
这里UVALIVE还
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 20000010
int pre[MAXN],post[MAXN];
int start[],c[];
int N;
void dfs(int root)
{
stack<int>s;
s.push(root);
pre[root] = ;
int dfs_clock = ;
while (!s.empty())
{
int x = s.top();
if (pre[x])
{
post[x] = ++dfs_clock;
s.pop();
continue;
}
pre[x] = ++dfs_clock;
for (int i =start[x]; i < start[x] + c[x] ; i++)
{
if (i < N)
{
pre[i] = ;
s.push(i);
}
else
{
pre[i] = ++dfs_clock;
post[i] = ++dfs_clock;
}
}
}
}
int main()
{
int T ,kase = ;
scanf("%d",&T);
while (T--)
{
printf("Case %d:\n",kase++);
scanf("%d",&N);
start[] = ;
for (int i = ; i < N; i++)
{
scanf("%d",&c[i]);
if (i)
start[i] = start[i - ] + c[i - ];
}
dfs();
int m;
scanf("%d",&m);
while (m--)
{
int u,v;
scanf("%d%d",&u,&v);
if(pre[u] < pre[v] && post[u] > post[v]) puts("Yes");
else puts("No");
}
if (T) putchar('\n');
}
}
是不行,ZOJ过了。
UVALIVE 3486 Cells的更多相关文章
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- Cells UVALive - 3486(dfs序+手动开栈)
给一棵树,每次每次询问一个点是否是另一个点的祖先? 输入时是每个下标对应节点的儿子的数量 用dfs序 时间戳.. 如果一个点是另一个点的祖先,那么它的两个标记一定在祖先的范围之内 #include & ...
- 【栈模拟dfs】Cells UVALive - 3486
题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...
- LA 3486 Cells(判祖先+栈模拟dfs)
https://vjudge.net/problem/UVALive-3486 题意: 判断u是否是v的祖先. 思路: 很简单,dfs遍历,记录每个节点第一次访问时的时间戳 in[i] 和第二次访问时 ...
- Gym 101194H / UVALive 7904 - Great Cells - [数学题+快速幂][2016 EC-Final Problem H]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- UVALive 5066 Fire Drill BFS+背包
H - Fire Drill Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- C#使用Aspose.Cells导出Excel简单实现
首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...
- Aspose.Cells导出Excel(2)
DataTable dtTitle = ds.Tables[]; DataTable dtDetail = ds.Tables[]; int columns = dtTitle.Columns.Cou ...
- Aspose.Cells导出Excel(1)
利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...
随机推荐
- PAT——乙级1006:换个格式输出整数&乙级1021:个位数统计&乙级1031:查验身份证
1006 换个格式输出整数 (15 point(s)) 让我们用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 ...
- [转载]深入理解Batch Normalization批标准化
文章转载自:http://www.cnblogs.com/guoyaohua/p/8724433.html Batch Normalization作为最近一年来DL的重要成果,已经广泛被证明其有效性和 ...
- Java生成C#可用Model包
项目需要提供接口给.NET团队使用,为方便大伙,特地写一个从Java接口生成C#可用Model包的工具Class 主Class是一个Controller,可以随时进行生成 package com.fa ...
- python类学习以及mro--多继承属性查找机制
版权声明:本文为博主原创文章,未经博主允许不得转载. 还记得什么是新式类和旧式类吗? Python中,一个class继承于object,或其bases class里面任意一个继承于object,这个c ...
- java long值转成时间格式
/** * 将long值转换为以小时计算的格式 * @param mss * @return */ public static String formatLongTime(long mss) { St ...
- c语言第五次作业-指针-总结博客
本次作业亮点 1.1整体情况 本次作业主要是对上次的大作业利用指针进行改进,但是大部分同学并没有很好按照老师的要求对大作业进行改进,函数的分装性也做得不够好,由于是初步学习指针,大家在本次的作业改造中 ...
- C++ Profiler工具之初体验
http://www.cnblogs.com/likwo/archive/2012/12/20/2826988.html 转 http://www.cnblogs.com/lenolix/archiv ...
- 学习MVC中出现的一个BUG
BUG描述:No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.S ...
- 文本太长,用省略号显示的css样式
——html代码 <divid="d1" title="鼠标放上显示的文字"></div> ——css代码 #d1{ width:300 ...
- Bootstrap中的Affix插件
我们为什么要用bootstrap?因为懒!哦....不,是因为方便,呃...意思差不多. 今天来说说Affix这个插件,它可以使导航栏固定,免去了自己手写的麻烦,用着非常方便,废话不多说,下面是用法. ...