E. Military Problem

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In this problem you will have to help Berland army with organizing their command delivery system.

There are nn officers in Berland army. The first officer is the commander of the army, and he does not have any superiors. Every other officer has exactly one direct superior. If officer aa is the direct superior of officer bb, then we also can say that officer bb is a direct subordinate of officer aa.

Officer xx is considered to be a subordinate (direct or indirect) of officer yy if one of the following conditions holds:

  • officer yy is the direct superior of officer xx;
  • the direct superior of officer xx is a subordinate of officer yy.

For example, on the picture below the subordinates of the officer 33 are: 5,6,7,8,95,6,7,8,9.

The structure of Berland army is organized in such a way that every officer, except for the commander, is a subordinate of the commander of the army.

Formally, let's represent Berland army as a tree consisting of nn vertices, in which vertex uu corresponds to officer uu. The parent of vertex uucorresponds to the direct superior of officer uu. The root (which has index 11) corresponds to the commander of the army.

Berland War Ministry has ordered you to give answers on qq queries, the ii-th query is given as (ui,ki)(ui,ki), where uiui is some officer, and kiki is a positive integer.

To process the ii-th query imagine how a command from uiui spreads to the subordinates of uiui. Typical DFS (depth first search) algorithm is used here.

Suppose the current officer is aa and he spreads a command. Officer aa chooses bb — one of his direct subordinates (i.e. a child in the tree) who has not received this command yet. If there are many such direct subordinates, then aa chooses the one having minimal index. Officer aa gives a command to officer bb. Afterwards, bb uses exactly the same algorithm to spread the command to its subtree. After bb finishes spreading the command, officer aa chooses the next direct subordinate again (using the same strategy). When officer aa cannot choose any direct subordinate who still hasn't received this command, officer aa finishes spreading the command.

Let's look at the following example:

If officer 11 spreads a command, officers receive it in the following order: [1,2,3,5,6,8,7,9,4][1,2,3,5,6,8,7,9,4].

If officer 33 spreads a command, officers receive it in the following order: [3,5,6,8,7,9][3,5,6,8,7,9].

If officer 77 spreads a command, officers receive it in the following order: [7,9][7,9].

If officer 99 spreads a command, officers receive it in the following order: [9][9].

To answer the ii-th query (ui,ki)(ui,ki), construct a sequence which describes the order in which officers will receive the command if the uiui-th officer spreads it. Return the kiki-th element of the constructed list or -1 if there are fewer than kiki elements in it.

You should process queries independently. A query doesn't affect the following queries.

Input

The first line of the input contains two integers nn and qq (2≤n≤2⋅105,1≤q≤2⋅1052≤n≤2⋅105,1≤q≤2⋅105) — the number of officers in Berland army and the number of queries.

The second line of the input contains n−1n−1 integers p2,p3,…,pnp2,p3,…,pn (1≤pi<i1≤pi<i), where pipi is the index of the direct superior of the officer having the index ii. The commander has index 11 and doesn't have any superiors.

The next qq lines describe the queries. The ii-th query is given as a pair (ui,kiui,ki) (1≤ui,ki≤n1≤ui,ki≤n), where uiui is the index of the officer which starts spreading a command, and kiki is the index of the required officer in the command spreading sequence.

Output

Print qq numbers, where the ii-th number is the officer at the position kiki in the list which describes the order in which officers will receive the command if it starts spreading from officer uiui. Print "-1" if the number of officers which receive the command is less than kiki.

You should process queries independently. They do not affect each other.

Example

input

Copy

9 6
1 1 1 3 5 3 5 7
3 1
1 5
3 4
7 3
1 8
1 9

output

Copy

3
6
8
-1
9
4

题解:记录每个节点的时间戳和其有几个子节点即可;DFS

