結果

問題 No.2563 色ごとのグループ
ユーザー Mikan04yMikan04y
提出日時 2023-12-02 15:45:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,031 bytes
コンパイル時間 2,300 ms
コンパイル使用メモリ 212,652 KB
実行使用メモリ 23,684 KB
最終ジャッジ日時 2023-12-02 15:45:28
合計ジャッジ時間 8,834 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 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 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 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 15 ms
6,676 KB
testcase_21 AC 10 ms
6,676 KB
testcase_22 AC 7 ms
6,676 KB
testcase_23 AC 13 ms
6,676 KB
testcase_24 AC 121 ms
11,532 KB
testcase_25 AC 119 ms
12,748 KB
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 100009;

// Union-Find
// 計算量O(α(n))
struct UnionFind
{
    int par[MAX_N]; // 親の番号
    int rank[MAX_N];

    // n要素で初期化
    void init(int N)
    {
        for (int i = 0; i < N; i++)
        {
            par[i] = i;  // 初めは全ての頂点が根
            rank[i] = 0; // 初めは全ての頂点のランクが0
        }
    }

    // 木の根を求める
    int root(int x)
    {
        if (par[x] == x) // 根
            return x;
        else
            return par[x] = root(par[x]); // 経路圧縮
    }

    // xとyが同じ集合に属するか否か
    bool same(int x, int y)
    {
        return root(x) == root(y);
    }

    // xとyの属する集合を併合
    void unite(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x == y) // 既に同じ集合に属するなら何もしない
            return;
        if (rank[x] < rank[y]) // xのrankの方が小さいとき
            par[x] = y;        // xをyを根としてつなぎ直す
        else
        {
            par[y] = x; // yをxを根としてつなぎ直す
            if (rank[x] == rank[y])
                rank[x]++;
        }
    }
};

int main()
{
    int N, M;
    cin >> N >> M;
    vector<set<int>> color(N, set<int>({}));
    int C[N];
    for (int i = 0; i < N; i++)
    {
        int c;
        cin >> c;
        c--;
        C[i] = c;
        color[c].insert(i);
    }

    UnionFind uf;
    uf.init(N);

    for (int i = 0; i < M; i++)
    {
        int u, v;
        cin >> u >> v;
        u--, v--;
        if (C[u] == C[v])uf.unite(u, v);
    }

    int ans = 0;
    for (int i = 0; i < N; i++)
    {
        auto itr1 = color[i].begin(), itr2 = itr1;
        while (itr2 != color[i].end())
        {
            if (!uf.same(*itr1, *itr2))
            {
                uf.unite(*itr1, *itr2);
                ans++;
            }
            itr2++;
        }
    }
    cout << ans << endl;
}
0