結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー 0214sh70214sh7
提出日時 2023-08-12 13:52:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 2,005 bytes
コンパイル時間 2,435 ms
コンパイル使用メモリ 208,244 KB
実行使用メモリ 6,928 KB
最終ジャッジ日時 2024-04-30 04:18:49
合計ジャッジ時間 4,281 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 59 ms
6,928 KB
testcase_04 AC 11 ms
5,608 KB
testcase_05 AC 42 ms
5,480 KB
testcase_06 AC 40 ms
6,312 KB
testcase_07 AC 46 ms
5,376 KB
testcase_08 AC 40 ms
6,812 KB
testcase_09 AC 55 ms
5,804 KB
testcase_10 AC 60 ms
6,844 KB
testcase_11 AC 52 ms
6,632 KB
testcase_12 AC 35 ms
5,748 KB
testcase_13 AC 18 ms
5,376 KB
testcase_14 AC 33 ms
5,376 KB
testcase_15 AC 40 ms
5,376 KB
testcase_16 AC 63 ms
6,348 KB
testcase_17 AC 20 ms
5,376 KB
testcase_18 AC 31 ms
6,128 KB
testcase_19 AC 8 ms
5,376 KB
testcase_20 AC 43 ms
5,376 KB
testcase_21 AC 21 ms
5,376 KB
testcase_22 AC 25 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PP;
//#define MOD 1000000007
#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define ALL(v) (v).begin(), (v).end()
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false

class unionfind{
    // Copyright (c) 2023 0214sh7
    // https://github.com/0214sh7/library/
    private:
    std::vector<int> UF,rank,size_;
    public:
    
    void init(int N){
        UF.clear();
        rank.clear();
        size_.clear();
        for(int i=0;i<N;i++){
            UF.push_back(i);
            rank.push_back(0);
            size_.push_back(1);
        }
    }
    
    unionfind(int N){
        init(N);
    }
    
    int root(int k){
        if(UF[k]==k){
            return k;
        }else{
            UF[k]=root(UF[k]);
            return UF[k];
        }
    }
    
    bool same(int p,int q){
        return root(p)==root(q);
    }
    
    void unite(int P,int Q){
        int p=root(P);
        int q=root(Q);
        if(p==q)return;
        if(rank[p]<rank[q])std::swap(p,q);
        UF[q]=p;
        if(rank[p]==rank[q])rank[p]++;
        size_[p] += size_[q];
        size_[q] = 0;
    }
    
    int size(int k){
        return size_[root(k)];
    }
    
};

int main(void){
    //cin.tie(nullptr);
    //ios::sync_with_stdio(false);
    
    ll N,M;
    cin >> N >> M;
    vector<ll> A(M),B(M);
    REP(i,M){
        cin >> A[i] >> B[i];
        A[i]--;B[i]--;
    }

    unionfind UF(2*N);
    REP(i,M){
        UF.unite(A[i],B[i]);
    }

    ll Ans = 0;
    REP(i,2*N){
        if(i!=UF.root(i))continue;
        if(UF.size(i)%2==1)Ans++;
    }

    Ans /= 2;
    cout << Ans << endl;

    return 0;
}
0