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 Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_imports)]

use text_io::*;
use proconio::*;
use std::collections::*;
use itertools::Itertools;
use std::process::exit;
use std::cmp::*;
use num::*;
use num::integer::Roots;
use std::str::FromStr;
use std::io::stdin;

fn main(){
    input! {
    n:usize,
    m:usize,
    p:[usize;n],
    c:[(usize,usize);m]
    }
    let mut uf=UnionFindTree::new(n);
    for i in 0..m{
        uf.unite(c[i].0-1,c[i].1-1);
    }
    let mut z=0;
    for i in 0..n{
        if uf.same(p[i]-1,i){
            z+=1;
        }
    }
    println!("{}",z)
}

/// UnionFind構造体
pub struct UnionFindTree {
    /// 頂点`i`の親を格納する配列
    parents: Vec<usize>,
    /// 頂点`i`が親であるときのその木の頂点数
    sizes: Vec<usize>,
    /// 重み付きUnionFindを使う際の重みの格納配列
    weights: Vec<isize>,
    /// 頂点`i`が属する木がループを持っているかどうか
    has_loops: Vec<bool>,
}

impl UnionFindTree {
    /// UnionFind初期化
    /// 計算量はO(n)
    pub fn new(n: usize) -> Self {
        let parents = (0..n).collect();
        let sizes = vec![1; n];
        let weights = vec![0; n];
        let has_loops = vec![false; n];
        UnionFindTree {
            parents,
            sizes,
            weights,
            has_loops,
        }
    }

    /// 親を再帰的に求め、途中の計算結果をもとに親の書き換えを行う関数
    /// 計算量はO(a(n)))
    pub fn root(&mut self, x: usize) -> usize {
        if self.parents[x] == x {
            x
        } else {
            let tmp = self.root(self.parents[x]);
            self.weights[x] += self.weights[self.parents[x]];
            self.parents[x] = tmp;
            tmp
        }
    }

    /// xを含むグラフの頂点数を返す
    /// 計算量はO(1)
    pub fn size(&self, x: usize) -> usize {
        self.sizes[x]
    }

    ///頂点iが属するグラフがループを持っているかどうか
    /// 計算量はO(1)
    pub fn has_loop(&self, x: usize) -> bool {
        self.has_loops[x]
    }

    /// 2つの頂点が同じ木に属しているかの判定
    /// `self.root()`を呼び出すため、`&mut self`を引数に取る。そのため、命名に`is_`を使っていない
    /// 計算量はO(a(n))
    pub fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }

    /// 重み付きUnionFindを考える際のUnite関数
    /// 計算量はO(a(n))
    pub fn unite_with_weight(&mut self, x: usize, y: usize, w: isize) {
        let root_x = self.root(x);
        let root_y = self.root(y);

        if self.same(x, y) {
            self.has_loops[root_x] = true;
            self.has_loops[root_y] = true;
        } else if self.sizes[root_x] >= self.sizes[root_y] {
            self.parents[root_y] = root_x;
            self.sizes[root_x] += self.sizes[root_y];
            self.weights[root_y] = -w - self.weights[y] + self.weights[x];
        } else {
            self.parents[root_x] = root_y;
            self.sizes[root_y] += self.sizes[root_x];
            self.weights[root_x] = w + self.weights[y] - self.weights[x];
        }
    }

    /// 重みを考慮しない際のUnite関数
    /// 重みとして0を与えているだけであり、計算量は同じくO(a(n))
    pub fn unite(&mut self, x: usize, y: usize) {
        self.unite_with_weight(x, y, 0);
    }

    /// 重み付きUnionFindにおいて、2つの頂点の距離を返す関数
    /// 2つの頂点が同じ木に属していない場合は`None`を返す
    /// 計算量はO(1)
    pub fn diff(&mut self, x: usize, y: usize) -> Option<isize> {
        if self.same(x, y) {
            Some(self.weights[x] - self.weights[y])
        } else {
            None
        }
    }

    /// xがxを含むグラフの親かどうか
    /// 計算量はO(n)
    pub fn is_parent(&self, x: usize) -> bool {
        self.parents[x] == x
    }

    /// 最大の頂点数のグラフの頂点数を返す
    /// 計算量はO(n)
    pub fn union_max(&self) -> usize{
        let max: &usize =self.sizes.iter().max().unwrap();
        *max
    }

    /// 最小の頂点数のグラフの頂点数を返す
    /// 計算量はO(n)
    pub fn union_min(&self) -> usize{
        let min: &usize =self.sizes.iter().min().unwrap();
        *min
    }

    /// 連結成分の数を返す
    /// 計算量はO(n)
    pub fn union_number(&self) -> usize {
        let mut gg=HashSet::new();
        for i in 0..self.parents.len() {
            if self.is_parent(i){
                gg.insert(self.parents[i]);
            }
        }
        gg.len()
    }
}