結果
| 問題 | No.697 池の数はいくつか |
| ユーザー |
|
| 提出日時 | 2026-08-31 08:48:30 |
| 言語 | C++23(gcc16) (gcc 16.1.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,467 ms / 6,000 ms |
| + 923µs | |
| コード長 | 1,460 bytes |
| 記録 | |
| コンパイル時間 | 4,727 ms |
| コンパイル使用メモリ | 363,732 KB |
| 実行使用メモリ | 136,636 KB |
| 最終ジャッジ日時 | 2026-08-31 08:49:00 |
| 合計ジャッジ時間 | 18,549 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
const pair<int, int> DXY[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct dsu{
vector<int> par, sz;
dsu(int n) : par(n), sz(n, 1){
iota(par.begin(), par.end(), 0);
}
int root(int x){
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
bool merge(int x, int y){
x = root(x), y = root(y);
if (x == y) return false;
if (sz[x] < sz[y]) swap(x, y);
par[y] = x, sz[x] += sz[y];
return true;
}
bool same(int x, int y){
return root(x) == root(y);
}
int size(int x){
return sz[root(x)];
}
};
int main(){
int H, W;
cin >> H >> W;
vector<vector<int>> A(H, vector<int>(W));
for (int i = 0; i < H; i++){
for (int j = 0; j < W; j++){
cin >> A[i][j];
}
}
auto id = [&](int i, int j){ return W*i+j; };
dsu uf(H*W);
for (int i = 0; i < H; i++){
for (int j = 0; j < W; j++){
for (auto [dx, dy] : DXY){
int nx = i+dx, ny = j+dy;
if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue;
if (A[i][j] && A[nx][ny]) uf.merge(id(i, j), id(nx, ny));
}
}
}
set<int> roots;
for (int i = 0; i < H; i++){
for (int j = 0; j < W; j++){
if (A[i][j]) roots.insert(uf.root(id(i, j)));
}
}
cout << roots.size() << endl;
}