結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー shop_oneshop_one
提出日時 2023-08-12 13:51:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,526 bytes
コンパイル時間 1,125 ms
コンパイル使用メモリ 115,196 KB
実行使用メモリ 8,284 KB
最終ジャッジ日時 2024-04-30 04:16:39
合計ジャッジ時間 2,720 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 35 ms
8,284 KB
testcase_04 AC 10 ms
6,876 KB
testcase_05 AC 24 ms
6,080 KB
testcase_06 AC 23 ms
7,648 KB
testcase_07 AC 24 ms
5,572 KB
testcase_08 AC 25 ms
8,016 KB
testcase_09 AC 31 ms
6,568 KB
testcase_10 AC 36 ms
8,060 KB
testcase_11 AC 32 ms
7,740 KB
testcase_12 AC 21 ms
6,620 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 18 ms
5,952 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 38 ms
7,264 KB
testcase_17 AC 11 ms
5,376 KB
testcase_18 AC 19 ms
7,112 KB
testcase_19 AC 7 ms
6,200 KB
testcase_20 AC 21 ms
5,376 KB
testcase_21 AC 12 ms
5,376 KB
testcase_22 AC 13 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
class union_find{
  int n;
  vector<int> par,rk,size;
  public:
  union_find(int max_n){
    n = max_n+1;
    for(int i=0;i<=n;i++){
      par.emplace_back(i);
      rk.emplace_back(0);
      size.emplace_back(1);
    }
  }
  int root(int x){
    vector<int> nodes;
    while(x!=par[x]){
      nodes.emplace_back(x);
      x = par[x];
    }
    for(auto v:nodes){
      par[v] = x;
    }
    return x;
  }
  void unite(int x,int y){
    x = root(x);
    y = root(y);
    if(x==y) return;
    if(rk[x]<rk[y]){
      par[x] = y;
      size[y] += size[x];
    }else{
      par[y] = x;
      if(rk[x]==rk[y]) rk[x]++;
      size[x] += size[y];
    }
  }
  bool same(int x,int y){
    return root(x)==root(y);
  }
  int treesize(int x){
    return size[root(x)];
  }
};
signed main(){
  init_io();
  ll n,m;
  cin >> n >> m;
  vector<ll> a(m), b(m);
  vector<ll> used(2*n, false);
  union_find uf(2*n);
  for (int i=0;i<m;i++) {
    cin >> a[i] >> b[i];
    uf.unite(a[i]-1, b[i]-1);
  }
  ll ans = 0;
  for (int i=0;i<2*n;i++) {
    ll r = uf.root(i);
    if (used[r]) continue;
    used[r] = true;
    ans += uf.treesize(r) % 2;
  }
  cout << ans/2 << endl;
}
0