//(・ω・) #include #include // cout, endl, cin #include // string, to_string, atoi #include // vector #include // min, max, swap, sort, reverse, lower_bound, upper_bound #include // pair, make_pair #include // tuple, make_tuple #include // int64_t, int*_t #include // printf #include // map #include // queue, priority_queue #include // set #include // stack #include // deque #include // unordered_map #include // unordered_set #include // bitset #include // isupper, islower, isdigit, toupper, tolower #include #include //setprecision #include #include //istringstream #include #include #include //std::advance() using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define SZ(x) ((int)(x).size()); #define vec vector template using v = vector; template using vv = v< v >; template using vvv = v< vv >; #define el '\n' #define vl v #define vvl vv #define pl pair #define repab(i, a, b) for(ll i = a; a < (ll)(b); ++i) #define rep(i, n) for(ll i = 0; i < (ll)(n); ++i) #define rep1(i, n) for(ll i = 1; i <= (ll)(n); ++i) #define rrep(i, n) for(ll i = ((ll)(n) - 1); i >= 0; --i) #define rrep1(i, n) for(ll i = ((ll)(n)); i > 0; --i) #define YN(x) cout << ((x) ? "YES\n" : "NO\n"); #define Yn(x) cout << ((x) ? "Yes\n" : "No\n"); #define yn(x) cout << ((x) ? "yes\n" : "no\n"); #define COUT(x) cout << (x) << el; const double pi = 3.141592653589793238; const int inf = 1073741823; const ll infl = 1LL << 60; ll gcd(ll a, ll b){return b ? gcd(b, a % b) : a;} int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; struct UnionFind { vector par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化 for(int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); //xの根をrx int ry = root(y); //yの根をry if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; int main(){ int n,m; cin >> n >> m; vector c(n); map< long long, vector > ma; UnionFind uf(n); rep(i, n){ cin >> c[i]; ma[c[i]].push_back(i); } rep(i, m){ int u,v; cin >> u >> v; --u; --v; if(c[u] == c[v]) uf.unite(u, v); } long long count = 0; rep(i, n){ for(auto j : ma[c[i]]){ if(!uf.same(i, j)){ uf.unite(i, j); count ++; } } } cout << count<< endl; return 0; }