結果

問題 No.697 池の数はいくつか
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-03-22 09:00:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 6,000 ms
コード長 1,133 bytes
コンパイル時間 554 ms
コンパイル使用メモリ 71,232 KB
実行使用メモリ 38,224 KB
最終ジャッジ日時 2023-08-08 02:24:11
合計ジャッジ時間 3,824 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 22 ms
7,028 KB
testcase_25 AC 22 ms
6,964 KB
testcase_26 AC 22 ms
6,920 KB
testcase_27 AC 22 ms
7,020 KB
testcase_28 AC 22 ms
7,028 KB
testcase_29 AC 173 ms
38,220 KB
testcase_30 AC 176 ms
38,224 KB
testcase_31 AC 173 ms
38,212 KB
testcase_32 AC 177 ms
38,092 KB
testcase_33 AC 179 ms
38,064 KB
testcase_34 AC 177 ms
38,204 KB
権限があれば一括ダウンロードができます

ソースコード

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