結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー えらーめぐえらーめぐ
提出日時 2023-08-12 13:55:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,644 bytes
コンパイル時間 1,808 ms
コンパイル使用メモリ 170,132 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-30 04:29:43
合計ジャッジ時間 3,407 ms
ジャッジサーバーID
(参考情報)
judge2 / 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 59 ms
5,376 KB
testcase_04 AC 8 ms
5,376 KB
testcase_05 AC 41 ms
5,376 KB
testcase_06 AC 39 ms
5,376 KB
testcase_07 AC 45 ms
5,376 KB
testcase_08 AC 39 ms
5,376 KB
testcase_09 AC 56 ms
5,376 KB
testcase_10 AC 60 ms
5,376 KB
testcase_11 AC 50 ms
5,376 KB
testcase_12 AC 34 ms
5,376 KB
testcase_13 AC 18 ms
5,376 KB
testcase_14 AC 32 ms
5,376 KB
testcase_15 AC 39 ms
5,376 KB
testcase_16 AC 64 ms
5,376 KB
testcase_17 AC 21 ms
5,376 KB
testcase_18 AC 29 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 43 ms
5,376 KB
testcase_21 AC 20 ms
5,376 KB
testcase_22 AC 24 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct dsu{
    //宣言方法:dsu(int 超点数)

    vector<int>parent;
    vector<int>size;

    //初期化処理
    dsu(int n):parent(n,-1),size(n,1){}//メンバ変数、-1なの:最初ぜんぶ根っこ 1なの:それぞれのグループのサイズがぜんぶ1

    int root(int x){
        if(parent[x]==-1)return x;//xが根っこ
        parent[x]=root(parent[x]);//根っこを呼び出す(メモ化再帰
        return parent[x];
    }

    bool same(int x,int y){
        return root(x)==root(y);//同じ根っこに属してるか(連結か)
    }
    void merge(int x,int y){
        int rootx=root(x),rooty=root(y);//xの根っことyの根っこ
        if(same(x,y))return;//すでに連結なので、なにもしない
        
        if(size[rootx]<size[rooty]){//計算量を減らすための場合分け
            parent[rootx] = root(y);//xの根っこをyの根っこにくっつける(親をyに)
            size[rooty]+=size[rootx];//yのいるグループのサイズがふえた
        }
        else{
            parent[rooty] = root(x);
            size[rootx]+=size[rooty];
        }
    }
};

int main(){
    int N,M;cin >> N >> M;
    //人数:2N
    dsu UF(N*2);
    for(int i = 0;i < M;i++){
        int a,b;cin >> a >> b;a--;b--;
        UF.merge(a,b);
    }
    int cnt = 0;
    for(int i = 0;i < N*2;i++){//i人目について
        if(UF.root(i)!=i)continue;//i人目が根っこじゃないとき
        if(UF.size[i]%2==0)continue;//グループ内で組ができる
        else cnt++;
        //
    }
    cout << cnt/2 << endl;
}
0