結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー ragnaragna
提出日時 2023-08-12 13:48:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,617 bytes
コンパイル時間 1,706 ms
コンパイル使用メモリ 176,440 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-04-30 04:09:52
合計ジャッジ時間 3,755 ms
ジャッジサーバーID
(参考情報)
judge3 / 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 96 ms
7,808 KB
testcase_04 AC 60 ms
9,728 KB
testcase_05 AC 43 ms
5,376 KB
testcase_06 AC 74 ms
8,320 KB
testcase_07 AC 44 ms
5,376 KB
testcase_08 AC 85 ms
9,984 KB
testcase_09 AC 56 ms
5,376 KB
testcase_10 AC 88 ms
7,040 KB
testcase_11 AC 79 ms
6,912 KB
testcase_12 AC 57 ms
6,528 KB
testcase_13 AC 24 ms
5,376 KB
testcase_14 AC 45 ms
5,504 KB
testcase_15 AC 38 ms
5,376 KB
testcase_16 AC 72 ms
5,376 KB
testcase_17 AC 19 ms
5,376 KB
testcase_18 AC 60 ms
7,552 KB
testcase_19 AC 56 ms
9,472 KB
testcase_20 AC 41 ms
5,376 KB
testcase_21 AC 30 ms
5,376 KB
testcase_22 AC 24 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

/// @brief Union-Find 木
/// @note 1.4 高速化 + 省メモリ化
/// @see https://zenn.dev/reputeless/books/standard-cpp-for-competitive-programming/viewer/union-find
class UnionFind
{
public:

	UnionFind() = default;

	/// @brief Union-Find 木を構築します。
	/// @param n 要素数
	explicit UnionFind(size_t n)
		: m_parentsOrSize(n, -1) {}

	/// @brief 頂点 i の root のインデックスを返します。
	/// @param i 調べる頂点のインデックス
	/// @return 頂点 i の root のインデックス
	int find(int i)
	{
		if (m_parentsOrSize[i] < 0)
		{
			return i;
		}

		// 経路圧縮
		return (m_parentsOrSize[i] = find(m_parentsOrSize[i]));
	}

	/// @brief a のグループと b のグループを統合します。
	/// @param a 一方のインデックス
	/// @param b 他方のインデックス
	void merge(int a, int b)
	{
		a = find(a);
		b = find(b);

		if (a != b)
		{
			// union by size (小さいほうが子になる)
			if (-m_parentsOrSize[a] < -m_parentsOrSize[b])
			{
				std::swap(a, b);
			}

			m_parentsOrSize[a] += m_parentsOrSize[b];
			m_parentsOrSize[b] = a;
		}
	}

	/// @brief a と b が同じグループに属すかを返します。
	/// @param a 一方のインデックス
	/// @param b 他方のインデックス
	/// @return a と b が同じグループに属す場合 true, それ以外の場合は false
	bool connected(int a, int b)
	{
		return (find(a) == find(b));
	}

	/// @brief i が属するグループの要素数を返します。
	/// @param i インデックス
	/// @return i が属するグループの要素数
	int size(int i)
	{
		return -m_parentsOrSize[find(i)];
	}

private:

	// m_parentsOrSize[i] は i の 親,
	// ただし root の場合は (-1 * そのグループに属する要素数)
	std::vector<int> m_parentsOrSize;
};
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rrep(i,a,b) for(ll i=(ll)(a-1);i>=(ll)(b);i--)
#define MOD 998244353
//#define MOD 1e9+7
#define INF 1e16
//#define PI numbers::pi
//#define E numbers::e
template <typename T> bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template <typename T> bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

int main(){
    ll n,m; cin >> n >> m;
    ll ans=0;
    UnionFind uf(2*n);
    set<ll> st;
    rep(i,0,m){
        ll a,b; cin >> a >> b;
        a--,b--;
        uf.merge(a,b);
    }
    rep(i,0,2*n){
        if(!st.count(uf.find(i))&&uf.size(i)%2==1){
            ans++;
            st.insert(uf.find(i));
        }
    }
    cout << ans/2 << endl;
}
0