Submission #74275075


Source Code Expand

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
// disable assert
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

namespace nachia {

struct DsuFast{
private:
    std::vector<int> w;
public:
    DsuFast(int n = 0) : w(n, -1) {}
    int leader(int u){
        if(w[u] < 0) return u;
        return w[u] = leader(w[u]);
    }
    int operator[](int u){ return leader(u); }
    int merge(int u, int v){
        u = leader(u);
        v = leader(v);
        if(u == v) return u;
        if(-w[u] < -w[v]) std::swap(u, v);
        w[u] += w[v];
        w[v] = u;
        return u;
    }
    int size(int u){ return -w[leader(u)]; }
    bool same(int u, int v){ return leader(u) == leader(v); }
};

} // namespace nachia
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template <class T> using V = vector<T>;
template <class A, class B> void chmax(A& l, const B& r){ if(l < r) l = r; }
template <class A, class B> void chmin(A& l, const B& r){ if(r < l) l = r; }

void testcase(){
  ll H, W; cin >> H >> W;
  V<string> A(H); REP(y,H) cin >> A[y];
  nachia::DsuFast dsu(H*W);
  auto Idx = [&](ll y, ll x) { return y* W + x; };
  REP(y,H) REP(x,W-1) if(A[y][x] == '.' && A[y][x+1] == '.') dsu.merge(Idx(y,x), Idx(y,x+1));
  REP(y,H-1) REP(x,W) if(A[y][x] == '.' && A[y+1][x] == '.') dsu.merge(Idx(y,x), Idx(y+1,x));
  V<ll> wt(H*W, 1);
  REP(y,H) REP(x,W) if(y == 0 || y == H-1 || x == 0 || x == W-1) wt[dsu[Idx(y,x)]] = 0;
  ll ans = 0;
  REP(y,H) REP(x,W) if(A[y][x] == '.' && dsu[Idx(y,x)] == Idx(y,x)) ans += wt[Idx(y,x)];
  cout << ans << "\n";
}

int main(){
  cin.tie(0)->sync_with_stdio(0);
  testcase();
  return 0;
}

Submission Info

Submission Time
Task C - Puddles
User Nachia
Language C++23 (GCC 15.2.0)
Score 300
Code Size 1794 Byte
Status AC
Exec Time 28 ms
Memory 16376 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 2
AC × 24
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All min.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
min.txt AC 1 ms 3508 KiB
random_01.txt AC 17 ms 16376 KiB
random_02.txt AC 6 ms 7120 KiB
random_03.txt AC 17 ms 16184 KiB
random_04.txt AC 8 ms 9712 KiB
random_05.txt AC 24 ms 16216 KiB
random_06.txt AC 2 ms 3912 KiB
random_07.txt AC 25 ms 16344 KiB
random_08.txt AC 2 ms 3752 KiB
random_09.txt AC 28 ms 16308 KiB
random_10.txt AC 21 ms 12872 KiB
random_11.txt AC 28 ms 16084 KiB
random_12.txt AC 1 ms 3588 KiB
random_13.txt AC 21 ms 16132 KiB
random_14.txt AC 3 ms 4312 KiB
random_15.txt AC 21 ms 16328 KiB
random_16.txt AC 7 ms 7416 KiB
random_17.txt AC 8 ms 16096 KiB
random_18.txt AC 13 ms 16372 KiB
random_19.txt AC 9 ms 16312 KiB
random_20.txt AC 10 ms 16308 KiB
random_21.txt AC 10 ms 16240 KiB
sample_01.txt AC 1 ms 3668 KiB
sample_02.txt AC 1 ms 3672 KiB