結果

問題 No.697 池の数はいくつか
ユーザー 👑 KazunKazun
提出日時 2020-10-11 21:03:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,628 ms / 6,000 ms
コード長 1,590 bytes
コンパイル時間 1,032 ms
コンパイル使用メモリ 75,620 KB
実行使用メモリ 109,060 KB
最終ジャッジ日時 2024-04-25 20:53:57
合計ジャッジ時間 13,601 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 183 ms
13,184 KB
testcase_25 AC 184 ms
13,056 KB
testcase_26 AC 183 ms
13,056 KB
testcase_27 AC 182 ms
13,056 KB
testcase_28 AC 185 ms
13,184 KB
testcase_29 AC 1,567 ms
109,060 KB
testcase_30 AC 1,628 ms
91,432 KB
testcase_31 AC 1,575 ms
109,052 KB
testcase_32 AC 1,623 ms
91,380 KB
testcase_33 AC 1,628 ms
91,312 KB
testcase_34 AC 1,624 ms
91,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;

struct Union_Find {

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

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

	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 0;
}
0