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.15.1) (judge verdict: Accepted)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
 
use std::cmp::Ordering;
 
/// Equivalent to std::lowerbound and std::upperbound in c++
pub trait BinarySearch<T> {
    fn lower_bound(&self, &T) -> usize;
    fn upper_bound(&self, &T) -> usize;
}
 
impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
 
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
 
    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
 
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}
 
 
 
 
 
 
use std::cmp;
use std::cmp::min;
use std::collections::BTreeMap;
use std::process;
use std::cmp::Ord;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::collections::BTreeSet;
use std::mem;
 
#[derive(PartialEq, Eq)]
pub struct point(i64, i64, i64);
impl Ord for point {
    fn cmp(&self, other: &point) -> Ordering {
        self.0.cmp(&other.0)
    }
}
 
impl PartialOrd for point {
    fn partial_cmp(&self, other: &point) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
/*
impl PartialEq for point {
    fn eq(&self, other: &point) -> bool {
        self.0 == other.0
    }
}
impl Eq for point {
    fn eq(&self, other: &point) -> bool {
        self.0 == other.0
    }s
}*/
type P = (i64 , i64);
fn bfs(start:&P, goal:&P, map:&Vec<Vec<char>>) -> i64 {
        let dx = vec![1, 0, -1, 0];
        let dy = vec![0, 1, 0, -1];
        let s = (start.0, start.1);
        let g = (goal.0 , goal.1);
        let mut queue:VecDeque<P> = VecDeque::new();
        let mut d:Vec<Vec<i64>> = Vec::new();
        for i in 0..map.len(){
            d.push(Vec::new());
            for j in 0..map[i].len(){
                d[i].push(30000000);
            }
        }
        queue.push_back((s.0, s.1));
        d[s.0 as usize][s.1 as usize] = 0;
        while(queue.len()!=0){
            let mut p:P = queue[0];
            queue.pop_front();
            if p.0 == g.0 && p.1 == g.1{break;}
            for i in 0..4{
                let nx = p.0 + dx[i];
                let ny = p.1 + dy[i];
                if (0<=nx && nx <map.len() as i64 && 0<=ny &&ny<map[0].len() as i64 && map[nx as usize][ny as usize]!='#' && d[nx as usize][ny as usize] == 30000000 ){
                    queue.push_back((nx, ny));
                    d[nx as usize][ny as usize] = d[p.0 as usize][p.1 as usize] + 1;
                }
            }
 
        }
        d[goal.0 as usize][goal.1 as usize]
}
 
fn bfs01(start:&P, goal:&P, map:&Vec<Vec<char>>) -> i64 {
        let dx = vec![1, 0, -1, 0];
        let dy = vec![0, 1, 0, -1];
        let s = (start.0, start.1);
        let g = (goal.0 , goal.1);
        let mut queue:VecDeque<P> = VecDeque::new();
        let mut d:Vec<Vec<i64>> = Vec::new();
        for i in 0..map.len(){
            d.push(Vec::new());
            for j in 0..map[i].len(){
                d[i].push(30000000);
            }
        }
        queue.push_back((s.0, s.1));
        d[s.0 as usize][s.1 as usize] = 0;
        while(queue.len()!=0){
            let mut p:P = queue[0];
            queue.pop_front();
            if p.0 == g.0 && p.1 == g.1{break;}
            for i in 0..4{
                let nx = p.0 + dx[i];
                let ny = p.1 + dy[i];
                if (0<=nx && nx <map.len() as i64 && 0<=ny &&ny<map[0].len() as i64 && d[nx as usize][ny as usize] == 30000000 ){
                    if map[nx as usize][ny as usize] == '#'{
                        queue.push_back((nx, ny));
                        d[nx as usize][ny as usize] = d[p.0 as usize][p.1 as usize] + 1;
                    }
                    else{
                        queue.push_front((nx, ny));
                        d[nx as usize][ny as usize] = d[p.0 as usize][p.1 as usize];
                    }
                }
            }
 
        }
        d[goal.0 as usize][goal.1 as usize]
}
 
struct UnionFind{
    par:Vec<usize>,
    rank:Vec<usize>,
    size:Vec<usize>,    
}
impl UnionFind{
    fn init(n:usize) -> UnionFind{
        let mut par = vec![0;n];
        for i in 0..n{
            par[i] = i;    
        }
        UnionFind{
            par:par,
            rank:vec![0;n], 
            size:vec![1;n], 
        }
    }
    fn find(&mut self, x:usize) ->usize{
        if(self.par[x] == x){
            x    
        }
        else{
            let p = self.par[x];
            let res = self.find(p);
            self.par[x] = res;
            res
        }
    }
    fn same(&mut self, a:usize, b:usize)->bool{
        self.find(a) == self.find(b)    
    }
    fn unite(&mut self, a:usize, b:usize){
        let x = self.find(a);
        let y = self.find(b);
        if x != y{
            if (self.rank[x] < self.rank[y]){
                self.par[x] = y;
                self.size[y] += self.size[x];
            }
            else{
                self.par[y] = x;
                self.size[x] += self.size[y];
                if(self.rank[x] == self.rank[y]){ self.rank[x]+=1;}
            }
        }
    }
    fn check_size(&mut self, a:usize) -> usize{
        let x = self.find(a);
        let s = self.size[x];
        s    
    }
}
fn gcd(a:usize, b:usize)->usize{
    if b==0{return a;}
    gcd(b, a%b)
}
fn lcm(a: usize, b: usize) -> usize {
    a / gcd(a, b) * b
}
fn extgcd(a:usize, b:usize, x:&mut usize, y:&mut usize)->usize{
    let mut d = a;
    if b != 0{
        d = extgcd(b, a%b, y, x);
        *y -= (a/b) * (*x);    
    }    
    else{
        *x = 1;
        *y = 0;
    }
    d
}

use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::any::{Any, TypeId};

struct vec2<T>(Vec<T>);
 
impl<T> Deref for vec2<T> {
    type Target = Vec<T>;
    fn deref(&self) -> &Vec<T> {
        &self.0
    }
}
 
impl<T> DerefMut for vec2<T> {
    fn deref_mut(&mut self) -> &mut Vec<T> {
        &mut self.0
    }
}
impl<T> Index<i64> for vec2<T> {
    type Output = T;
    fn index(&self, index: i64) -> &T {
        &self.0[index as usize]
    }
}
 
impl<T> IndexMut<i64> for vec2<T> {
    fn index_mut(&mut self, index: i64) -> &mut T {
        &mut self.0[index as usize]
    }
}

fn segment_sieve(a:usize, b:usize) -> Vec<bool>{
    let mut is_prime= vec![false; b+1];
    let mut is_prime_small= vec![false; b+1];
    for i in 0..b{
        if i*i>=b{break;}    
        is_prime_small[i] = true;
    }
    for i in 0..b{
        if i>=b-a{break;}
        is_prime[i] = true;    
    }
    for i in 2..b{
        if i*i>=b{break;}
        if is_prime_small[i] == true{


            let mut j = 2*i;
            while(j*j<b){
                is_prime_small[j] = false;
                j+=i;
            }
            
            j = cmp::max(2, (a+i-1)/i)*i;
            while(j<b){
                is_prime[j-a] = false;
                j+=i;    
            }    
        }    
    }
    is_prime


}

fn divisor(n:usize) -> Vec<usize>{
    let mut res:Vec<usize> = Vec::new(); 
    for i in 1..n+1{
        if i*i>n{break;}
        if n%i == 0{
            res.push(i);
            if i != n/i{
                res.push(n/i);    
            }    
        }    
    }
    res
}

fn prime_factor(nn:usize) -> HashMap<usize, usize>{
    let mut res:HashMap<usize, usize> = HashMap::new();
    let mut n = nn.clone();
    for i in 2..n+1{
        if i*i>n{break;}
        while(n%i == 0){
            *res.entry(i).or_insert(0) += 1;
            n/=i;    
        }    
    }
    if (n != 1){res.insert(n,1);}
    res
}

use std::collections::BinaryHeap;

#[derive(PartialEq)]
struct mf(i64);

impl Eq for mf {}

impl PartialOrd for mf {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl Ord for mf {
    fn cmp(&self, other: &mf) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}
fn main() {
    input!{
        n:usize,
        m:usize,
        pq:[usize;n],
        rs:[(usize, usize) ;m],
    }
    let mut u1 = UnionFind::init(n+1);
    for i in 0..m{
        u1.unite(rs[i].0, rs[i].1);
    }
    let mut ans = 0;
    for i in 1..n+1{
        if u1.find(i) == u1.find(pq[i-1]){
            ans+=1;
        }
    }
    println!("{}",ans);

}