Problem:
Score : 400 points

Problem Statement

We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N.
We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M).
AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:

Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.

Find the maximum possible number of i such that p_i = i after operations.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

p is a permutation of integers from 1 through N.

1 ≤ x_j,y_j ≤ N

x_j ≠ y_j

If i ≠ j, \{x_i,y_i\} ≠ \{x_j,y_j\}.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the maximum possible number of i such that p_i = i after operations.

Sample Input 1

5 2
5 3 1 4 2
1 3
5 4

Sample Output 1

2

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

Sample Input 2

3 2
3 2 1
1 2
2 3

Sample Output 2

3

If we perform the operation by, for example, choosing j=1, j=2, j=1 in this order, p becomes 1 2 3, which is obviously optimal.
Note that we may choose the same j any number of times.

Sample Input 3

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

Sample Output 3

8

Sample Input 4

5 1
1 2 3 4 5
1 5

Sample Output 4

5

We do not have to perform the operation.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
//using System.Text;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
//using System.Numerics;
//using static System.Math;
//using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    void chmax<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? b : a; }
    void chmin<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? a : b; }

    void solve()
    {
        int N = cin.nextint;
        int M = cin.nextint;
        var P = cin.scanint.Select(i => i - 1).ToArray();
        var U = new DisjointSet(N);
        for (int i = 0; i < M; i++)
        {
            U.Unite(cin.nextint - 1, cin.nextint - 1);
        }
        int sum = 0;
        for (int i = 0; i < N; i++)
        {
            if (U.Same(i, P[i])) sum++;
        }
        WriteLine(sum);
    }

}

class DisjointSet
{
    int[] par;
    byte[] rank;
    /// <summary>
    /// 0 から <paramref name="N"/>-1 までの番号がついた <paramref name="N"/> 個の要素からなる集合を作成します．
    /// </summary>
    /// <param name="N">要素数</param>
    /// <remarks>このコンストラクタは O(N) で実行されます．</remarks>
    public DisjointSet(int N)
    {
        par = new int[N];
        rank = new byte[N];
        for (int i = 0; i < N; i++)
            par[i] = -1;
    }
    /// <summary>
    /// 指定した要素が属する集合の代表値を取得します．
    /// </summary>
    /// <param name="id">調べたい要素の 0-indexed での番号</param>
    /// <returns>指定した要素が属する集合の代表値</returns>
    /// <remarks>最悪計算量 O(α(N)) で実行されます．</remarks>
    public int this[int id]
    {
        get
        {
            if ((par[id] < 0)) return id;
            return par[id] = this[par[id]];
        }
    }
    /// <summary>
    /// 指定した 2 つの要素が属する集合同士を 1 つに統合することを試みます．
    /// </summary>
    /// <param name="x">最初の要素の 0-indexed での番号</param>
    /// <param name="y">2 つ目の要素の 0-indexed での番号</param>
    /// <returns>統合に成功したならば true，そうでなければ false．</returns>
    /// <remarks>最悪計算量 O(α(N)) で実行されます．</remarks>
    public bool Unite(int x, int y)
    {
        x = this[x]; y = this[y];
        if (x == y) return false;
        if (rank[x] < rank[y]) { var tmp = x; x = y; y = tmp; }
        par[x] += par[y];
        par[y] = x;
        if (rank[x] == rank[y])
            rank[x]++;
        return true;
    }
    /// <summary>
    /// 指定した要素が属する集合のサイズを取得します．
    /// </summary>
    /// <param name="x">指定する要素の 0-indexed での番号</param>
    /// <returns>集合のサイズ</returns>
    /// <remarks>最悪計算量 O(α(N)) で実行されます．</remarks>
    public int Size(int x) { return -par[this[x]]; }

    /// <summary>
    /// 指定した 2 つの要素が属する集合が同じかどうかを判定する．計算量 O(α(N))．
    /// </summary>
    /// <param name="x">最初の要素の 0-indexed での番号</param>
    /// <param name="y">2 つ目の要素の 0-indexed での番号</param>
    /// <returns>同じならば true，そうでなければ false．</returns>
    /// <remarks>最悪計算量 O(α(N)) で実行されます．</remarks>
    public bool Same(int x, int y) { return this[x] == this[y]; }

}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan { get { return ReadLine().Split(); } }
    public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
    public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
    public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint { get { return int.Parse(next); } }
    public long nextlong { get { return long.Parse(next); } }
    public double nextdouble { get { return double.Parse(next); } }
}