結果

問題 No.697 池の数はいくつか
ユーザー yuppe19 😺
提出日時 2019-03-22 09:00:51
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 197 ms / 6,000 ms
コード長 1,133 bytes
コンパイル時間 747 ms
コンパイル使用メモリ 70,892 KB
実行使用メモリ 38,656 KB
最終ジャッジ日時 2024-11-08 08:10:32
合計ジャッジ時間 4,017 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

constexpr int inf = 1 << 30;

struct UnionFind {
  vector<int> par;
  explicit UnionFind(int n) : par(n, -1) {}
  int root(int a) {
    if(par[a] < 0) { return a; }
    return par[a] = root(par[a]);
  }
  int size(int a) {
    return -par[root(a)];
  }
  void unite(int a, int b) {
    a = root(a);
    b = root(b);
    if(a == b) { return; }
    if(size(a) < size(b)) { swap(a, b); }
    par[a] += par[b];
    par[b] = a;
  }
};

int main(void) {
  int R, C; scanf("%d %d\n", &R, &C);
  int N = R * C;
  vector<int> prev(C), curr(C);
  UnionFind uf(N);
  for(int r=0, ptr=0; r<R; ++r) {
    for(int c=0; c<C; ++c, ++ptr) {
      curr[c] = getchar_unlocked(); getchar_unlocked();
      switch(curr[c]) {
        case '1':
          if(c && curr[c-1] == '1') { uf.unite(ptr, ptr-1); }
          if(     prev[c]   == '1') { uf.unite(ptr, ptr-C); }
          break;
        default:
          uf.par[ptr] = inf;
      }
    }
    swap(prev, curr);
  }
  int res = 0;
  for(int ptr=0; ptr<N; ++ptr) {
    if(uf.par[ptr] < 0) { ++res; }
  }
  printf("%d\n", res);
  return 0;
}
0