PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000051. Đưa các giá trị về vị trí đúng (Equals)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đưa các giá trị về vị trí đúng (Equals)

Equals

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation pp of 1,2,…,n1,2,\ldots,n and a list of mm allowed pairs of positions. In one operation, choose any listed pair (xj,yj)(x_j,y_j) and swap the values currently stored at those two positions.

Each pair may be used any number of times, and swaps may be performed in any order. Repeated swaps can indirectly move a value through several positions. A position ii is correct if its final value is ii.

Find the maximum number of positions that can be correct simultaneously after some sequence of allowed swaps. The entire permutation does not necessarily have to be sorted.

Input

The first line contains n,mn,m, the second the permutation pp, and each of the next mm lines contains xj,yjx_j,y_j.

Output

Print the maximum number of simultaneously correct positions.

Subtasks

  • Subtask 1 (20 points): 2≤n≤302\le n\le30, 1≤m≤301\le m\le30; m≤n(n−1)/2m\le n(n-1)/2; pp is a permutation of 1,…,n1,\ldots,n; 1≤xj,yj≤n1\le x_j,y_j\le n, xj≠yjx_j\ne y_j; allowed pairs are distinct.
  • Subtask 2 (30 points): 2≤n≤30002\le n\le3000, 1≤m≤30001\le m\le3000; m≤n(n−1)/2m\le n(n-1)/2; pp is a permutation of 1,…,n1,\ldots,n; 1≤xj,yj≤n1\le x_j,y_j\le n, xj≠yjx_j\ne y_j; allowed pairs are distinct.
  • Subtask 3 (50 points): 2≤n≤1052\le n\le10^5, 1≤m≤1051\le m\le10^5; m≤n(n−1)/2m\le n(n-1)/2; pp is a permutation of 1,…,n1,\ldots,n; 1≤xj,yj≤n1\le x_j,y_j\le n, xj≠yjx_j\ne y_j; allowed pairs are distinct.

Examples

Input

5 2
5 3 1 4 2
1 3
5 4

Output

2

Explanation

Position 4 is already correct, and swapping positions 1 and 3 fixes position 1, giving 2.