結果

問題 No.1390 Get together
ユーザー 沙耶花沙耶花
提出日時 2021-02-12 21:26:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,550 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 206,416 KB
実行使用メモリ 13,524 KB
最終ジャッジ日時 2023-09-27 02:47:01
合計ジャッジ時間 5,300 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 53 ms
10,520 KB
testcase_17 AC 93 ms
13,256 KB
testcase_18 AC 55 ms
10,508 KB
testcase_19 AC 101 ms
13,184 KB
testcase_20 AC 105 ms
13,436 KB
testcase_21 AC 103 ms
13,256 KB
testcase_22 AC 81 ms
12,264 KB
testcase_23 AC 84 ms
12,544 KB
testcase_24 AC 81 ms
12,240 KB
testcase_25 AC 106 ms
13,348 KB
testcase_26 AC 109 ms
13,188 KB
testcase_27 AC 102 ms
13,524 KB
testcase_28 AC 104 ms
13,380 KB
testcase_29 AC 102 ms
13,184 KB
testcase_30 AC 102 ms
13,312 KB
testcase_31 AC 102 ms
13,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

}  // namespace atcoder

using namespace atcoder;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000001


int main(){
	
	int N,M;
	cin>>N>>M;
	dsu D(N+M);
	vector<vector<int>> C(N);
	rep(i,N){
		int b,c;
		scanf("%d %d",&b,&c);
		b--;c--;
		
		C[c].push_back(i);
		
		D.merge(b+N,i);
	}
	
	int ans = 0;
	
	rep(i,N){
		rep(j,C[i].size()){
			if(D.same(C[i][0],C[i][j]))continue;
			ans++;
			D.merge(C[i][0],C[i][j]);
		}
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0