Codeforce 1006E

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)(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

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

Output

3
6
8
-1
9
4

 思路:

每次询问时都进行dfs会超时,所以想办法先保存结果。

设置了三个数组

d[i]:表示根节点与结点i的距离

o[i]:表示与根结点距离为o[i]的结点

x[i]:表示结点i的子孙结点数

对每次询问(ui,ki),可以先判断结点ui的子孙节点数是否小于ki(即x[ui]<ki),如果小于,则输出-1

否则,输出与根节点距离为d[ui]+ki的结点编号(即o[d[ui]+ki])

而初始化这三个数组,只要从根节点出发做一次bfs即可

#include<cstdio>
#include<vector>
#include<string.h>
using namespace std;
static const int MAX = 200050;
vector<int> G[MAX];
int n, q;
int d[MAX]; //d[i]表示结点i距离根节点的长度
int o[MAX]; //与根节点距离o[i]的结点
int x[MAX]; //x[i]表示结点i的儿子结点个数
int l = 1;  //当前结点距根节点距离
void dfs(int s)
{
    d[s] = l;
    o[l] = s;
    x[s] = 1;
    l++;
    for (int i = 0; i < G[s].size(); i++)
    {
        int t = G[s][i];
        dfs(t);
        x[s] += x[t];   //当前结点的儿子数量 = 其儿子结点的儿子结点数量之和
    }
}

int main()
{
    while (scanf("%d %d", &n, &q) != EOF)
    {
        memset(d, 0, sizeof(d));
        memset(o, 0, sizeof(o));
        memset(x, 1, sizeof(x));
        for (int i = 2; i <= n; i++)
        {
            int p;
            scanf("%d", &p);
            G[p].push_back(i);
        }
        dfs(1);
        for (int i = 0; i < q; i++)
        {
            int u, k;
            scanf("%d %d", &u, &k);
            if (x[u] < k)
                printf("-1\n");
            else
                printf("%d\n", o[d[u] + k - 1]);
        }
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值