結果
| 問題 | No.697 池の数はいくつか |
| ユーザー |
kyuna
|
| 提出日時 | 2019-07-19 17:04:38 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 1,704 ms / 6,000 ms |
| コード長 | 1,524 bytes |
| 記録 | |
| コンパイル時間 | 1,114 ms |
| コンパイル使用メモリ | 76,832 KB |
| 実行使用メモリ | 73,728 KB |
| 最終ジャッジ日時 | 2024-11-08 08:21:01 |
| 合計ジャッジ時間 | 14,237 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> data;
int __size;
UnionFind(int size) : data(size, -1), __size(size) { }
bool unionSet(int x, int y) {
if ((x = root(x)) != (y = root(y))) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x; __size--;
}
return x != y;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
int size() { return __size; }
};
int main() {
const int ALIVE = 1;
int h, w; cin >> h >> w;
vector<vector<int>> field(h, vector<int>(w));
for (auto &row: field) for (auto &c: row) cin >> c;
auto idx = [&](int i, int j) { return i * w + j; };
auto out_field = [&](int i, int j) { return i < 0 || i >= h || j < 0 || j >= w; };
UnionFind uf(h * w);
int cnt = 0;
for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
if (field[i][j] != ALIVE) continue;
cnt++;
for (int di: {-1, 0, 1}) for (int dj: {-1, 0, 1}) {
if (di * di + dj * dj != 1) continue;
int ni = i + di, nj = j + dj;
if (out_field(ni, nj) || field[ni][nj] != ALIVE) continue;
if (idx(i, j) < idx(ni, nj)) uf.unionSet(idx(i, j), idx(ni, nj));
}
}
int cnt_wall = h * w - cnt;
cout << uf.size() - cnt_wall << endl;
return 0;
}
kyuna