結果

問題 No.2563 色ごとのグループ
ユーザー through
提出日時 2023-12-07 19:08:44
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,144 bytes
コンパイル時間 2,086 ms
コンパイル使用メモリ 177,556 KB
実行使用メモリ 18,432 KB
最終ジャッジ日時 2024-09-27 02:17:48
合計ジャッジ時間 4,617 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
#define rep(i, n) for(ll i = 0; i < n; i++)

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    
    ll n, m; cin >> n >> m;
    vector<ll> c(n);
    rep(i,n) cin >> c[i];
    vector<vector<ll>> Graph(n);
    rep(i,m) {
        ll u, v; cin >> u >> v; u--; v--;
        Graph[u].emplace_back(v);
        Graph[v].emplace_back(u);
    }
    vector<ll> cnt(n+1,0);
    vector<bool> vis(n,false);
    queue<ll> todo;
    rep(i,n) {
        if( vis[i] ) continue;
        todo.push(i);
        while( !todo.empty() ) {
            ll now = todo.front(); todo.pop();
            if( vis[now] ) continue;
            vis[now] = true;
            for(auto to:Graph[now]) {
                if( vis[to] || c[now] != c[to] ) continue;
                todo.push(to);
            }
        }
        cnt[c[i]]++;
    }
    ll ans = 0;
    rep(i,n+1) ans += max(cnt[i]-1,0ll);
    cout << ans << endl;
    
    return 0;
}
0