結果

問題 No.697 池の数はいくつか
ユーザー CleyLCleyL
提出日時 2022-01-05 09:01:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 928 ms / 6,000 ms
コード長 1,309 bytes
コンパイル時間 966 ms
コンパイル使用メモリ 73,712 KB
実行使用メモリ 109,100 KB
最終ジャッジ日時 2024-04-25 21:07:06
合計ジャッジ時間 8,840 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 107 ms
15,240 KB
testcase_25 AC 107 ms
15,244 KB
testcase_26 AC 106 ms
15,072 KB
testcase_27 AC 106 ms
15,188 KB
testcase_28 AC 107 ms
15,372 KB
testcase_29 AC 919 ms
108,952 KB
testcase_30 AC 928 ms
109,000 KB
testcase_31 AC 917 ms
108,892 KB
testcase_32 AC 923 ms
109,068 KB
testcase_33 AC 924 ms
109,100 KB
testcase_34 AC 928 ms
109,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
struct UnionFind {
  int n;
  vector<int> par;
  vector<int> size_;
  int whole;
  UnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1), whole(n_){
    for(int i = 0; n > i; i++)par[i] = i;
  }

  int root(int x){
    if(par[x] == x)return x;
    return par[x] = root(par[x]);
  }

  void unite(int a,int b){
    int ra = root(a);
    int rb = root(b);
    if(ra==rb)return;
    if(size(ra) > size(rb)) swap(ra,rb);
    par[ra] = rb;
    size_[rb] += size_[ra];
    whole--;
  }

  bool same(int a, int b){
    return root(a) == root(b);
  }

  int size(int a){
    return size_[root(a)];
  }
  void debug(){
    for(int i = 0; n > i; i++){
      cout << size_[root(i)] << " ";
    }
    cout << endl;

    return;
  }
};
int main(){
    int h,w;
    scanf("%d%d",&h,&w);
    vector<vector<int>> A(h,vector<int>(w));
    UnionFind B(h*w);
    int zero = 0;
    for(int i = 0; h > i; i++){
        for(int j = 0; w > j; j++){
          scanf("%d",&A[i][j]);
          if(A[i][j] == 1){
            if(i && A[i-1][j] == 1){
              B.unite(w*i+j,w*(i-1)+j);
            }
            if(j && A[i][j-1] == 1){
              B.unite(w*i+j,w*i+j-1);
            }
          }else zero++;
        }
    }
    printf("%d\n",B.whole-zero);
}
0