結果

問題 No.1390 Get together
ユーザー MitarushiMitarushi
提出日時 2021-02-12 23:41:18
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,574 bytes
コンパイル時間 5,787 ms
コンパイル使用メモリ 130,504 KB
実行使用メモリ 6,212 KB
最終ジャッジ日時 2023-09-27 07:24:47
合計ジャッジ時間 9,146 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 45 ms
6,152 KB
testcase_17 AC 43 ms
6,148 KB
testcase_18 AC 46 ms
6,160 KB
testcase_19 AC 51 ms
6,212 KB
testcase_20 AC 49 ms
6,160 KB
testcase_21 AC 50 ms
6,168 KB
testcase_22 AC 52 ms
6,148 KB
testcase_23 AC 52 ms
6,152 KB
testcase_24 AC 49 ms
6,156 KB
testcase_25 AC 51 ms
6,116 KB
testcase_26 AC 51 ms
6,152 KB
testcase_27 AC 50 ms
6,152 KB
testcase_28 AC 50 ms
6,192 KB
testcase_29 AC 50 ms
6,144 KB
testcase_30 AC 52 ms
6,188 KB
testcase_31 AC 51 ms
6,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template<class T>
void print(const T &t) { std::cout << t << '\n'; }

template<class Head, class... Tail>
void print(const Head &head, const Tail &... tail) {
    std::cout << head << ' ';
    print(tail...);
}

template<typename T>
using v = std::vector<T>;
using ll = long long;
using std::cin;

struct UnionFind {
    v<int> parent, rank, count;

    explicit UnionFind(int n) :
            parent(n, -1), rank(n, 1), count(n, 1) {
    }

    int get_root(int n) {
        if (parent[n] == -1) {
            return n;
        } else {
            return parent[n] = get_root(parent[n]);
        }
    }

    bool is_same(int i, int j) {
        return get_root(i) == get_root(j);
    }

    void merge(int i, int j) {
        i = get_root(i);
        j = get_root(j);
        if (i == j) {
            return;
        }
        if (rank[i] < rank[j]) {
            std::swap(i, j);
        }
        parent[j] = i;
        rank[i] = std::max(rank[i], rank[j] + 1);
        count[i] += count[j];
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    int n, m;
    cin >> n >> m;

    v<int> col(n, -1);
    UnionFind uf(m);

    for (int i = 0; i < n; ++i) {
        int b, c;
        cin >> b >> c;
        b--;
        c--;
        if (col[c] != -1) {
            uf.merge(b, col[c]);
        }
        col[c] = b;
    }

    int ans = 0;
    for (int i = 0; i < m; ++i) {
        if (uf.parent[i] == -1) {
            ans += uf.count[i] - 1;
        }
    }
    print(ans);
}
0