結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー ryota2357ryota2357
提出日時 2023-07-11 13:22:40
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 1,428 bytes
コンパイル時間 887 ms
コンパイル使用メモリ 127,488 KB
実行使用メモリ 20,096 KB
最終ジャッジ日時 2024-04-30 02:51:42
合計ジャッジ時間 3,412 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 148 ms
15,232 KB
testcase_04 AC 78 ms
18,304 KB
testcase_05 AC 51 ms
5,632 KB
testcase_06 AC 126 ms
16,256 KB
testcase_07 AC 48 ms
5,376 KB
testcase_08 AC 145 ms
20,096 KB
testcase_09 AC 66 ms
6,064 KB
testcase_10 AC 138 ms
12,928 KB
testcase_11 AC 121 ms
12,800 KB
testcase_12 AC 85 ms
11,776 KB
testcase_13 AC 30 ms
6,092 KB
testcase_14 AC 63 ms
8,576 KB
testcase_15 AC 40 ms
5,376 KB
testcase_16 AC 86 ms
8,332 KB
testcase_17 AC 20 ms
5,376 KB
testcase_18 AC 94 ms
14,464 KB
testcase_19 AC 67 ms
17,408 KB
testcase_20 AC 44 ms
5,376 KB
testcase_21 AC 39 ms
7,552 KB
testcase_22 AC 26 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind
{
    vector<int> data;
    const uint __size;
    UnionFind(const int size = 0) noexcept : data(size, -1), __size(max(0, size)) {}
    inline int root(const int& x) {
        assert((uint)x < __size);
        if (data[x] < 0) return x;
        return data[x] = root(data[x]);
    }
    inline bool unite(const int& x, const int& y) {
        int rx = root(x), ry = root(y);
        if (rx == ry) return false;
        if (data[rx] > data[ry]) swap(rx, ry);
        data[rx] += data[ry];
        data[ry] = rx;
        return true;
    }
    inline bool same(const int& x, const int& y) { return root(x) == root(y); }
    inline int size(const int& x) { return -data[root(x)]; }
};

int main() {
    int n, m;
    cin >> n >> m;
    assert(1 <= n && n <= 100000);
    assert(0 <= m && m <= 100000);
    UnionFind uf(2 * n);

    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        assert(1 <= a && a <= 2 * n);
        assert(1 <= b && b <= 2 * n);
        a -= 1;
        b -= 1;
        uf.unite(a, b);
    }

    map<int, vector<int>> group;
    for (int i = 0; i < 2 * n; ++i) {
        group[uf.root(i)].push_back(i);
    }

    int ans = 0;
    for (auto [_, g] : group) {
        if (g.size() % 2) {
            ans += 1;
        }
    }
    cout << ans / 2 << endl;
    return 0;
}
0