QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 256 MB Puntuación total: 100

#8205. Interstellar Travel

Estadísticas

It is the summer of 21YY.

Time has passed, and Xiao Cheng is now a technical consultant for the Space Defense Council of the Federation of Planets.

Following the federal election last year, the new president has just been sworn in. One of the new government's primary policies is to reorganize administrative regions and streamline the existing administrative relationships between planets. The Federation consists of $N$ planets, and the new administrative structure forms a binary tree. The administrative center of the Federation is the capital, and all other planets are divided into at most two regions, with each region internally following the same structure.

Before this administrative reform, each planet was stationed with a space fleet. The combat power of the fleet stationed at planet $i$ is $F_i$. During the administrative reform, the Space Defense Council is responsible for adjusting the locations of each fleet to comply with military regulations, which require that the fleet stationed at the center of each administrative region must be the one with the highest combat power within that region.

Furthermore, according to military regulations, there is only one way to adjust fleet locations: swapping the fleets of two planets.

Currently, there are $M$ bidirectional interstellar routes between all planets. Fleets can only be swapped between two planets if there is an interstellar route connecting them. To save costs, the Council hopes to minimize the total number of swaps.

Earlier this year, the Council commissioned Xiao Cheng to develop a swap plan, but he has not yet completed the task. Can you help Xiao Cheng so he can avoid being dismissed?

Input

The first line contains two integers $N$ and $M$.

The second line contains $N$ integers $R_1, R_2, \dots, R_N$, separated by spaces. $R_i$ represents the ID of the direct superior planet of planet $i$. For the federal capital, $R_i$ is represented by $0$.

The third line contains $N$ distinct integers $F_1, F_2, \dots, F_N$, separated by spaces, where $F_i$ represents the combat power of the fleet currently stationed at planet $i$.

The next $M$ lines each contain two integers $X_i$ and $Y_i$, indicating that there is an interstellar route between planet $X_i$ and planet $Y_i$.

Output

A single integer representing the minimum total number of swaps required.

Examples

Input 1

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

Output 1

2

Input 2

5 4
3 3 5 5 0
4 8 10 9 3
1 2
2 4
1 3
4 5

Output 2

6

Note

For the first example, the administrative hierarchy is as follows:

The optimal strategy is to perform two swaps: $(2, 3)$ and $(1, 4)$.

For the second example, the administrative hierarchy is as follows:

The optimal strategy is to perform a total of six swaps in the order: $(1, 2), (1, 3), (1, 2), (2, 4), (4, 5), (2, 4)$.

Constraints

There are 10 test cases in total. The data scale and constraints for each test case are as follows:

# $N$ $M$ Other Constraints
#1 $N = 5$ $M = 5$ None
#2 $N = 7$ $M = 6$
#3 $N = 8$ $M = 10$ Administrative relationship is a chain
#4 $N = 9$ $M = 30$
#5 $N = 10$ $M = 19$
#6 $N = 13$ $M = 13$
#7 $N = 16$ $M = 120$
#8 $N = 10$ $M = 12$ Answer does not exceed 10
#9 $N = 11$ $M = 17$
#10 $N = 12$ $M = 20$

For 100% of the data, $1 \le F_i \le 100$. The input data guarantees that the administrative relationships between planets form a binary tree, there is at most one interstellar route between any two planets, and there are no self-loops in the interstellar routes. The data is guaranteed to have a solution.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.