結果

問題 No.1390 Get together
ユーザー se1ka2se1ka2
提出日時 2021-02-12 21:32:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 1,163 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 72,248 KB
実行使用メモリ 13,400 KB
最終ジャッジ日時 2023-09-27 03:02:54
合計ジャッジ時間 4,841 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,076 KB
testcase_01 AC 4 ms
8,028 KB
testcase_02 AC 3 ms
8,068 KB
testcase_03 AC 6 ms
8,156 KB
testcase_04 AC 7 ms
8,132 KB
testcase_05 AC 6 ms
8,448 KB
testcase_06 AC 6 ms
8,148 KB
testcase_07 AC 5 ms
8,188 KB
testcase_08 AC 6 ms
8,300 KB
testcase_09 AC 7 ms
8,228 KB
testcase_10 AC 4 ms
8,040 KB
testcase_11 AC 4 ms
8,316 KB
testcase_12 AC 4 ms
8,048 KB
testcase_13 AC 3 ms
8,128 KB
testcase_14 AC 3 ms
8,044 KB
testcase_15 AC 3 ms
8,156 KB
testcase_16 AC 110 ms
10,044 KB
testcase_17 AC 151 ms
13,188 KB
testcase_18 AC 120 ms
10,160 KB
testcase_19 AC 154 ms
13,328 KB
testcase_20 AC 155 ms
13,272 KB
testcase_21 AC 157 ms
13,400 KB
testcase_22 AC 136 ms
12,488 KB
testcase_23 AC 143 ms
12,208 KB
testcase_24 AC 139 ms
12,168 KB
testcase_25 AC 154 ms
13,204 KB
testcase_26 AC 155 ms
13,204 KB
testcase_27 AC 158 ms
13,228 KB
testcase_28 AC 158 ms
13,292 KB
testcase_29 AC 157 ms
13,184 KB
testcase_30 AC 157 ms
13,216 KB
testcase_31 AC 157 ms
13,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

struct UnionFind
{
    std::vector<int> par, sz;
    
    UnionFind(int n){
        par.resize(n);
        sz.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;
            sz[i] = 1;
        }
    }
    
    int find(int i){
        if(i == par[i]) return i;
        return par[i] = find(par[i]);
    }
    
    bool same(int i, int j){
        return find(i) == find(j);
    }
    
    void unite(int i, int j){
        i = find(i), j = find(j);
        if(i == j) return;
        if(sz[i] < sz[j]) std::swap(i, j);
        par[j] = i;
        sz[i] += sz[j];
    }
    
    int size(int i){
        return sz[find(i)];
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> v[200005];
    for(int i = 0; i < n; i++){
        int b, c;
        cin >> b >> c;
        b--; c--;
        v[c].push_back(b);
    }
    UnionFind uf(m);
    for(int c = 0; c < n; c++){
        for(int j = 1; j < (int)v[c].size(); j++) uf.unite(v[c][0], v[c][j]);
    }
    int ans = 0;
    for(int i = 0; i < m; i++){
        if(uf.find(i) != i) ans++;
    }
    cout << ans << endl;
}
0