結果
| 問題 |
No.697 池の数はいくつか
|
| ユーザー |
hiro71687k
|
| 提出日時 | 2024-11-26 16:00:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,839 ms / 6,000 ms |
| コード長 | 1,807 bytes |
| コンパイル時間 | 4,313 ms |
| コンパイル使用メモリ | 253,784 KB |
| 最終ジャッジ日時 | 2025-02-25 06:16:00 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
using ll = int;
using ld = long double;
struct unionfind
{
vector<ll>par,siz;
unionfind(ll n): par(n,-1),siz(n,1){}
ll root(ll x){
if (par[x]==-1)
{
return x;
}else{
return par[x]=root(par[x]);
}
}
bool issame(ll x,ll y){
return root(x)==root(y);
}
bool unite(ll x,ll y){
x=root(x);
y=root(y);
if (x==y)
{
return false;
}
if (siz[x]<siz[y])
{
swap(x,y);
}
par[y]=x;
siz[x]+=siz[y];
return true;
}
ll size(ll x){
return siz[root(x)];
}
};
int main(){
ll h,w;
cin >> h >> w;
vector<vector<ll>>a(h,vector<ll>(w));
for (ll i = 0; i < h; i++)
{
for (ll j = 0; j < w; j++)
{
cin >> a[i][j];
}
}
unionfind uf(h*w);
for (ll i = 0; i < h; i++)
{
for (ll j = 0; j < w; j++)
{
if (i!=0)
{
if (a[i-1][j]&&a[i][j])
{
uf.unite((i-1)*w+j,i*w+j);
}
}
if (j!=0)
{
if (a[i][j-1]&&a[i][j])
{
uf.unite(i*w+j-1,i*w+j);
}
}
}
}
vector<ll>memo(h*w,0);
ll ans=0;
for (ll i = 0; i < h; i++)
{
for (ll j = 0; j < w; j++)
{
if (a[i][j]==0)
{
continue;
}
if (memo[uf.root(i*w+j)]==0)
{
ans++;
}
memo[uf.root(i*w+j)]++;
}
}
cout << ans << endl;
}
hiro71687k