#include #include #include #include #include using namespace std; int main() { int n, m; cin >> n >> m; vector c(n); for (int i = 0; i < n; i++) cin >> c[i]; for (int i = 0; i < n; i++) c[i]--; vector> G(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; if (c[u] == c[v]) { G[u].push_back(v); G[v].push_back(u); } } vector> group(n); for (int i = 0; i < n; i++) { group[c[i]].insert(i); } vector seen(n, false); long long res = 0; queue test; for (int i = 0; i < n; i++) { if (group[i].size() == 0) continue; int x = 0; for (int w : group[i]) { if (seen[w]) continue; seen[w] = true; x++; test.push(w); while(!test.empty()) { int p = test.front(); test.pop(); for (int q : G[p]) { if (seen[q]) continue; seen[q] = true; test.push(q); } } } res += x - 1; } cout << res << endl; return 0; }