結果

問題 No.1390 Get together
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-21 22:48:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 1,358 bytes
コンパイル時間 2,175 ms
コンパイル使用メモリ 203,640 KB
実行使用メモリ 13,488 KB
最終ジャッジ日時 2023-09-11 17:51:47
合計ジャッジ時間 7,428 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 111 ms
10,312 KB
testcase_17 AC 152 ms
13,216 KB
testcase_18 AC 120 ms
10,308 KB
testcase_19 AC 158 ms
13,116 KB
testcase_20 AC 160 ms
13,332 KB
testcase_21 AC 156 ms
13,248 KB
testcase_22 AC 138 ms
12,228 KB
testcase_23 AC 143 ms
12,160 KB
testcase_24 AC 142 ms
12,268 KB
testcase_25 AC 155 ms
13,184 KB
testcase_26 AC 156 ms
13,260 KB
testcase_27 AC 151 ms
13,404 KB
testcase_28 AC 155 ms
13,256 KB
testcase_29 AC 155 ms
13,188 KB
testcase_30 AC 158 ms
13,220 KB
testcase_31 AC 157 ms
13,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

    UnionFind(int 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);
        int 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);
        int ry = root(y);
        return rx == ry;
    }

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

    int group_count(){
        return ngroup;
    }
};

int main(){
 
    int N, M, c, b, ans=0;
    cin >> N >> M;
    UnionFind tree(M+1);
    vector<vector<int>> color(N+1);
    for (int i=0; i<N; i++){
        cin >> b >> c;
        color[c].push_back(b);
    }

    for (int i=1; i<=N; i++){
        for (int j=0; j<color[i].size(); j++) tree.unite(color[i][0], color[i][j]);
    }

    for (int i=1; i<=M; i++){
        if (tree.root(i) == i) ans += tree.size(i)-1;
    }

    cout << ans << endl;
 
    return 0;
}
0