#P7548. [BJWC2017] 星际穿越

[BJWC2017] 星际穿越

Description

The Federation has NN planets, and the new administrative structure is a binary tree. The administrative center of the Federation is the capital. All other planets are divided into at most two regions, and inside each region the structure is the same.

Before this administrative reform, each planet stationed a space fleet. The combat power of the fleet stationed on planet ii is FiF_i.

During the reform, the Space Defense Council needs to adjust the stationed locations of fleets to satisfy the military rule that the fleet stationed at the center of each administrative region must be the strongest fleet (by combat power) within that region. Also, according to the military rules, there is only one way to adjust fleet locations: the fleets of two planets swap their stations. Now, there are MM bidirectional interstellar routes between planets. Only two planets connected by an interstellar route can swap fleets.

To save expenses, the council wants the total number of swaps to be as small as possible. Early this year, the council commissioned Xiao Cheng to make a swapping plan, but he still has not finished it. Can you help him so that he can avoid being dismissed?

Input Format

The first line contains two integers N,MN, M separated by spaces.

The second line contains NN integers R1,R2,,RNR_1, R_2, \ldots, R_N separated by spaces. RiR_i denotes the index of the direct superior planet of planet ii. For the capital of the Federation, RiR_i is 00.

The third line contains NN pairwise distinct integers F1,F2,,FNF_1, F_2, \ldots, F_N separated by spaces. Here FiF_i denotes the combat power of the fleet currently stationed on planet ii.

The next MM lines each contain two integers Xi,YiX_i, Y_i, indicating that there is an interstellar route between planets XiX_i and YiY_i.

The input guarantees that the administrative relationships between planets form a binary tree, that there is at most one interstellar route between any two planets, and that there are no self-loops.

The testdata guarantees that a solution exists.

Output Format

Output one line with one integer, the minimum total number of swaps.

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

Hint

Constraints

For 100%100\% of the testdata, 1N121 \le N \le 12, 1M201 \le M \le 20, 1Fi1001 \le F_i \le 100.

Translated by ChatGPT 5