题意:

  就是有一颗树  然后每次询问 父结点 的 第k个结点是不是他的子嗣。。。是的话就输出这个子嗣。。不是 就输出-1

解析:

  突然想到后缀数组的sa 和 x的用法。。就是我们可以用一个id标记当前结点的等级 用idx标记等级ans是哪一个结点。。然后用en标记结点u的子嗣的结束结点

  然后每次判断一下就可以了

  

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e6+, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
typedef long long LL;
int vis[maxn], id[maxn], idx[maxn], en[maxn];
vector<int> G[maxn]; int n, q;
int ans;
void dfs(int u)
{
id[ans] = u;
idx[u] = ans;
// cout<< u << " << " " << idx[u] <<endl;
// if(ans == n) return;
for(int i=; i<G[u].size(); i++)
{
int v = G[u][i];
ans++;
dfs(v);
}
en[u] = ans;
} int main()
{
rd(n), rd(q);
int tmp;
ans = ;
for(int i=; i<=n; i++)
{
rd(tmp);
G[tmp].push_back(i);
}
dfs();
int u, k;
// for(int i=1; i<=n; i++)
// cout<< i << " " <<idx[i] << " " << en[i] <<endl; for(int i=; i<q; i++)
{
rd(u), rd(k);
int temp = idx[u];
if(id[temp+k-] == || idx[id[temp+k-]] > en[u])
printf("-1\n");
else
printf("%d\n", id[temp+k-]);
} return ;
}

Military Problem CodeForces - 1006E(dfs搜一下 标记一下)的更多相关文章

  1. Military Problem CodeForces 1006E (dfs序)

    J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...

  2. BZOJ 2435: [Noi2011]道路修建 dfs搜图

    2435: [Noi2011]道路修建 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他 ...

  3. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  4. B - Save the problem! CodeForces - 867B 构造题

    B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...

  5. CodeForces 1006E Military Problem(DFS,树的选择性遍历)

    http://codeforces.com/contest/1006/problem/E 题意: 就是给出n,m,共n个点[1,n],m次询问.第二行给出n-1个数a[i],2<=i<=n ...

  6. Codeforces Round #498 (Div. 3) E. Military Problem (DFS)

    题意:建一颗以\(1\)为根结点的树,询问\(q\)次,每次询问一个结点,问该结点的第\(k\)个子结点,如果不存在则输出\(-1\). 题解:该题数据范围较大,需要采用dfs预处理的方法,我们从结点 ...

  7. Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树

    E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...

  8. Codeforces Round #498 (Div. 3)--E. Military Problem

    题意问,这个点的然后求子树的第i个节点. 这道题是个非常明显的DFS序: 我们只需要记录DFS的入DFS的时间,以及出DFS的时间,也就是DFS序, 然后判断第i个子树是否在这个节点的时间段之间. 最 ...

  9. Cyclic Components CodeForces - 977E(DFS)

    Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and  ...

随机推荐

  1. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  2. jqgrid 将列头设置为超链接或按钮

    有时,需要将某个列头设置为超链接或按钮,点击超链接或按钮能够跳转至其他页面(或执行一个事件操作). 可以把 label 值设置成一个a标签或button 代码如下: colModel: [{ labe ...

  3. 第16章 STM32中断应用概览

    第16章     STM32中断应用概览 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...

  4. .NET Core中向已存在文件的特定位置写入数据

    本例使用.NET Core向一个文本文件中的特定位置写入数据,来模拟文件上传中的断点续传是如何在服务器端实现的. 新建一个.NET Core控制台项目FileContinueToWrite,其Prog ...

  5. Spring 整合Mybatis实例

    演示样例下载地址:http://download.csdn.net/detail/geloin/4506640 本文基于Spring 注解,让Spring跑起来.本文使用Mysql数据库. (1) 导 ...

  6. Modelsim SE 和 Quartus II 编译器(综合器)的区别

    当对目标模块进行RTL描述后,习惯先会用Modelsim做一下功能仿真.当我们写好Tensbench文件,直接在Modelsim SE中对源文件(design和Testbench)进行编译时,如果源文 ...

  7. 2017-2018-2 20155203《网络对抗技术》 Exp7:网络欺诈防范

    1.基础问题回答 (1)通常在什么场景下容易受到DNS spoof攻击 连接无线网络,和恶意攻击者处在同一局域网下. (2)在日常生活工作中如何防范以上两攻击方法 首先决不去点击浏览器都认为不安全的网 ...

  8. 20155210 Exp5 MSF基础应用

    Exp5 MSF基础应用 一个主动攻击实践,MS08-067 首先利用msfconsole启用msf终端 然后利用search MS08-067搜索漏洞,会显示相应漏洞模块 如图: 根据上图,我们输入 ...

  9. 20155333 《网络对抗》Exp3 免杀原理与实践

    20155333 <网络对抗>Exp3 免杀原理与实践 基础问题回答 (1)杀软是如何检测出恶意代码的? 基于特征码的检测: 启发式恶意软件检测: 基于行为的恶意软件检测. (2)免杀是做 ...

  10. WPF应用

    代码 private void button1_Click(object sender, RoutedEventArgs e) { calculate sa = new calculate(int.P ...