CF1006E.Military Problem
普及/提高-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
In this problem you will have to help Berland army with organizing their command delivery system.
There are n 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 a is the direct superior of officer b, then we also can say that officer b is a direct subordinate of officer a.
Officer x is considered to be a subordinate (direct or indirect) of officer y if one of the following conditions holds:
- officer y is the direct superior of officer x;
- the direct superior of officer x is a subordinate of officer y.
For example, on the picture below the subordinates of the officer 3 are: 5,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 n vertices, in which vertex u corresponds to officer u. The parent of vertex u corresponds to the direct superior of officer u. The root (which has index 1) corresponds to the commander of the army.
Berland War Ministry has ordered you to give answers on q queries, the i-th query is given as (ui,ki), where ui is some officer, and ki is a positive integer.
To process the i-th query imagine how a command from ui spreads to the subordinates of ui. Typical DFS (depth first search) algorithm is used here.
Suppose the current officer is a and he spreads a command. Officer a chooses b — 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 a chooses the one having minimal index. Officer a gives a command to officer b. Afterwards, b uses exactly the same algorithm to spread the command to its subtree. After b finishes spreading the command, officer a chooses the next direct subordinate again (using the same strategy). When officer a cannot choose any direct subordinate who still hasn't received this command, officer a finishes spreading the command.
Let's look at the following example:

If officer 1 spreads a command, officers receive it in the following order: [1,2,3,5,6,8,7,9,4].
If officer 3 spreads a command, officers receive it in the following order: [3,5,6,8,7,9].
If officer 7 spreads a command, officers receive it in the following order: [7,9].
If officer 9 spreads a command, officers receive it in the following order: [9].
To answer the i-th query (ui,ki), construct a sequence which describes the order in which officers will receive the command if the ui-th officer spreads it. Return the ki-th element of the constructed list or -1 if there are fewer than ki elements in it.
You should process queries independently. A query doesn't affect the following queries.
本题中,你需要帮助贝尔兰军队构建其命令传递系统。
贝尔兰军队中共有 n 名军官。其中第 1 号军官是全军指挥官,他没有上级。其余每名军官恰好有一名直属上级。若军官 a 是军官 b 的直属上级,则我们也称军官 b 是军官 a 的直属下级。
若满足以下任一条件,则称军官 x 是军官 y 的下级(可以是直属下级,也可以是间接下级):
- 军官 y 是军官 x 的直属上级;
- 军官 x 的直属上级是军官 y 的下级。
例如,在下方图示中,军官 3 的下级为:5,6,7,8,9。
贝尔兰军队的组织结构满足:除指挥官外,每名军官均为军队指挥官的下级。
形式化地,我们将贝尔兰军队建模为一棵含 n 个顶点的树,其中顶点 u 对应军官 u;顶点 u 的父节点对应军官 u 的直属上级;根节点(编号为 1)对应军队指挥官。
贝尔兰国防部要求你回答 q 个查询,其中第 i 个查询形如 (ui,ki),其中 ui 是某位军官,ki 是一个正整数。
为处理第 i 个查询,请想象命令从军官 ui 向其所有下级传播的过程。此处采用典型的深度优先搜索(DFS)算法。
假设当前传播命令的军官为 a。军官 a 选择其一名尚未收到该命令的直属下级 b(即树中 a 的一个子节点)。若存在多个这样的直属下级,则 a 选择编号最小者。随后,a 将命令传达给 b;接着,b 使用完全相同的算法将其向下传播至自己的子树中。当 b 完成传播后,a 再次按相同策略选择下一个尚未收到命令的直属下级。当 a 无法再选出任何尚未收到命令的直属下级时,a 的命令传播过程结束。
我们来看如下示例:

若军官 1 发布命令,则军官们接收命令的顺序为:[1,2,3,5,6,8,7,9,4]。
若军官 3 发布命令,则军官们接收命令的顺序为:[3,5,6,8,7,9]。
若军官 7 发布命令,则军官们接收命令的顺序为:[7,9]。
若军官 9 发布命令,则军官们接收命令的顺序为:[9]。
为回答第 i 个查询 (ui,ki),请构造一个序列,表示当军官 ui 发布命令时,各位军官接收命令的顺序。返回该序列中第 ki 个元素;若序列长度小于 ki,则返回 −1。
各查询相互独立,前一查询的结果不影响后续查询。
输入格式
The first line of the input contains two integers n and q (2≤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−1 integers p2,p3,…,pn (1≤pi<i), where pi is the index of the direct superior of the officer having the index i. The commander has index 1 and doesn't have any superiors.
The next q lines describe the queries. The i-th query is given as a pair (ui,ki) (1≤ui,ki≤n), where ui is the index of the officer which starts spreading a command, and ki is the index of the required officer in the command spreading sequence.
输入的第一行包含两个整数 n 和 q(2≤n≤2⋅105,1≤q≤2⋅105)——分别表示 Berland 军队中军官的数量以及查询的数量。
输入的第二行包含 n−1 个整数 p2,p3,…,pn(1≤pi<i),其中 pi 表示编号为 i 的军官的直属上级的编号。最高指挥官编号为 1,且没有上级。
接下来的 q 行描述各个查询。第 i 个查询以一对数 (ui,ki)(1≤ui,ki≤n)给出,其中 ui 表示开始传达命令的军官编号,ki 表示命令传播序列中所要求的军官编号。
输出格式
Print q numbers, where the i-th number is the officer at the position ki in the list which describes the order in which officers will receive the command if it starts spreading from officer ui. Print "-1" if the number of officers which receive the command is less than ki.
You should process queries independently. They do not affect each other.
输出 q 个数字,其中第 i 个数字表示:若命令从警官 ui 开始传播,则在接收命令的警官列表中位于第 ki 位的警官编号;若接收命令的警官总数少于 ki,则输出 "-1"。
你需要独立处理每个查询,各查询之间互不影响。
输入输出样例
输入#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
输入解题思路,AI测评打分。不知道怎么写?