結果

問題 No.1390 Get together
ユーザー trineutrontrineutron
提出日時 2021-02-12 21:41:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 1,025 bytes
コンパイル時間 2,554 ms
コンパイル使用メモリ 210,328 KB
実行使用メモリ 23,612 KB
最終ジャッジ日時 2023-09-27 03:24:05
合計ジャッジ時間 7,154 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 137 ms
14,368 KB
testcase_17 AC 203 ms
23,612 KB
testcase_18 AC 121 ms
11,564 KB
testcase_19 AC 193 ms
19,908 KB
testcase_20 AC 202 ms
22,232 KB
testcase_21 AC 203 ms
21,916 KB
testcase_22 AC 161 ms
16,828 KB
testcase_23 AC 158 ms
16,764 KB
testcase_24 AC 159 ms
16,932 KB
testcase_25 AC 194 ms
20,076 KB
testcase_26 AC 193 ms
20,168 KB
testcase_27 AC 196 ms
19,908 KB
testcase_28 AC 195 ms
20,044 KB
testcase_29 AC 197 ms
20,188 KB
testcase_30 AC 197 ms
20,112 KB
testcase_31 AC 195 ms
19,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class unionfind {
    vector<int> par;

public:
    unionfind(int n) {
        for (int i = 0; i < n; i++) {
            par.push_back(i);
        }
    }

    int find(int n) {
        if (par.at(n) == n) return n;
        return par.at(n) = find(par.at(n));
    }

    void merge(int m, int n) {
        m = find(m);
        n = find(n);
        if (m == n) return;
        par.at(m) = n;
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> b(n), c(n);
    for (int i = 0; i < n; i++) {
        cin >> b.at(i) >> c.at(i);
        b.at(i)--;
        c.at(i)--;
    }
    vector<vector<int>> color(n);
    for (int i = 0; i < n; i++) {
        color.at(c.at(i)).push_back(b.at(i));
    }
    unionfind t(m);
    for (int i = 0; i < n; i++) {
        for (int v : color.at(i)) {
            t.merge(v, color.at(i).at(0));
        }
    }
    set<int> s;
    for (int i = 0; i < m; i++) {
        s.insert(t.find(i));
    }
    cout << m - s.size() << endl;
}
0