結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー 👑 Nafmo2Nafmo2
提出日時 2023-06-21 01:32:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 919 bytes
コンパイル時間 2,225 ms
コンパイル使用メモリ 210,308 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-30 02:51:03
合計ジャッジ時間 5,158 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 96 ms
8,704 KB
testcase_04 AC 41 ms
10,112 KB
testcase_05 AC 44 ms
5,376 KB
testcase_06 AC 70 ms
9,216 KB
testcase_07 AC 45 ms
5,376 KB
testcase_08 AC 81 ms
10,880 KB
testcase_09 AC 56 ms
5,376 KB
testcase_10 AC 91 ms
7,680 KB
testcase_11 AC 78 ms
7,552 KB
testcase_12 AC 57 ms
7,168 KB
testcase_13 AC 24 ms
5,376 KB
testcase_14 AC 47 ms
5,760 KB
testcase_15 AC 38 ms
5,376 KB
testcase_16 AC 72 ms
5,632 KB
testcase_17 AC 19 ms
5,376 KB
testcase_18 AC 57 ms
8,320 KB
testcase_19 AC 38 ms
9,728 KB
testcase_20 AC 42 ms
5,376 KB
testcase_21 AC 29 ms
5,376 KB
testcase_22 AC 23 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind {
  vector<int> data;
  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return (false);
    if (data[x] > data[y]) std::swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  bool same(int x, int y) { return find(x) == find(y); }
  int size(int k) { return (-data[find(k)]); }
};

int main(void) {
  int ans = 0;
  int N, M;
  cin >> N >> M;
  UnionFind uf(2 * N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.unite(a, b);
  }
  map<int, int> parent;
  for (int i = 0; i < 2 * N; i++) {
    parent[uf.find(i)]++;
  }
  for (auto [x, y] : parent) {
    if (y & 1) {
      ans++;
    }
  }
  cout << (ans + 1) / 2 << endl;
}
0