luogu#P12075. [OOI 2025] Dreaming is not harmful

[OOI 2025] Dreaming is not harmful

题目描述

In the wedding agency M., mass layoffs are planned. All employees are busy counting the days until, with a favorable turn of events, they can head the company instead of working.

The structure of the company represents a suspended tree with the root at vertex number 11. The immediate supervisor of employee number vv is the employee with number pvp_v. The level of competence of employee vv is defined by the parameter svs_v. This parameter is different for all employees. The higher the level of competence, the more useful the employee is to the company. Note that as a result of an opaque hiring process, it may happen that a less competent employee is the supervisor of a more competent one.

As a result of significant personnel restructuring, every day the CEO, who is at the root of the working hierarchy, will be fired. If there are employees left in the company, the most competent immediate subordinate will take their place. After that, the other subordinates of the former director will become subordinates of the new director. See the explanations in the examples for a better understanding of the condition.

Each employee easily calculated how many days it would take for them to become the CEO. Many were not ready to wait that long, as they would only get to be the director for one day! To speed up this process, they are ready to cancel one of their colleagues. The canceled employee's level of competence drops to 00, as no one is willing to interact with them anymore.

You will need to answer qq queries. In the kk-th query, employee number vkv_k is interested in the minimum number of days until they can head the company if they are willing to cancel exactly one employee. All queries are imaginary and independent, and the real levels of competence of the employees remain unchanged for all queries.

输入格式

The first line contains two integers nn, qq (2n3000002 \le n \le 300\,000, 1qn1 \le q \le n) --- the number of employees and the number of queries.

The second line contains n1n - 1 integers p2p_2, p3p_3, \ldots, pnp_{n} (1pi<i1 \le p_i < i) --- the immediate supervisors of employees numbered from 22 to nn.

The third line contains nn integers s1s_1, s2s_2, \ldots, sns_n (1sin1 \le s_i \le n) --- the levels of competence of the employees. It is guaranteed that they are all different.

The fourth line contains qq integers v1v_1, v2v_2, \dots, vqv_q (1vin1 \le v_i \le n) --- the promotion queries. It is guaranteed that all numbers viv_i are distinct.

输出格式

Output qq integers separated by spaces --- the minimum number of days after which employees v1,v2,,vqv_1, v_2, \dots, v_q can become directors.

5 4
1 2 2 1
3 5 1 2 4
5 3 1 4
1 3 0 2

提示

Note

In the test example, the fifth employee can head the company in 1 day. To do this, it is enough to cancel the second employee. The structure of the company will change as follows:

The third employee can head the company in 3 days. To do this, it is enough to cancel the fifth or fourth employee. If the fifth is canceled, the structure of the company will change as follows:

The first employee is already the head of the company, so the answer to the corresponding query is 00.

The fourth employee can become the head of the company in two days. It is enough, similarly to the previous example, to cancel the fifth employee.

Scoring

The tests for this problem consist of nine groups. Points for each group are given only if all tests of the group and all tests of the required groups are passed. Please note that passing the example tests is not required for some groups. Offline-evaluation\textbf{Offline-evaluation} means that the results of testing your solution on this group will only be available after the end of the competition.

Supervisors* of an employee --- the set of their immediate supervisor and all supervisors of their immediate supervisor.