CF1006E.Military Problem

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 uu corresponds 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)(u_i, k_i) , where uiu_i is some officer, and kik_i is a positive integer.

To process the ii -th query imagine how a command from uiu_i spreads to the subordinates of uiu_i . 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)(u_i, k_i) , construct a sequence which describes the order in which officers will receive the command if the uiu_i -th officer spreads it. Return the kik_i -th element of the constructed list or -1 if there are fewer than kik_i elements in it.

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

输入格式

The first line of the input contains two integers nn and qq ( 2n2105,1q21052 \le n \le 2 \cdot 10^5, 1 \le q \le 2 \cdot 10^5 ) — the number of officers in Berland army and the number of queries.

The second line of the input contains n1n - 1 integers p2,p3,,pnp_2, p_3, \dots, p_n ( 1pi<i1 \le p_i < i ), where pip_i 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,kiu_i, k_i ) ( 1ui,kin1 \le u_i, k_i \le n ), where uiu_i is the index of the officer which starts spreading a command, and kik_i is the index of the required officer in the command spreading sequence.

输出格式

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

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

输入输出样例

  • 输入#1

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

    输出#1

    3
    6
    8
    -1
    9
    4
    
首页