#include #ifdef LOCAL #include "./debug.cpp" #else #define debug(...) #define print_line #endif using namespace std; using ll = long long; /** * @brief Disjoint Set Union * @docs docs/graph/dsu.md */ struct dsu { vector par, sz; dsu(int n) { par.resize(n); sz.resize(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } par[x] = find(par[x]); return par[x]; } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (sz[x] < sz[y]) { swap(x, y); } par[y] = x; sz[x] += sz[y]; } int size(int x) { return sz[find(x)]; } bool is_united(int x, int y) { return find(x) == find(y); } }; int main() { int N, M; cin >> N >> M; vector C(N); for (int i = 0; i < N; i++) { cin >> C[i]; C[i]--; } dsu ds(N); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; u--; v--; if (C[u] == C[v]) ds.unite(u, v); } vector> D(N); for (int i = 0; i < N; i++) { D[C[i]].insert(ds.find(i)); } int ans = 0; for (int i = 0; i < N; i++) { if (D[i].size() > 0) ans += D[i].size() - 1; } cout << ans << endl; }