#P9988. [Ynoi2079] 2stmo

    ID: 10171 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>Special JudgeO2优化Ynoi2079

[Ynoi2079] 2stmo

Problem Description

Given a rooted tree with nn vertices, the vertices are numbered 1,,n1,\dots,n, where 11 is the root. f2,,fnf_2,\dots,f_n denote the parents of vertices 2,,n2,\dots,n, respectively.

Given mm pairs of integers a1,b1,,am,bma_1,b_1,\dots,a_m,b_m, you need to construct a permutation p1,,pmp_1,\dots,p_m of 1,,m1,\dots,m such that the cost of this permutation does not exceed 4×1094\times 10^9.

The cost of a permutation is defined as:

$|S(a_{p_1})|+|S(b_{p_1})|+\sum\limits_{i=2}^m |S(a_{p_{i-1}})\oplus S(a_{p_i})|+|S(b_{p_{i-1}})\oplus S(b_{p_i})|$

Here, S(x)S(x) is the set of vertices in the subtree rooted at xx, A|A| is the number of elements in set AA, and ABA\oplus B is the symmetric difference of sets AA and BB (that is, the set of elements that appear in exactly one of AA and BB).

Input Format

The first line contains two integers n,mn,m.

The second line contains n1n-1 integers f2,,fnf_2,\dots,f_n.

The next mm lines each contain two integers representing ai,bia_i,b_i, for i=1,,mi=1,\dots,m.

Output Format

Output mm lines, each with one integer, representing p1,,pmp_1,\dots,p_m in order.

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

Hint

Idea: nzhtl1477, Solution: ccz181078, Code: ccz181078, Data: ccz181078.

For 25%25\% of the testdata, n,m104n,m\le 10^4.

For 50%50\% of the testdata, n,m2×105n,m\le 2\times 10^5.

For 75%75\% of the testdata, n,m5×105n,m\le 5\times 10^5.

For 100%100\% of the testdata, 1n,m1061\le n,m\le 10^6, 1fii11\le f_i\le i-1, 1ai,bin1\le a_i,b_i\le n.

Constraints.

Translated by ChatGPT 5