atcoder#ARC097B. [ABC097D] Equals
[ABC097D] Equals
Score : points
Problem Statement
We have a permutation of the integers from through , , , .., . We also have pairs of two integers between and (inclusive), represented as , , .., . AtCoDeer the deer is going to perform the following operation on as many times as desired so that the number of ( ) such that is maximized:
- Choose such that , and swap and .
Find the maximum possible number of such that after operations.
Constraints
- is a permutation of integers from through .
- If , .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the maximum possible number of such that after operations.
5 2
5 3 1 4 2
1 3
5 4
2
If we perform the operation by choosing , becomes 1 3 5 4 2
, which is optimal, so the answer is .
3 2
3 2 1
1 2
2 3
3
If we perform the operation by, for example, choosing , , in this order, becomes 1 2 3
, which is obviously optimal.
Note that we may choose the same any number of times.
10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
8
5 1
1 2 3 4 5
1 5
5
We do not have to perform the operation.