結果

問題 No.2563 色ごとのグループ
ユーザー shihakashihaka
提出日時 2023-12-02 15:36:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,980 bytes
コンパイル時間 2,597 ms
コンパイル使用メモリ 219,084 KB
実行使用メモリ 814,236 KB
最終ジャッジ日時 2023-12-02 15:37:02
合計ジャッジ時間 8,127 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 3 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 3 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 4 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 17 ms
7,148 KB
testcase_21 AC 11 ms
6,676 KB
testcase_22 AC 10 ms
6,676 KB
testcase_23 AC 15 ms
6,676 KB
testcase_24 AC 139 ms
15,188 KB
testcase_25 AC 116 ms
15,568 KB
testcase_26 AC 164 ms
20,136 KB
testcase_27 AC 140 ms
22,708 KB
testcase_28 AC 192 ms
21,948 KB
testcase_29 AC 280 ms
27,312 KB
testcase_30 AC 254 ms
27,312 KB
testcase_31 AC 254 ms
27,316 KB
testcase_32 AC 251 ms
27,316 KB
testcase_33 MLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

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, int p) {
    vis[i] = true;
    rep(j,to[i].size()) {
        int v = to[i][j];
        if(v == p) {
            continue;
        }
        if(c[v] == c[i]) {
            unite(v, i);
            dfs(v, i);
        }
    }
}

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, -1);
        }
    }

    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