結果
| 問題 |
No.697 池の数はいくつか
|
| ユーザー |
👑 Kazun
|
| 提出日時 | 2020-10-24 01:28:29 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,362 ms / 6,000 ms |
| コード長 | 1,693 bytes |
| コンパイル時間 | 998 ms |
| コンパイル使用メモリ | 75,204 KB |
| 実行使用メモリ | 109,056 KB |
| 最終ジャッジ日時 | 2024-11-08 08:47:43 |
| 合計ジャッジ時間 | 15,935 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#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) {
std::vector<int> V(0);
while (parent[x] >= 0) {
V.push_back(x);
x = parent[x];
}
for (int i = 0; i < V.size(); i++) {
parent[V[i]] = x;
}
return 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;
}
Kazun