use std::collections::HashSet; fn solve() -> u64 { let nm = input(); let mut nm = nm.split(' '); let n: usize = nm.next().unwrap().parse().unwrap(); let m: usize = nm.next().unwrap().parse().unwrap(); let c: Vec = input().split(' ').map(|di| di.parse().unwrap()).collect(); let mut graph = vec![vec![]; n]; for _ in 0..m { let uv = input(); let mut uv = uv.split(' '); let u: usize = uv.next().unwrap().parse().unwrap_or(1) - 1; let v: usize = uv.next().unwrap().parse().unwrap_or(1) - 1; graph[u].push(v); graph[v].push(u); } let mut colors = vec![0; n]; let mut not_visited: HashSet<_> = (0..n).collect(); while !not_visited.is_empty() { let start = *not_visited.iter().next().unwrap(); let col = c[start]; colors[col-1] += 1; not_visited.remove(&start); let mut stack = vec![start]; while let Some(cur) = stack.pop() { for &nxt in graph[cur].iter() { if c[nxt] == col && not_visited.contains(&nxt) { stack.push(nxt); not_visited.remove(&nxt); } } } } return colors.iter().map(|&ci: &u64| ci.saturating_sub(1)).sum(); } fn main() { println!("{}", solve()); } fn input() -> String { let mut buffer = String::new(); std::io::stdin().read_line(&mut buffer).unwrap(); return buffer.trim().to_string(); }