結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー ayataka5ayataka5
提出日時 2023-08-12 13:43:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,427 bytes
コンパイル時間 2,210 ms
コンパイル使用メモリ 206,312 KB
実行使用メモリ 5,504 KB
最終ジャッジ日時 2024-04-30 03:56:40
合計ジャッジ時間 3,937 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 57 ms
5,376 KB
testcase_04 AC 9 ms
5,376 KB
testcase_05 AC 41 ms
5,376 KB
testcase_06 AC 37 ms
5,376 KB
testcase_07 AC 45 ms
5,376 KB
testcase_08 AC 37 ms
5,376 KB
testcase_09 AC 54 ms
5,376 KB
testcase_10 AC 58 ms
5,504 KB
testcase_11 AC 49 ms
5,376 KB
testcase_12 AC 34 ms
5,376 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 32 ms
5,376 KB
testcase_15 AC 41 ms
5,376 KB
testcase_16 AC 62 ms
5,376 KB
testcase_17 AC 21 ms
5,376 KB
testcase_18 AC 29 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 44 ms
5,376 KB
testcase_21 AC 21 ms
5,376 KB
testcase_22 AC 24 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

/* thx for Kohenyan (Debug) */
struct UnionFind {
    vector<int> par, rank;
    //init size N
    UnionFind(int N) {
        par.assign(N, int()), rank.assign(N, int());
        for(int i = 0; i < N; i++) {
            par[i] = i;
            rank[i] = 1;
        }
    }

    // Find Loot
    int find(int x) {
        if(par[x] == x) {
            return x;
        }else {
            return par[x] = find(par[x]);
        }
    }

    // Unite x and y
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) return;

        if(rank[x] < rank[y]) {
            par[x] = y;
            rank[y] += rank[x];
        }else {
            par[y] = x;
            rank[x] += rank[y];
        }
    }

    // isSametree?
    bool same(int x, int y) {
        return find(x) == find(y);
    }

    // size of x's set
    int size(int x) {
        return rank[find(x)];
    }
};

int main() {
    int N, M;
    cin >> N >> M;
    vector A(M, 0), B(M, 0);
    for(int i = 0; i < M; i++) cin >> A[i] >> B[i];
    UnionFind UF(2*N);
    for(int i = 0; i < M; i++) UF.unite(A[i]-1, B[i]-1);
    int ans(N);
    vector seen(2*N, false);
    for(int i = 0; i < 2*N; i++) {
        if(seen[UF.find(i)]) continue;
        else {
            ans -= UF.size(i) / 2;
            seen[UF.find(i)] = true;
        }
    }
    cout << ans << endl;
    return 0;
}
0