結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー LaFolia13LaFolia13
提出日時 2023-07-29 16:55:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 1,123 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 174,916 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-30 02:51:54
合計ジャッジ時間 3,994 ms
ジャッジサーバーID
(参考情報)
judge3 / 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 104 ms
8,704 KB
testcase_04 AC 57 ms
10,112 KB
testcase_05 AC 44 ms
5,376 KB
testcase_06 AC 80 ms
9,216 KB
testcase_07 AC 45 ms
5,376 KB
testcase_08 AC 93 ms
10,880 KB
testcase_09 AC 58 ms
5,376 KB
testcase_10 AC 96 ms
7,680 KB
testcase_11 AC 85 ms
7,552 KB
testcase_12 AC 62 ms
7,168 KB
testcase_13 AC 26 ms
5,376 KB
testcase_14 AC 48 ms
5,760 KB
testcase_15 AC 38 ms
5,376 KB
testcase_16 AC 75 ms
5,504 KB
testcase_17 AC 19 ms
5,376 KB
testcase_18 AC 62 ms
8,320 KB
testcase_19 AC 56 ms
9,600 KB
testcase_20 AC 42 ms
5,376 KB
testcase_21 AC 32 ms
5,376 KB
testcase_22 AC 24 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9+7;
constexpr llong inf = mod * mod;

struct union_find {
  std::vector<int> par;

  union_find() {}
  union_find(int n) :
    par(n+1, -1)
  {}

  int root(int x) {
    if(par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  int size(int x) {
    return -par[root(x)];
  }
  bool same(int a, int b) {
    return root(a) == root(b);
  }
  bool merge(int a, int b) {
    a = root(a), b = root(b);
    if(a == b) return false;
    if(par[a] > par[b]) std::swap(a, b);
    par[a] += par[b];
    par[b] = a;
    return true;
  }
};

int main() {
  llong N, M;
  cin >> N >> M;

  union_find uf(2 * N);
  for (int i = 0; i < M; i++) {
    llong A, B;
    cin >> A >> B;
    uf.merge(A, B);
  }

  set<llong> used;
  llong cnt = 0;
  for (int i = 1; i <= 2 * N; i++) {
    if (used.count(uf.root(i))) {
      continue;
    }
    used.insert(uf.root(i));
    cnt += uf.size(i) % 2;
  }

  cout << cnt / 2 << endl;

  return 0;
}
0