結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー Ping Chung ChangPing Chung Chang
提出日時 2023-08-29 21:44:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 753 bytes
コンパイル時間 5,151 ms
コンパイル使用メモリ 165,432 KB
実行使用メモリ 4,964 KB
最終ジャッジ日時 2023-08-29 21:44:25
合計ジャッジ時間 8,477 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 62 ms
4,872 KB
testcase_04 AC 12 ms
4,472 KB
testcase_05 AC 34 ms
4,376 KB
testcase_06 AC 41 ms
4,644 KB
testcase_07 AC 33 ms
4,376 KB
testcase_08 AC 38 ms
4,964 KB
testcase_09 AC 47 ms
4,376 KB
testcase_10 AC 58 ms
4,652 KB
testcase_11 AC 48 ms
4,584 KB
testcase_12 AC 36 ms
4,380 KB
testcase_13 AC 18 ms
4,380 KB
testcase_14 AC 27 ms
4,376 KB
testcase_15 AC 25 ms
4,376 KB
testcase_16 AC 58 ms
4,380 KB
testcase_17 AC 14 ms
4,376 KB
testcase_18 AC 30 ms
4,500 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 27 ms
4,376 KB
testcase_21 AC 8 ms
4,380 KB
testcase_22 AC 18 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second

const int mxn = 2e5+10;
int dsu[mxn],sz[mxn];

int root(int k){
	return k == dsu[k]?k:dsu[k] = root(dsu[k]);
}
inline void onion(int a,int b){
	a  = root(a),b = root(b);
	if(a == b)return;
	if(sz[a]<sz[b])swap(a,b);
	sz[a] += sz[b];
	dsu[b] = a;
	return;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i = 1;i<=n*2;i++){
		dsu[i] = i;
		sz[i] = 1;
	}
	for(int i = 0;i<m;i++){
		int a,b;
		cin>>a>>b;
		onion(a,b);
	}
	ll ans = n;
	for(int i = 1;i<=n*2;i++){
		if(i == dsu[i])ans -= sz[i]/2;
	}
	cout<<ans;
}
0