結果
| 問題 |
No.697 池の数はいくつか
|
| ユーザー |
|
| 提出日時 | 2018-08-12 21:44:29 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 267 ms / 6,000 ms |
| コード長 | 1,238 bytes |
| コンパイル時間 | 1,917 ms |
| コンパイル使用メモリ | 171,576 KB |
| 実行使用メモリ | 38,400 KB |
| 最終ジャッジ日時 | 2024-11-08 08:02:09 |
| 合計ジャッジ時間 | 5,667 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
constexpr int LAND = 10'000'000;
struct UnionFind{
vector<int> data;
UnionFind(int size) : data(size, -1){}
void unite(int x, int y){
x = root(x);
y = root(y);
if (x != y){
if (data[y] < data[x]) swap(x, y);
data[x] += data[y];
data[y] = x;
}
}
int root(int x) {return data[x] < 0 ? x : data[x] = root(data[x]);}
int size(int x) {return -data[root(x)];}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W;
cin >> H >> W;
UnionFind uf(H * W);
vector<char> pre(W);
vector<char> cur(W);
for (int h = 0 ; h < H; ++h){
int hW = h * W;
for (auto & c : cur) cin >> c;
for (int w = 0; w < W; ++w){
if (cur[w] == '1'){
if (w > 0 and cur[w - 1] == '1') uf.unite(hW + w, hW + w - 1);
if (h > 0 and pre[w] == '1') uf.unite(hW + w, hW + w - W);
}else{
uf.data[hW + w] = LAND;
}
}
swap(pre, cur);
}
// count the number of ponds
int ans = 0;
for (auto v : uf.data) if (v < 0) ++ans;
cout << ans << endl;
return 0;
}