結果

問題 No.2563 色ごとのグループ
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-12-07 14:16:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,571 bytes
コンパイル時間 2,226 ms
コンパイル使用メモリ 206,640 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2023-12-07 14:17:01
合計ジャッジ時間 6,421 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 2 ms
6,548 KB
testcase_16 AC 2 ms
6,548 KB
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 3 ms
6,548 KB
testcase_20 AC 5 ms
6,548 KB
testcase_21 AC 4 ms
6,548 KB
testcase_22 AC 4 ms
6,548 KB
testcase_23 AC 5 ms
6,548 KB
testcase_24 AC 33 ms
6,548 KB
testcase_25 AC 28 ms
6,548 KB
testcase_26 AC 39 ms
6,548 KB
testcase_27 AC 34 ms
6,548 KB
testcase_28 AC 45 ms
6,548 KB
testcase_29 AC 61 ms
6,548 KB
testcase_30 AC 61 ms
6,548 KB
testcase_31 AC 60 ms
6,548 KB
testcase_32 AC 60 ms
6,548 KB
testcase_33 AC 70 ms
6,548 KB
testcase_34 AC 69 ms
6,548 KB
testcase_35 AC 69 ms
6,548 KB
testcase_36 AC 70 ms
6,548 KB
testcase_37 AC 72 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct UnionFind {
    int ngroup, N;
    vector<int> par, siz;

    UnionFind(int _N) : N(_N), par(_N), siz(_N){
        ngroup = _N;
        for(int i = 0; i < N; i++) par[i] = i, siz[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    int unite(int x, int y) {
        int rx = root(x), ry = root(y);
        if (rx == ry) return rx;
        ngroup--;
        if (siz[rx] > siz[ry]) swap(rx, ry);
        par[rx] = ry; siz[ry] += siz[rx];
        return ry;
    }

    bool same(int x, int y) {
        int rx = root(x), ry = root(y);
        return rx == ry;
    }

    int size(int x) {return siz[root(x)];}
    int group_count() {return ngroup;}

    vector<vector<int>> groups(){
        vector<int> rev(N); int nrt=0;
        for (int i=0; i<N; i++) if (root(i) == i) rev[i] = nrt, nrt++;
        vector<vector<int>> res(nrt);
        for (int i=0; i<N; i++) res[rev[root(i)]].push_back(i);

        return res;
    }
};

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    ll N, M, ans=0, a, b;
    cin >> N >> M;
    vector<int> C(N), cnt(N+1);
    for (int i=0; i<N; i++) cin >> C[i];
    UnionFind tree(N);
    for (int i=0; i<M; i++){
        cin >> a >> b; a--; b--;
        if (C[a] == C[b]) tree.unite(a, b);
    }

    for (int i=0; i<N; i++){
        if (tree.root(i) == i) cnt[C[i]]++;
    }
    for (int i=1; i<=N; i++) ans += max(0, cnt[i]-1);
    cout << ans << endl;

    return 0;
}
0