結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                            👑  Kazun
                         | 
                    
| 提出日時 | 2020-10-11 20:59:28 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                RE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,647 bytes | 
| コンパイル時間 | 729 ms | 
| コンパイル使用メモリ | 74,872 KB | 
| 実行使用メモリ | 144,256 KB | 
| 最終ジャッジ日時 | 2024-11-25 16:34:28 | 
| 合計ジャッジ時間 | 12,568 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | RE * 3 | 
| other | RE * 32 | 
ソースコード
#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;
}
            
            
            
        
            
Kazun