結果
| 問題 | No.2563 色ごとのグループ | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-11-23 14:29:20 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 164 ms / 2,000 ms | 
| コード長 | 976 bytes | 
| コンパイル時間 | 2,191 ms | 
| コンパイル使用メモリ | 204,320 KB | 
| 最終ジャッジ日時 | 2025-02-17 23:18:59 | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 35 | 
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n,m;
    cin >> n >> m;
    vector<int> c(n);
    rep(i,n) cin >> c[i],c[i]--;
    vector<vector<int>> g(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        a--;b--;
        if(c[a]!=c[b]) continue;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> cnt(n,0);
    vector<bool> dist(n,false);
    rep(i,n){
        if(dist[i]) continue;
        
        queue<int> q;
        q.push(i);
        dist[i]=true;
        while(!q.empty()){
            int t = q.front();
            q.pop();
            for(int x:g[t]){
                if(dist[x]) continue;
                dist[x]=true;
                q.push(x);
            }
        }
        cnt[c[i]]++;
    }
    int ans = 0;
    rep(i,n) if(cnt[i]>0) ans += cnt[i]-1;
    cout << ans << endl;
}
            
            
            
        