結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー marble72marble72
提出日時 2023-08-12 14:19:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 999 bytes
コンパイル時間 1,699 ms
コンパイル使用メモリ 175,124 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-30 05:41:59
合計ジャッジ時間 4,000 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 90 ms
8,704 KB
testcase_04 AC 50 ms
10,112 KB
testcase_05 AC 42 ms
5,376 KB
testcase_06 AC 73 ms
9,216 KB
testcase_07 AC 42 ms
5,376 KB
testcase_08 AC 81 ms
10,880 KB
testcase_09 AC 54 ms
5,376 KB
testcase_10 AC 86 ms
7,680 KB
testcase_11 AC 78 ms
7,552 KB
testcase_12 AC 59 ms
7,168 KB
testcase_13 AC 23 ms
5,376 KB
testcase_14 AC 45 ms
5,760 KB
testcase_15 AC 37 ms
5,376 KB
testcase_16 AC 70 ms
5,376 KB
testcase_17 AC 18 ms
5,376 KB
testcase_18 AC 59 ms
8,320 KB
testcase_19 AC 50 ms
9,600 KB
testcase_20 AC 40 ms
5,376 KB
testcase_21 AC 31 ms
5,376 KB
testcase_22 AC 24 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind
{
  vector< int > data;

  UnionFind(int sz)
  {
    data.assign(sz, -1);
  }

  bool unite(int x, int y)
  {
    x = find(x), y = find(y);
    if(x == y) return(false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return(true);
  }

  int find(int k)
  {
    if(data[k] < 0) return(k);
    return(data[k] = find(data[k]));
  }

  int size(int k)
  {
    return(-data[find(k)]);
  }
};

int main(){
    int N,M;
    cin >> N >> M;
    UnionFind g(2*N);
    for(int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        a--;b--;
        g.unite(a,b);
    }
    set<int> S;
    int ans=0;
    for(int i=0;i<2*N;i++){
        int f = g.find(i);
        int s = g.size(i);
        if(S.count(f) != 0){
            continue;
        }else{
            if(s%2 != 0){
                ans++;
            }
            S.insert(f);
        }
    }
    cout << ans/2 << endl;
}
0