#include #include using namespace std; using ll = long long; using ld = long double; using st = string; using mint = atcoder::modint998244353; using Mint = atcoder::modint1000000007; #define vl vector #define vvl vector> #define vd vector #define vvd vector> #define vb vector #define vs vector #define vp vector> #define vm vector #define vvm vector> #define vM vector #define vvM vector> #define chmax(n,v) n=nv?v:n #define all(n) begin(n),end(n) #define rev(n) reverse(all(n)) #define sor(n) stable_sort(all(n)) #define rep(i,n) for(ll i=0;i<(n);++i) #define rrep(i,a,n) for(ll i=a;i<(n);++i) #define sz(n) n.size() #define bit(n,shift) ((n&(1< void input(T &a) { cin >> a; }; template void input(T1&a, T2 &...b) { cin >> a; input(b...); }; const ll inf = 1e18; //std::setprecision(13); 出力桁を増やす //2^29<10^9 ll di[8]={1,-1,0,0,1,1,-1,-1}; ll dj[8]={0,0,1,-1,1,-1,-1,1}; //ll da[4]={1,1,-1,-1}; //ll db[4]={1,-1,-1,1}; //ll na[4]={1,0,1,0}; //ll nb[4]={0,1,0,1}; template vector rd(size_t n) { vector ts(n); for (size_t i = 0; i < n; i++) cin >> ts[i]; return ts; } bool is_prime(ll N) { if (N == 1) return false; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) return false; } return true; } int calc_digit(ll N){ int res = 0; while(N>0){ res++; N/=10; } return res; } ll factorial(ll n, ll mod = 1e18) { ll ans = 1; for(ll i = n; i >= 2; i--) ans = (ans * i) % mod; return ans; } void printl(vl v){ ll vsz=v.size(); for(ll i=0;i par; vector size; UnionFind(ll n){ par.resize(2*(n+1),-1); size.resize(2*(n+1),1); } ll root(ll x){ if (par[x]==-1) return x; return par[x]=root(par[x]); } void unite(ll u,ll v){ ll uu=root(u),vv=root(v); if(uu==vv){ return; } if(size[uu]> prime_factorize(ll N){ vector > res; for (ll a = 2; a * a <= N; ++a) { if (N % a != 0) continue; ll ex = 0; while (N % a == 0) { ex++; N /= a; } res.push_back({a, ex}); } if (N != 1) res.push_back({N, 1}); return res; } vl p_list(ll n){ vl res; vb check(n+1); for(ll i=2;i<=n;i++){ if(check[i]) continue; res.push_back(i); ll ii=i; while(ii<=n){ check[ii]=true; ii+=i; } } return res; } vl cumulative_sum(vl a){ ll s=sz(a); vl b(s+1,0); for(ll i=0;i> n >> m; vl c=rd(n); UnionFind t(n); rep(i,m){ ll u,v; cin >> u >> v; u--,v--; if(c[u]==c[v]) t.unite(u,v); } vl cnt(n,0); rep(i,n){ if(t.root(i)==i) cnt[c[i]]++; } ll ans=0; rep(i,n){ if(cnt[i]>1) ans+=(cnt[i]-1); } cout << ans; }