atcoder#ARC097B. [ABC097D] Equals

[ABC097D] Equals

Score : 400400 points

Problem Statement

We have a permutation of the integers from 11 through NN, p1p_1, p2p_2, .., pNp_N. We also have MM pairs of two integers between 11 and NN (inclusive), represented as (x1,y1)(x_1,y_1), (x2,y2)(x_2,y_2), .., (xM,yM)(x_M,y_M). AtCoDeer the deer is going to perform the following operation on pp as many times as desired so that the number of ii (11 \leq ii \leq NN) such that pi=ip_i = i is maximized:

  • Choose jj such that 11 \leq jj \leq MM, and swap pxjp_{x_j} and pyjp_{y_j}.

Find the maximum possible number of ii such that pi=ip_i = i after operations.

Constraints

  • 22 \leq NN \leq 10510^5
  • 11 \leq MM \leq 10510^5
  • pp is a permutation of integers from 11 through NN.
  • 11 \leq xj,yjx_j,y_j \leq NN
  • xjx_j \neq yjy_j
  • If ii \neq jj, {xi,yi}\{x_i,y_i\} \neq {xj,yj}\{x_j,y_j\}.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

p1p_1 p2p_2 .... pNp_N

x1x_1 y1y_1

x2x_2 y2y_2

::

xMx_M yMy_M

Output

Print the maximum possible number of ii such that pi=ip_i = i after operations.

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

If we perform the operation by choosing j=1j=1, pp becomes 1 3 5 4 2, which is optimal, so the answer is 22.

3 2
3 2 1
1 2
2 3
3

If we perform the operation by, for example, choosing j=1j=1, j=2j=2, j=1j=1 in this order, pp becomes 1 2 3, which is obviously optimal. Note that we may choose the same jj 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.