#P12075. CEO Replacement Optimization

    ID: 14183 Type: Default 1000ms 256MiB

CEO Replacement Optimization

CEO Replacement Optimization

In the wedding agency M., the company is organized as a suspended tree (i.e. a rooted tree where every node aside from the root has a unique parent) with vertex 1 as the root. For each employee v, its immediate supervisor is employee pv (given for v > 1). Each employee has a unique competence level sv (all values are distinct). Due to an opaque hiring process the supervision relation does not necessarily follow competence, so it is possible that a supervisor is less competent than a subordinate.

Every day the current CEO (starting with employee 1) is fired. Then, if there is at least one subordinate, the most competent (i.e. with maximum $s$) immediate subordinate of the fired employee will become the new CEO. Immediately after that, all the other immediate subordinates of the fired employee become subordinates of the new CEO. In other words, if at some step the current CEO v has a children list $C(v)$, after firing v the new CEO is

u=argmaxwC(v)  sw,u = \arg\max_{w \in C(v)}\; s_w,

and for every other $w \in C(v)$ the parent is set to $u$.

Every employee has computed how many days it would take for them to become CEO with this process. However, many employees are not willing to wait that long – if they ever become CEO, they will only have one day in the job. To speed things up, each employee is ready to cancel exactly one colleague. Canceling an employee sets that employee's competence to $0$, so they are never chosen in a promotion round. Note that each query is independent; canceling in one query does not affect the original competence values.

You are given $q$ queries. In the $k$th query, employee $v_k$ wonders: if they cancel exactly one colleague (and they choose optimally whom to cancel among those other than themselves), what is the minimum number of days until they become CEO?

Note: The formulas are given in \( \LaTeX \) format.

inputFormat

The first line contains two integers $n$ and $q$, the number of employees and the number of queries, respectively.

The second line contains $n-1$ integers: $p_2, p_3, \ldots, p_n$, where $p_v$ is the immediate supervisor of employee $v$.

The third line contains $n$ distinct integers: $s_1, s_2, \ldots, s_n$, where $s_v$ is the competence of employee $v$. Higher values indicate higher competence.

Each of the following $q$ lines contains one integer $v_k$, representing a query for employee $v_k$ (note that an employee will never cancel themselves).

outputFormat

For each query, output a single integer—the minimum number of days until employee $v_k$ becomes CEO if they cancel exactly one colleague optimally.

sample

3 3
1 1
7 10 5
1
2
3
0

1 1

</p>