結果

問題 No.1390 Get together
コンテスト
ユーザー zhenggeek
提出日時 2026-03-08 16:23:23
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 809 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,053 ms
コンパイル使用メモリ 335,552 KB
実行使用メモリ 19,040 KB
最終ジャッジ日時 2026-03-08 16:23:31
合計ジャッジ時間 6,032 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
const int N = 4e5+10;
int n,m;
int fa[N];
struct ball{
	int b,c;
}balls[N];
int find(int x){
	if(fa[x] == x){
		return x;
	}return fa[x] = find(fa[x]);
}
void unite(int x,int y){
	int nx = find(x),ny = find(y);
	if(nx != ny){
		fa[ny] = nx;
	}
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m;
	for(int i = 1;i<=m+n;i++){
		fa[i] = i;
	}
	unordered_set<int> boxst;
	for(int i = 1;i<=n;i++){
		cin>>balls[i].b>>balls[i].c;
		int box = balls[i].b;
		int color = balls[i].c + m;
		boxst.insert(box);
		unite(box,color);
	}
	unordered_set<int> kuai;
	for(int i = 1;i<=n;i++){
		int box= balls[i].b;
		int color = balls[i].c + m;
		kuai.insert(find(box));
		kuai.insert(find(color));
	}
	int ans = boxst.size() - kuai.size();
	cout<<ans;
	return 0;
}
0