結果

問題 No.1390 Get together
ユーザー misora192misora192
提出日時 2021-02-14 20:59:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,857 bytes
コンパイル時間 1,863 ms
コンパイル使用メモリ 185,544 KB
実行使用メモリ 29,100 KB
最終ジャッジ日時 2023-09-29 13:56:56
合計ジャッジ時間 9,895 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 168 ms
18,376 KB
testcase_17 AC 194 ms
13,068 KB
testcase_18 AC 193 ms
26,496 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

// union-find tree with size(struct)
struct UnionFind{
    vector<int> par; // 親ノード
    vector<int> rank; // ランク
	vector<int> size; // 連結成分のサイズ

    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n);
		rank.resize(n);
		size.resize(n);
        for (int i = 0; i < n; ++i){
			par[i] = i;
			rank[i] = 0;
			size[i] = 1;
		}
    }

    int find(int x) {
        if (par[x] == x)  return x;

        int r = find(par[x]);
        return par[x] = r;
    }

    bool issame(int x, int y) {
        return find(x) == find(y);
    }

    bool unite(int x, int y) {
	    x = find(x);
		y = find(y);
	    if (x == y) return false;

	    if (rank[x] < rank[y]) swap(x, y);
	    if (rank[x] == rank[y]) ++rank[x];
	    par[y] = x;
		size[x] += size[y];
	    return true;
    }

	int getSize(int x){
		return size[find(x)];
	}

	int getRank(int x){
		return rank[find(x)];
	}
};


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, M;
	cin >> N >> M;

	vector<int> b(N), c(N);
	rep(i, N) {
		cin >> b[i] >> c[i];
		b[i]--;
		c[i]--;
	}


	int col = 0;
	map<int, int> cmap;
	rep(i, N){
		if(cmap.count(c[i]) == 0){
			cmap[c[i]] = col;
			col++;
		}
	}

	UnionFind uf(col);
	map<int, vector<int>> bmap;
	rep(i, N){
		if(bmap.count(b[i]) == 0){
			bmap[b[i]].push_back(cmap[c[i]]);
		}else{
			uf.unite(bmap[b[i]].back(), cmap[c[i]]);
			bmap[b[i]].push_back(cmap[c[i]]);
		}
	}

	int box = bmap.size();
	set<int> st;
	rep(i, col) st.insert(uf.par[i]);

	cout << box - st.size() << endl;
}
0