题意问,这个点的然后求子树的第i个节点。

这道题是个非常明显的DFS序:

我们只需要记录DFS的入DFS的时间,以及出DFS的时间,也就是DFS序,

然后判断第i个子树是否在这个节点的时间段之间。

最后用一个映射,把相应DFS序对应的节点编号写入。即可

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int maxx = 2e5+;
vector<int>G[maxx];
int cnt=;
int st[maxx],ed[maxx],id[maxx];
void dfs(int x){
cnt++;
st[x]=cnt;//DFS序
id[cnt]=x;//x对应的DFS序
for (int i=;i<G[x].size();i++){
dfs(G[x][i]);
}
ed[x]=cnt;//x对应的DFS结束
}
int main(){
int n,q;
int tmp;
scanf("%d%d",&n,&q);
cnt=;
for (int i=;i<=n;i++){
scanf("%d",&tmp);
G[tmp].push_back(i);
}
dfs();
int tmp2;
while(q--){
scanf("%d%d",&tmp,&tmp2);
if (st[tmp]+tmp2-<=ed[tmp]){
printf("%d\n",id[st[tmp]+tmp2-]);
}else {
printf("-1\n");
}
}
return ;
}

Codeforces Round #498 (Div. 3)--E. Military Problem的更多相关文章

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

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

  2. Codeforces Round #367 (Div. 2) C. Hard problem

    题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...

  3. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题

    Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...

  4. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  5. Codeforces Round #498 (Div. 3) 简要题解

    [比赛链接] https://codeforces.com/contest/1006 [题解] Problem A. Adjacent Replacements        [算法] 将序列中的所有 ...

  6. Codeforces Round #361 (Div. 2) C.NP-Hard Problem

    题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...

  7. Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题

    C. NP-Hard Problem 题目连接: http://www.codeforces.com/contest/688/problem/C Description Recently, Pari ...

  8. CodeForces Round #498 Div.3 A. Adjacent Replacements

    http://codeforces.com/contest/1006/problem/A Mishka got an integer array aa of length nn as a birthd ...

  9. Codeforces Round #603 (Div. 2) A. Sweet Problem(数学)

    链接: https://codeforces.com/contest/1263/problem/A 题意: You have three piles of candies: red, green an ...

随机推荐

  1. javascript基础修炼(4)——UMD规范的代码推演

    javascript基础修炼(4)--UMD规范的代码推演 1. UMD规范 地址:https://github.com/umdjs/umd UMD规范,就是所有规范里长得最丑的那个,没有之一!!!它 ...

  2. Unity的四元素与Vector3的乘积的含义

    Quaternion.Euler(x,y,z)含义是按照每个旋转轴以x,y,z旋转度旋转,例子:Quaternion.Euler(45,45,45) Quaternion与Vector3的右乘操作(* ...

  3. mac IntelliJ Idea添加schema和dtd约束提示

    打开设置 找到Schemac and DTDs 配置约束文件

  4. Ext.override

    Ext.override:4种情况 如果target是使用Ext.define声明的一个类,给出overrides那个类的override方法被调用(看Ext.Base.override) If th ...

  5. 【代码笔记】Web-CSS-CSS Float(浮动)

    一, 效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  6. SAP MM ME1M报表结果不科学?

    SAP MM ME1M报表结果不科学? 做过SAP MM顾问的都知道,报表ME1M可以查询物料的info record列表,即是说可以以列表的形式批量显示多个物料的采购价格主数据. 但是这个报表有个不 ...

  7. nodejs+express+mongodb写api接口的简单尝试

    1:启动mongodb服务 我的mongoDB的安装目录:E:\mongoDB\bin,版本:3.4.9 打开cmd  -> e:(进入e盘) -> cd mongoDB/bin(进入mo ...

  8. Oracle 12c RAC 静默安装文档

    参考文档: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/cwlin/index.html https://docs. ...

  9. SQL Server 2008初次启动

    一.关于安装 SQL Server 数据库的安装,经过自己的安装,总体还是比较容易,没有太多难度,安装包在网上也有很多,在此,就跳过安装的这一步. 二.初次启动SQL Server 安装完成数据库后, ...

  10. 英语进阶系列-A05-英语升级练习三

    古诗背诵 要求:认真背诵和朗读,然后翻译成现代文,并绘制图像描述图中的意向,时间限制到10 minutes.另外,从中找出英文单词,并记录. 例如:慈母 = kind mother,手 = hand, ...