結果

問題 No.2563 色ごとのグループ
ユーザー Mikan04yMikan04y
提出日時 2023-12-02 15:47:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 2,031 bytes
コンパイル時間 2,277 ms
コンパイル使用メモリ 212,652 KB
実行使用メモリ 24,452 KB
最終ジャッジ日時 2023-12-02 15:47:14
合計ジャッジ時間 7,481 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 ms
6,676 KB
testcase_06 AC 1 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 2 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 8 ms
6,676 KB
testcase_23 AC 13 ms
6,676 KB
testcase_24 AC 121 ms
11,532 KB
testcase_25 AC 103 ms
12,748 KB
testcase_26 AC 151 ms
17,544 KB
testcase_27 AC 144 ms
23,336 KB
testcase_28 AC 168 ms
19,160 KB
testcase_29 AC 236 ms
24,452 KB
testcase_30 AC 267 ms
24,452 KB
testcase_31 AC 234 ms
24,452 KB
testcase_32 AC 245 ms
24,452 KB
testcase_33 AC 268 ms
24,452 KB
testcase_34 AC 292 ms
24,452 KB
testcase_35 AC 263 ms
24,452 KB
testcase_36 AC 268 ms
24,452 KB
testcase_37 AC 270 ms
24,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int MAX_N = 200009;

// 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