結果

問題 No.2563 色ごとのグループ
ユーザー shihakashihaka
提出日時 2023-12-02 15:38:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 1,966 bytes
コンパイル時間 2,560 ms
コンパイル使用メモリ 219,216 KB
実行使用メモリ 29,896 KB
最終ジャッジ日時 2023-12-02 15:38:39
合計ジャッジ時間 7,626 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 6 ms
6,676 KB
testcase_20 AC 16 ms
7,148 KB
testcase_21 AC 9 ms
6,676 KB
testcase_22 AC 8 ms
6,676 KB
testcase_23 AC 14 ms
6,676 KB
testcase_24 AC 133 ms
15,188 KB
testcase_25 AC 113 ms
15,568 KB
testcase_26 AC 162 ms
20,136 KB
testcase_27 AC 134 ms
22,708 KB
testcase_28 AC 176 ms
21,948 KB
testcase_29 AC 239 ms
27,312 KB
testcase_30 AC 238 ms
27,312 KB
testcase_31 AC 241 ms
27,316 KB
testcase_32 AC 256 ms
27,316 KB
testcase_33 AC 253 ms
29,872 KB
testcase_34 AC 243 ms
29,896 KB
testcase_35 AC 247 ms
29,888 KB
testcase_36 AC 250 ms
29,748 KB
testcase_37 AC 271 ms
29,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
template<typename T> inline bool chmax(T &a, T b) {return ((a < b) ? (a = b, true) : (false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a > b) ? (a = b, true) : (false));}
typedef long long ll;
typedef pair<ll,ll> P;

const int MAX_N = 500005;
int par[MAX_N];
int dep[MAX_N];

void init(int n) {
    for(int i = 0; i < n; i++) {
        par[i] = i;
        dep[i] = 0;
    }
}
int find(int x) {
    if(par[x] == x) {
        return x;
    } else {
        return par[x] = find(par[x]);
    }
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y)
        return;
    
    if(dep[x] < dep[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if(dep[x] == dep[y])
            dep[x]++;
    }
}
bool same(int x, int y) {
    return find(x) == find(y);
}


vector<int> c;
vector<vector<int>> to;
vector<bool> vis;
void dfs(int i) {
    vis[i] = true;
    rep(j,to[i].size()) {
        int v = to[i][j];
        if(vis[v]) {
            continue;
        }
        if(c[v] == c[i]) {
            unite(v, i);
            dfs(v);
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    c.resize(n);
    rep(i,n) cin >> c[i];
    vector<int> u(m), v(m);
    rep(i,m) cin >> u[i] >> v[i];

    rep(i,n) {
        c[i]--;
    }

    to.resize(n);
    vis.resize(n);
    rep(i,m) {
        u[i]--; v[i]--;
        to[u[i]].push_back(v[i]);
        to[v[i]].push_back(u[i]);
    }

    init(n);
    rep(i,n) {
        if(!vis[i]) {
            dfs(i);
        }
    }

    vector<vector<int>> kind(n);
    rep(i,n) {
        kind[c[i]].push_back(i);
    }

    int ans = 0;
    rep(i,n) {
        set<int> st;
        rep(j,kind[i].size()) {
            st.insert(find(kind[i][j]));
        }
        if(st.size() > 0) {
            ans += st.size() - 1;
        }
    }
    cout << ans << endl;


    return 0;
}
0