結果

問題 No.1390 Get together
ユーザー leaf_1415leaf_1415
提出日時 2021-02-12 21:27:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,108 bytes
コンパイル時間 3,507 ms
コンパイル使用メモリ 91,200 KB
実行使用メモリ 18,420 KB
最終ジャッジ日時 2023-09-27 02:49:15
合計ジャッジ時間 5,760 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
12,400 KB
testcase_01 AC 8 ms
12,404 KB
testcase_02 AC 8 ms
12,260 KB
testcase_03 AC 9 ms
12,372 KB
testcase_04 AC 9 ms
12,260 KB
testcase_05 AC 10 ms
12,476 KB
testcase_06 AC 9 ms
12,328 KB
testcase_07 AC 9 ms
12,412 KB
testcase_08 AC 9 ms
12,312 KB
testcase_09 AC 10 ms
12,436 KB
testcase_10 AC 8 ms
12,220 KB
testcase_11 AC 8 ms
12,476 KB
testcase_12 AC 8 ms
12,220 KB
testcase_13 AC 8 ms
12,344 KB
testcase_14 AC 8 ms
12,404 KB
testcase_15 AC 8 ms
12,372 KB
testcase_16 AC 65 ms
16,108 KB
testcase_17 AC 84 ms
18,148 KB
testcase_18 AC 68 ms
16,008 KB
testcase_19 AC 96 ms
18,196 KB
testcase_20 AC 93 ms
18,144 KB
testcase_21 AC 93 ms
18,144 KB
testcase_22 AC 78 ms
17,332 KB
testcase_23 AC 79 ms
17,176 KB
testcase_24 AC 80 ms
17,260 KB
testcase_25 AC 93 ms
18,280 KB
testcase_26 AC 96 ms
18,268 KB
testcase_27 AC 96 ms
18,264 KB
testcase_28 AC 96 ms
18,144 KB
testcase_29 AC 94 ms
18,200 KB
testcase_30 AC 95 ms
18,132 KB
testcase_31 AC 93 ms
18,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define outl(x) cout << x << endl
#define SP << " " << 
#define inf 1e18

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct UnionFind{
	int size;
	vector<int> parent;
	vector<int> rank;
	vector<int> v, e;
	
	UnionFind(){}
	UnionFind(int size){
		this->size = size;
		parent.resize(size+1);
		rank.resize(size+1);
		v.resize(size+1);
		e.resize(size+1);
		init();
	}
	void init(){
		for(int i = 0; i <= size; i++){
			parent[i] = i, rank[i] = 0;
			v[i] = 1, e[i] = 0;
		}
	}
	int root(int i){
		if(parent[i] == i) return i;
		return parent[i] = root(parent[i]);
	}
	bool same(int i, int j){
		return root(i) == root(j);
	}
	void merge(int i, int j){ // j will become new root
		parent[i] = j;
		v[j] += v[i];
		e[j] += e[i] + 1;
	}
	void unite(int i, int j){
		int root_i = root(i), root_j = root(j);
		if(root_i == root_j){
			e[root_i]++;
			return;
		}
		if(rank[root_i] < rank[root_j]) merge(root_i, root_j);
		else merge(root_j, root_i);
		if(rank[root_i] == rank[root_j]) rank[root_i]++;
	}
};


ll n, m;
ll b[200005], c[200005];
vector<ll> vec[200005];
UnionFind uf(200005);

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	rep(i, 1, n) cin >> b[i] >> c[i];
	
	rep(i, 1, n) vec[c[i]].push_back(b[i]);
	ll ans = 0;
	rep(i, 1, n){
		sort(all(vec[i]));
		if(vec[i].size() <= 1) continue;
		
		rep(j, 1, (int)vec[i].size()-1){
			ll u = vec[i][j-1], v = vec[i][j];
			if(!uf.same(u, v)) ans++, uf.unite(u, v);
		}
	}
	cout << ans << endl;
	
	return 0;
}
0