CF1062E.Company
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The company X has n employees numbered from 1 through n. Each employee u has a direct boss pu (1≤pu≤n), except for the employee 1 who has no boss. It is guaranteed, that values pi form a tree. Employee u is said to be in charge of employee v if u is the direct boss of v or there is an employee w such that w is in charge of v and u is the direct boss of w. Also, any employee is considered to be in charge of himself.
In addition, for each employee u we define it's level lv(u) as follow:
- lv(1)=0
- lv(u)=lv(pu)+1 for u=1
In the near future, there are q possible plans for the company to operate. The i-th plan consists of two integers li and ri, meaning that all the employees in the range [li,ri], and only they, are involved in this plan. To operate the plan smoothly, there must be a project manager who is an employee in charge of all the involved employees. To be precise, if an employee u is chosen as the project manager for the i-th plan then for every employee v∈[li,ri], u must be in charge of v. Note, that u is not necessary in the range [li,ri]. Also, u is always chosen in such a way that lv(u) is as large as possible (the higher the level is, the lower the salary that the company has to pay the employee).
Before any plan is operated, the company has JATC take a look at their plans. After a glance, he tells the company that for every plan, it's possible to reduce the number of the involved employees exactly by one without affecting the plan. Being greedy, the company asks JATC which employee they should kick out of the plan so that the level of the project manager required is as large as possible. JATC has already figured out the answer and challenges you to do the same.
公司 X 有 n 名员工,编号从 1 到 n。除编号为 1 的员工(其无直属上司)外,每名员工 u 均有一名直属上司 pu(1≤pu≤n)。题目保证所有 pi 的取值构成一棵树。若员工 u 是员工 v 的直属上司,或存在某员工 w,使得 w 管辖 v 且 u 是 w 的直属上司,则称 u 管辖 v。此外,任何员工均视为管辖自身。
另外,对每名员工 u,定义其层级 lv(u) 如下:
- lv(1)=0
- 当 u=1 时,lv(u)=lv(pu)+1
近期,公司共有 q 种可能的运营方案。第 i 个方案由两个整数 li 和 ri 组成,表示仅编号在区间 [li,ri] 内的员工参与该方案。为确保方案顺利执行,必须指定一名项目经理,该经理需管辖所有参与员工。换言之,若第 i 个方案选定员工 u 作为项目经理,则对每个 v∈[li,ri],都必须满足 u 管辖 v。注意:u 不一定属于区间 [li,ri];且总是选择满足条件的 u 中 lv(u) 尽可能大者(层级越高,公司所需支付的薪资越低)。
在任一方案执行前,公司请 JATC 审阅这些方案。JATC 略作浏览后指出:对每个方案,均可恰好剔除一名参与员工,而不影响方案的可行性(即仍存在满足条件的项目经理)。出于贪心考虑,公司进一步询问 JATC:应剔除哪一名员工,才能使所需项目经理的层级 lv(u) 达到最大?JATC 已得出答案,并向你发起挑战,邀请你独立求解。
输入格式
The first line contains two integers n and q (2≤n≤100000, 1≤q≤100000) — the number of employees and the number of plans, respectively.
The second line contains n−1 integers p2,p3,…,pn (1≤pi≤n) meaning pi is the direct boss of employee i.
It is guaranteed, that values pi form a directed tree with the root of 1.
Each of the following q lines contains two integers li and ri (1≤li<ri≤n) — the range of the employees, involved in the corresponding plan.
第一行包含两个整数 n 和 q(2≤n≤100000,1≤q≤100000)—— 分别表示员工数量和计划数量。
第二行包含 n−1 个整数 p2,p3,…,pn(1≤pi≤n),表示员工 i 的直属上司为 pi。
保证这些 pi 值构成一棵以 1 为根的有向树。
接下来的 q 行中,每行包含两个整数 li 和 ri(1≤li<ri≤n)—— 表示对应计划所涉及的员工编号范围。
输出格式
Print q lines, each containing two integers — the number of the employee which should be kicked from the corresponding plan and the maximum possible level of the project manager in that case.
If there are more than one way to choose that employee, print any of them.
输出 q 行,每行包含两个整数——对应计划中应被解雇的员工编号,以及此时项目主管可能达到的最大级别。
如果存在多种选择该员工的方式,输出任意一种即可。
输入输出样例
输入#1
11 5 1 1 3 3 3 4 2 7 7 6 4 6 4 8 1 11 9 11 8 11
输出#1
4 1 8 1 1 0 11 3 8 1
说明/提示
In the example:

In the first query, we can choose whether 4 or 5 or 6 and the project manager will be 3.
In the second query, if we choose any employee other than the employee 8, the project manager will be 1. If we choose 8, the project manager will be 3. Since lv(3)=1>lv(1)=0, choosing 8 is the best strategy.
In the third query, no matter how we choose the employee, the project manager will always be 1.
In the fourth query, if we choose 9 or 10 then the project manager will be 3. If we choose 11 then the project manager will be 7. Since lv(7)=3>lv(3)=1, we choose 11 as the answer.
在示例中:

在第一个查询中,我们可以选择员工 4、5 或 6 中的任意一人,此时项目经理为 3。
在第二个查询中,若选择除员工 8 外的任意其他员工,则项目经理为 1;若选择员工 8,则项目经理为 3。由于 lv(3)=1>lv(1)=0,因此选择员工 8 是最优策略。
在第三个查询中,无论选择哪位员工,项目经理始终为 1。
在第四个查询中,若选择员工 9 或 10,则项目经理为 3;若选择员工 11,则项目经理为 7。由于 lv(7)=3>lv(3)=1,因此选择员工 11 作为答案。
输入解题思路,AI测评打分。不知道怎么写?