結果

問題 No.1390 Get together
ユーザー okok
提出日時 2021-02-12 21:55:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 2,045 bytes
コンパイル時間 1,042 ms
コンパイル使用メモリ 92,236 KB
実行使用メモリ 23,496 KB
最終ジャッジ日時 2023-09-27 03:52:02
合計ジャッジ時間 5,085 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 107 ms
21,536 KB
testcase_17 AC 82 ms
19,280 KB
testcase_18 AC 109 ms
23,496 KB
testcase_19 AC 165 ms
21,404 KB
testcase_20 AC 142 ms
20,288 KB
testcase_21 AC 153 ms
19,940 KB
testcase_22 AC 142 ms
21,796 KB
testcase_23 AC 145 ms
21,524 KB
testcase_24 AC 145 ms
21,520 KB
testcase_25 AC 162 ms
21,268 KB
testcase_26 AC 176 ms
21,332 KB
testcase_27 AC 171 ms
21,328 KB
testcase_28 AC 175 ms
21,340 KB
testcase_29 AC 164 ms
21,428 KB
testcase_30 AC 161 ms
21,452 KB
testcase_31 AC 160 ms
21,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


class union_find
{
	int  _setnum;
	vector<int> par, nume;
public:
	union_find(){
	}
	
	union_find(int x){
		par.resize(x);
		nume.resize(x);
		init();
	}
	
	~union_find(){
		//
		
	}
	
	void clear(){
		_setnum = 0;
		par.clear();
		nume.clear();
	}
	
	void init(){
		_setnum = par.size();
		for(int i = 0; i < par.size(); i++){
			par[i] = i;
			nume[i] = 1;
		}
	}
	
	void resize(int x){
		
		par.resize(x);
		nume.resize(x);
		init();
	}

	int find(int x){
		return par[x] == x ? x : par[x] = find(par[x]);
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
	
		if(x == y)return;
		
		_setnum--;
		
		if(nume[x] > nume[y]) std::swap(x,y);
		
		par[x] = y;
		nume[y] += nume[x];
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}
	
	int numel(int x){
		return nume[find(x)];
	}
	
	int size(){
		return par.size();
	}
	
	int setnum(){
		return _setnum;
	}
};

signed main(){
	cout<<fixed<<setprecision(10);
	
	int N, M;
    int ans = 0;
    union_find uf, uf2;
    vector<bool> used;
    vector<int> con;
    vector<vector<int>> col;
    
    cin>>N>>M;
    
    uf.resize(N + M);
    used.resize(M + N);
    uf2.resize(N);
	col.resize(M);
    
    for(int i = 0; i < N; i++){
        int b, c;
        
        cin>>b>>c;
        
        b--;
        c--;
        
        uf.unite(b, M + c);
        if(col[b].size()) uf2.unite(c, col[b].back());
        
        col[b].push_back(c);
    }
    
    for(int i = 0; i < M; i++){
        if(used[uf.find(i)]) continue;
        if(!col[i].size()) continue;
        used[uf.find(i)] = true;
        ans += uf.numel(i) - uf2.numel(col[i].back()) - 1;
    }
	
    cout<<ans<<endl;
    
	return 0;
}
0