#include using namespace std; const int MAX_N = 200009; // Union-Find // 計算量O(α(n)) struct UnionFind { int par[MAX_N]; // 親の番号 int rank[MAX_N]; // n要素で初期化 void init(int N) { for (int i = 0; i < N; i++) { par[i] = i; // 初めは全ての頂点が根 rank[i] = 0; // 初めは全ての頂点のランクが0 } } // 木の根を求める int root(int x) { if (par[x] == x) // 根 return x; else return par[x] = root(par[x]); // 経路圧縮 } // xとyが同じ集合に属するか否か bool same(int x, int y) { return root(x) == root(y); } // xとyの属する集合を併合 void unite(int x, int y) { x = root(x); y = root(y); if (x == y) // 既に同じ集合に属するなら何もしない return; if (rank[x] < rank[y]) // xのrankの方が小さいとき par[x] = y; // xをyを根としてつなぎ直す else { par[y] = x; // yをxを根としてつなぎ直す if (rank[x] == rank[y]) rank[x]++; } } }; int main() { int N, M; cin >> N >> M; vector> color(N, set({})); int C[N]; for (int i = 0; i < N; i++) { int c; cin >> c; c--; C[i] = c; color[c].insert(i); } UnionFind uf; uf.init(N); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; u--, v--; if (C[u] == C[v])uf.unite(u, v); } int ans = 0; for (int i = 0; i < N; i++) { auto itr1 = color[i].begin(), itr2 = itr1; while (itr2 != color[i].end()) { if (!uf.same(*itr1, *itr2)) { uf.unite(*itr1, *itr2); ans++; } itr2++; } } cout << ans << endl; }