結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー takumi152takumi152
提出日時 2023-08-12 13:48:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,506 bytes
コンパイル時間 1,164 ms
コンパイル使用メモリ 108,320 KB
実行使用メモリ 11,080 KB
最終ジャッジ日時 2024-04-30 04:08:50
合計ジャッジ時間 2,785 ms
ジャッジサーバーID
(参考情報)
judge1 / 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 43 ms
9,968 KB
testcase_04 AC 24 ms
10,052 KB
testcase_05 AC 20 ms
5,376 KB
testcase_06 AC 36 ms
9,816 KB
testcase_07 AC 20 ms
5,376 KB
testcase_08 AC 39 ms
11,080 KB
testcase_09 AC 25 ms
5,504 KB
testcase_10 AC 38 ms
8,308 KB
testcase_11 AC 34 ms
8,224 KB
testcase_12 AC 26 ms
7,548 KB
testcase_13 AC 12 ms
5,376 KB
testcase_14 AC 23 ms
6,528 KB
testcase_15 AC 15 ms
5,376 KB
testcase_16 AC 32 ms
6,528 KB
testcase_17 AC 9 ms
5,376 KB
testcase_18 AC 30 ms
9,264 KB
testcase_19 AC 20 ms
9,400 KB
testcase_20 AC 17 ms
5,376 KB
testcase_21 AC 14 ms
5,504 KB
testcase_22 AC 11 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <unordered_map>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 998244353;

struct unionfind {
  vector<int> group;
  vector<int> rank;

  unionfind() {
    unionfind(0);
  }

  unionfind(int n) {
    group = vector<int>(n);
    rank = vector<int>(n);
    for (int i = 0; i < n; i++) group[i] = i;
  }

  int root(int x) {
    if (group[x] == x) return x;
    return group[x] = root(group[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rank[rx] > rank[ry]) group[ry] = rx;
    else if (rank[ry] > rank[rx]) group[rx] = ry;
    else if (rx != ry) {
      group[ry] = rx;
      rank[rx]++;
    }
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<Pii> ab(m);
  for (auto &x: ab) cin >> x.first >> x.second;

  unionfind uf(n * 2);
  for (int i = 0; i < m; i++) {
    uf.unite(ab[i].first - 1, ab[i].second - 1);
  }

  unordered_map<int, int> group_count;
  for (int i = 0; i < n * 2; i++) {
    group_count[uf.root(i)]++;
  }

  int odd_count_num = 0;
  for (auto &[_, count]: group_count) {
    if (count % 2 != 0) odd_count_num++;
  }

  int ans = odd_count_num / 2;

  cout << ans << endl;

  return 0;
}
0