AC代码为:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m, a, b, c = 1, d;
bool vis[maxn] = { false };
vector<int> graph[maxn];
int level[maxn],pos[maxn],child[maxn];
int dfs(int src) 
{
    vis[src] = true;
    pos[c] = src;
    level[src] = c++;
    int temp = 1;
    for (int i = 0; i<graph[src].size(); i++) 
    {
        if (!vis[graph[src][i]]) temp += dfs(graph[src][i]);
    }
    return  child[src] = temp;
}
int main() 
{
    cin >> n >> m;
    for (int i = 2; i <= n; i++) 
    {
        cin >> a;
        graph[a].push_back(i);
    }
    dfs(1);
    while (m--) 
    {
        cin >> a >> b;
        if (child[a]<b) cout << -1 << endl;
        else cout << pos[level[a] + b - 1] << endl;
    }
    return 0;
}

CodeForces1006E- Military Problem的更多相关文章

  1. Military Problem CodeForces 1006E (dfs序)

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

  2. 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 ...

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

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

  4. Military Problem CodeForces - 1006E(dfs搜一下 标记一下)

    题意: 就是有一颗树  然后每次询问 父结点 的 第k个结点是不是他的子嗣...是的话就输出这个子嗣..不是 就输出-1 解析: 突然想到后缀数组的sa 和 x的用法..就是我们可以用一个id标记当前 ...

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

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

  6. 树&图 记录

    A - Lake Counting POJ - 2386 最最最最最基础的dfs 挂这道题为了提高AC率(糖水不等式 B - Paint it really, really dark gray Cod ...

  7. Codeforces Div3 #498 A-F

                                                                               . A. Adjacent Replacement ...

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

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

  9. DFS序专题

    牛客专题之DFS序 简介 dfs序: 每个节点在dfs深度优先遍历中的进出栈的时间序列,也就是tarjan算法中的dfn数组. 画个图理解一下: 这棵树的dfs序:1 3 2 4 2 5 6 7 6 ...

随机推荐

  1. JVM原理速记复习Java虚拟机总结思维导图面试必备

    良心制作,右键另存为保存 喜欢可以点个赞哦 Java虚拟机 一.运行时数据区域 线程私有 程序计数器 记录正在执行的虚拟机字节码指令的地址(如果正在执行的是Native方法则为空),是唯一一个没有规定 ...

  2. Unity 横版2D移动跳跃问题——关于一段跳与二段跳

    1.初始条件: 1.角色只绑定一个碰撞体,移动时施加力或给予速度,用跳跃次数JumpTimes或者bool值OnGround判断是否在地面. 2.只用一个tilemap搭建2D场景,因此所有tilem ...

  3. PHPExcel数据导入(含图片)

    PHPExcel是一个PHP类库,用来帮助我们简单.高效实现从Excel读取Excel的数据和导出数据到Excel. 首先下载压缩包: https://codeload.github.com/PHPO ...

  4. 为企业应用开发提速,写给企业IT部门的低代码开发基础知识

    简介:应用程序开发长期以来一直是IT部门和业务部门面临的问题. IT部门总是被新的应用程序需求弄得不堪重负.他们不可能完成业务部门想要完成的每一个项目. 同时,业务部门的用户厌倦了等待,并开始完全绕过 ...

  5. 别翻了,这篇文章就是要让你入门java多线程!

    目录 1. 理解线程与进程 2.理解并行和并发 3.特殊的一个单线程:主线程(Main线程) 4. 创建多线程的四种方式 5.使用匿名内部类方式创建线程 6.线程安全问题 7.解决线程安全问题:线程同 ...

  6. Ocelot学习笔记

    最近因工作需要,开始学习Ocelot.首先简单介绍一下,Ocelot是一个基于.net core的开源webapi 服务网关项目,目前已经支持了IdentityServer认证.根据 作者介绍,Oce ...

  7. Linux基础命令复习01

    一.Linux中的基本查看.查找命令: 1.ls 查看目录信息:  -l  #查看属性,以长格式显示 -d #查看本身属性 -A #显示包括以.开头的隐藏文档 -h #提供易读的单位 -R  #表示递 ...

  8. zabbix template

    1. template 是分层级的,所有很多修改需要到父级去修改,而且影响所有子级.

  9. 扛把子组20191114-4 Beta发布用户使用报告

    此作业的要求参见:http://edu.cnblogs.com/campus/nenu/2019fall/homework/10007 小组情况: 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩 ...

  10. 新闻实时分析系统-Kafka分布式集群部署

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...