CF1062E.Company

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The company XX has nn employees numbered from 11 through nn. Each employee uu has a direct boss pup_u (1≤pu≤n1 \le p_u \le n), except for the employee 11 who has no boss. It is guaranteed, that values pip_i form a tree. Employee uu is said to be in charge of employee vv if uu is the direct boss of vv or there is an employee ww such that ww is in charge of vv and uu is the direct boss of ww. Also, any employee is considered to be in charge of himself.

In addition, for each employee uu we define it's level lv(u)lv(u) as follow:

  • lv(1)=0lv(1)=0
  • lv(u)=lv(pu)+1lv(u)=lv(p_u)+1 for u≠1u \neq 1

In the near future, there are qq possible plans for the company to operate. The ii-th plan consists of two integers lil_i and rir_i, meaning that all the employees in the range [li,ri][l_i, r_i], 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 uu is chosen as the project manager for the ii-th plan then for every employee v∈[li,ri]v \in [l_i, r_i], uu must be in charge of vv. Note, that uu is not necessary in the range [li,ri][l_i, r_i]. Also, uu is always chosen in such a way that lv(u)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.

公司 XX 有 nn 名员工,编号从 11 到 nn。除编号为 11 的员工(其无直属上司)外,每名员工 uu 均有一名直属上司 pup_u(1≤pu≤n1 \le p_u \le n)。题目保证所有 pip_i 的取值构成一棵树。若员工 uu 是员工 vv 的直属上司,或存在某员工 ww,使得 ww 管辖 vv 且 uu 是 ww 的直属上司,则称 uu 管辖 vv。此外,任何员工均视为管辖自身。

另外,对每名员工 uu,定义其层级 lv(u)lv(u) 如下:

  • lv(1)=0lv(1)=0
  • 当 u≠1u \neq 1 时,lv(u)=lv(pu)+1lv(u)=lv(p_u)+1

近期,公司共有 qq 种可能的运营方案。第 ii 个方案由两个整数 lil_i 和 rir_i 组成,表示仅编号在区间 [li,ri][l_i, r_i] 内的员工参与该方案。为确保方案顺利执行,必须指定一名项目经理,该经理需管辖所有参与员工。换言之,若第 ii 个方案选定员工 uu 作为项目经理,则对每个 v∈[li,ri]v \in [l_i, r_i],都必须满足 uu 管辖 vv。注意:uu 不一定属于区间 [li,ri][l_i, r_i];且总是选择满足条件的 uu 中 lv(u)lv(u) 尽可能大者(层级越高,公司所需支付的薪资越低)。

在任一方案执行前,公司请 JATC 审阅这些方案。JATC 略作浏览后指出:对每个方案,均可恰好剔除一名参与员工,而不影响方案的可行性(即仍存在满足条件的项目经理)。出于贪心考虑,公司进一步询问 JATC:应剔除哪一名员工,才能使所需项目经理的层级 lv(u)lv(u) 达到最大?JATC 已得出答案,并向你发起挑战,邀请你独立求解。

输入格式

The first line contains two integers nn and qq (2≤n≤100 0002 \le n \le 100\,000, 1≤q≤100 0001 \le q \le 100\,000) — the number of employees and the number of plans, respectively.

The second line contains n−1n-1 integers p2,p3,…,pnp_2, p_3, \dots, p_n (1≤pi≤n1 \le p_i \le n) meaning pip_i is the direct boss of employee ii.

It is guaranteed, that values pip_i form a directed tree with the root of 11.

Each of the following qq lines contains two integers lil_i and rir_i (1≤li<ri≤n1 \le l_i \lt r_i \le n) — the range of the employees, involved in the corresponding plan.

第一行包含两个整数 nn 和 qq(2≤n≤100 0002 \le n \le 100\,000,1≤q≤100 0001 \le q \le 100\,000)—— 分别表示员工数量和计划数量。

第二行包含 n−1n-1 个整数 p2,p3,…,pnp_2, p_3, \dots, p_n(1≤pi≤n1 \le p_i \le n),表示员工 ii 的直属上司为 pip_i。

保证这些 pip_i 值构成一棵以 11 为根的有向树。

接下来的 qq 行中,每行包含两个整数 lil_i 和 rir_i(1≤li<ri≤n1 \le l_i \lt r_i \le n)—— 表示对应计划所涉及的员工编号范围。

输出格式

Print qq 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.

输出 qq 行,每行包含两个整数——对应计划中应被解雇的员工编号,以及此时项目主管可能达到的最大级别。

如果存在多种选择该员工的方式,输出任意一种即可。

输入输出样例

  • 输入#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 44 or 55 or 66 and the project manager will be 33.

In the second query, if we choose any employee other than the employee 88, the project manager will be 11. If we choose 88, the project manager will be 33. Since lv(3)=1>lv(1)=0lv(3)=1 \gt lv(1)=0, choosing 88 is the best strategy.

In the third query, no matter how we choose the employee, the project manager will always be 11.

In the fourth query, if we choose 99 or 1010 then the project manager will be 33. If we choose 1111 then the project manager will be 77. Since lv(7)=3>lv(3)=1lv(7)=3 \gt lv(3)=1, we choose 1111 as the answer.

在示例中:

在第一个查询中,我们可以选择员工 44、55 或 66 中的任意一人,此时项目经理为 33。

在第二个查询中,若选择除员工 88 外的任意其他员工,则项目经理为 11;若选择员工 88,则项目经理为 33。由于 lv(3)=1>lv(1)=0lv(3)=1 \gt lv(1)=0,因此选择员工 88 是最优策略。

在第三个查询中,无论选择哪位员工,项目经理始终为 11。

在第四个查询中,若选择员工 99 或 1010,则项目经理为 33;若选择员工 1111,则项目经理为 77。由于 lv(7)=3>lv(3)=1lv(7)=3 \gt lv(3)=1,因此选择员工 1111 作为答案。

输入解题思路,AI测评打分。不知道怎么写?

首页