結果

問題 No.697 池の数はいくつか
ユーザー 👑 KazunKazun
提出日時 2020-10-11 20:59:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,647 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 75,136 KB
実行使用メモリ 144,336 KB
最終ジャッジ日時 2024-05-04 07:09:04
合計ジャッジ時間 14,137 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct Union_Find {

	std::vector<int> parent;
	std::vector<int> rank;
	int N;

	Union_Find(int n) {
		N = n;
		parent = std::vector<int>(N, -1);
		rank = std::vector<int>(N, 0);
	}

	int find(int x) {
		if (parent[x] < 0) return x;
		return parent[x] = find(parent[x]);
	}

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

	void unite(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) return;
		if (parent[x] > parent[y]) std::swap(x, y);
		parent[x] += parent[y];
		parent[y] = x;
		return;
	}

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

	int group_count() {
		int K = 0;
		for (int i = 0; i < N; i++) {
			if (parent[i] < 0) K++;
		}
		return K;
	}

	std::vector<int> members(int x) {
		std::vector<int> v(0);

		int r = find(x);
		for (int i = 0; i < N; i++) {
			if (find(i) == r) v.push_back(i);
		}
		return v;
	}
};

int main() {
	int H, W;
	int K = 0;
	int a, b, c;

	cin >> H >> W;

	vector<vector<int>> S(H, vector<int>(W));
	vector<vector<int>> T(H, vector<int>(W, -1));

	for (int y = 0; y < H; y++) {
		for (int x = 0; x < W; x++) {
			cin >> S.at(y).at(x);

			if (S.at(y).at(x) == 1) {
				T.at(y).at(x) = K;
				K++;
			}
		}
	}

	Union_Find U(K);
	for (int y = 0; y < H; y++) {
		for (int x = 0; x < W; x++) {
			if (S.at(y).at(x) == 1) {
				a = T.at(y).at(x);
				if (y < H - 1 && S.at(y + 1).at(x) == 1) {
					b = T.at(y + 1).at(x);
					U.unite(a, b);
				}

				if (x < W - 1 && S.at(y).at(x + 1) == 1) {
					c = T.at(y).at(x + 1);
					U.unite(a, c);
				}
			}
		}
	}

	cout << U.group_count() << endl;
	return 1;
}
0